Fix build and versioning (borrowed from yarn)

This commit is contained in:
James Mills 2023-01-21 11:25:30 +10:00
parent c727404d91
commit 07d888c2c6
No known key found for this signature in database
GPG key ID: AC4C014F1440EBD6
6 changed files with 78 additions and 20 deletions

5
.gitignore vendored
View file

@ -5,8 +5,9 @@
/dist /dist
/tube /tube
tube /cmd/tube/tube
/.DS_Store
**/.DS_Store
videos/* videos/*
!videos/README.md !videos/README.md

View file

@ -39,9 +39,10 @@ COPY ./utils/*.go ./utils/
# COMMIT value to change. # COMMIT value to change.
ARG VERSION="0.0.0" ARG VERSION="0.0.0"
ARG COMMIT="HEAD" ARG COMMIT="HEAD"
ARG BUILD=""
# Build server binary # Build server binary
RUN make server VERSION=$VERSION COMMIT=$COMMIT RUN make server VERSION=$VERSION COMMIT=$COMMIT BUILD=$BUILD
# Runtime # Runtime
FROM alpine:latest FROM alpine:latest

View file

@ -2,9 +2,10 @@
.PHONY: help deps dev build install image release test clean clean-all .PHONY: help deps dev build install image release test clean clean-all
export CGO_ENABLED=0 export CGO_ENABLED=0
VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "0.0.0") VERSION=$(shell git describe --abbrev=0 --tags 2>/dev/null || echo "$VERSION")
COMMIT=$(shell git rev-parse --short HEAD || echo "HEAD") COMMIT=$(shell git rev-parse --short HEAD || echo "$COMMIT")
BRANCH=$(shell git rev-parse --abbrev-ref HEAD) BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
BUILD=$(shell git show -s --pretty=format:%cI)
GOCMD=go GOCMD=go
GOVER=$(shell go version | grep -o -E 'go1\.17\.[0-9]+') GOVER=$(shell go version | grep -o -E 'go1\.17\.[0-9]+')
@ -35,12 +36,23 @@ dev : DEBUG=1
dev : server ## Build debug version of tube dev : server ## Build debug version of tube
@./tube @./tube
server: generate ## Build the tube server server: generate ## Build the yarnd server
@$(GOCMD) build -tags "embed static_build" \ ifeq ($(DEBUG), 1)
@echo "Building in debug mode..."
@$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \
-ldflags "\
-X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT) \
-X $(shell go list).Build=$(BUILD)" \
./cmd/tube/...
else
@$(GOCMD) build $(FLAGS) -tags "netgo static_build" -installsuffix netgo \
-ldflags "-w \ -ldflags "-w \
-X $(shell go list).Version=$(VERSION) \ -X $(shell go list).Version=$(VERSION) \
-X $(shell go list).Commit=$(COMMIT)" \ -X $(shell go list).Commit=$(COMMIT) \
. -X $(shell go list).Build=$(BUILD)" \
./cmd/tube/...
endif
build: server ## Build the server build: server ## Build the server
@ -54,10 +66,18 @@ install: build ## Install tube to $DESTDIR
ifeq ($(PUBLISH), 1) ifeq ($(PUBLISH), 1)
image: generate ## Build the Docker image image: generate ## Build the Docker image
@docker buildx build --push --platform linux/arm64,linux/amd64 --tag $(IMAGE):$(TAG) --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" . @docker buildx build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg BUILD="$(BUILD)" \
--platform linux/amd64,linux/arm64 --push -t $(IMAGE):$(TAG) .
else else
image: generate image: generate
@docker buildx build --platform linux/arm64,linux/amd64 --tag $(IMAGE):$(TAG) --build-arg VERSION="$(VERSION)" --build-arg COMMIT="$(COMMIT)" . @docker build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg BUILD="$(BUILD)" \
-t $(IMAGE):$(TAG) .
endif endif
release: generate ## Release a new version to Gitea release: generate ## Release a new version to Gitea

View file

@ -4,9 +4,11 @@ import (
"fmt" "fmt"
"os" "os"
"git.mills.io/prologic/tube/app"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
"git.mills.io/prologic/tube"
"git.mills.io/prologic/tube/app"
) )
var ( var (
@ -36,7 +38,7 @@ func main() {
} }
if version { if version {
fmt.Printf("tube version %s", FullVersion()) fmt.Printf("tube version %s", tube.FullVersion())
os.Exit(0) os.Exit(0)
} }

2
doc.go Normal file
View file

@ -0,0 +1,2 @@
// Package tube is a Youtube-like (without censorship and features you don't need!) Video Sharing App
package tube

View file

@ -1,18 +1,50 @@
package main package tube
import ( import (
"fmt" "fmt"
runtime "runtime/debug"
"strings"
)
const (
defaultVersion = "0.0.0"
defaultCommit = "HEAD"
defaultBuild = "0000-01-01:00:00+00:00"
) )
var ( var (
// Version release version // Version is the tagged release version in the form <major>.<minor>.<patch>
Version = "0.0.1" // following semantic versioning and is overwritten by the build system.
Version = defaultVersion
// Commit will be overwritten automatically by the build system // Commit is the commit sha of the build (normally from Git) and is overwritten
Commit = "HEAD" // by the build system.
Commit = defaultCommit
// Build is the date and time of the build as an RFC3339 formatted string
// and is overwritten by the build system.
Build = defaultBuild
) )
// FullVersion returns the full version and commit hash // FullVersion display the full version and build
func FullVersion() string { func FullVersion() string {
return fmt.Sprintf("%s@%s", Version, Commit) var sb strings.Builder
isDefault := Version == defaultVersion && Commit == defaultCommit && Build == defaultBuild
if !isDefault {
sb.WriteString(fmt.Sprintf("%s@%s %s", Version, Commit, Build))
}
if info, ok := runtime.ReadBuildInfo(); ok {
if isDefault {
sb.WriteString(fmt.Sprintf(" %s", info.Main.Version))
}
sb.WriteString(fmt.Sprintf(" %s", info.GoVersion))
if info.Main.Sum != "" {
sb.WriteString(fmt.Sprintf(" %s", info.Main.Sum))
}
}
return sb.String()
} }