From d529531fea86f1eb5ff915e72353b4a77b959306 Mon Sep 17 00:00:00 2001 From: Drew Edwards Date: Thu, 1 Feb 2024 18:27:01 +0000 Subject: [PATCH] fix: retain version info in builds --- .github/workflows/docker.yml | 17 +++++++++++------ .github/workflows/go.yml | 4 ++-- .github/workflows/release.yml | 10 ++++++---- .goreleaser.yml | 4 ---- .prerelease.sh | 6 ------ Dockerfile | 18 +++++++++++++++--- casket/casketmain/run.go | 26 +++++++++++++++++--------- casket/main.go | 8 ++++++++ 8 files changed, 59 insertions(+), 34 deletions(-) delete mode 100644 .prerelease.sh diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index d68233ab..f860b612 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -19,10 +19,13 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 + with: + # TODO: fetch-tags: true may also work here (commits are required for the goreleaser changelog, though) + fetch-depth: 0 # Used to get tags to populate the version number - name: Log in to the Container registry - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ github.actor }} @@ -30,7 +33,7 @@ jobs: - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@v4 + uses: docker/metadata-action@v5 with: images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} tags: | @@ -42,16 +45,18 @@ jobs: latest=true - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Build and push Docker image - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5 with: context: . push: true platforms: ${{ env.PLATFORMS }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9805e7ca..721ffe93 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,13 +13,13 @@ jobs: steps: - name: Set up Go 1.x - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: go-version: 1.19 id: go - name: Check out code into the Go module directory - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Get dependencies run: go get -v -t ./... diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8b83e173..78774d36 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -10,12 +10,14 @@ jobs: runs-on: ubuntu-latest steps: - name: Set up Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v5 with: go-version: 1.19 - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Used to get tags to populate the version number - name: Go module init run: | @@ -24,9 +26,9 @@ jobs: cd .. - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 + uses: goreleaser/goreleaser-action@v5 with: version: latest - args: release --rm-dist + args: release --clean env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.goreleaser.yml b/.goreleaser.yml index 7d32f8ef..fa183bab 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -28,10 +28,6 @@ builds: # Set dist directory to build/ since we have files in dist/ dist: build -before: - hooks: - - sh ./.prerelease.sh - archives: - id: casket builds: diff --git a/.prerelease.sh b/.prerelease.sh deleted file mode 100644 index c073142e..00000000 --- a/.prerelease.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -sleep 20 # allow github to catch up -cd casket -GOPRIVATE=github.com/tmpim/casket go get "github.com/tmpim/casket@master" -go mod tidy -cd .. diff --git a/Dockerfile b/Dockerfile index e977048c..d63e7159 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,14 +2,26 @@ FROM golang:1.19-bullseye AS builder WORKDIR /workdir -COPY go.mod go.sum /workdir ENV GOPROXY=https://proxy.golang.org,direct +ENV GOPRIVATE=github.com/tmpim/casket +COPY go.mod go.sum /workdir RUN go mod download COPY . /workdir - WORKDIR /workdir/casket -RUN CGO_ENABLED=0 go build -o casket . + +# Required to build with version information - but allow this step to fail (e.g. we're building a PR). Casket will try +# to get the version from the module (this step) first, and then try to get it from `main.version` (goreleaser and +# ldflags). See also: +# - casket/casketmain/run.go#getBuildModule() +# - https://goreleaser.com/cookbooks/using-main.version/ +RUN go get "github.com/tmpim/casket@master"; exit 0 + +ENV CGO_ENABLED=0 +# -s: Omit the symbol table and debug information +# -w: Omit the DWARF symbol table +# -X: Include the git tag as the version (goreleaser also uses main.version tag) +RUN go build -ldflags="-s -w -X 'main.version=$(git describe --tags --dirty)'" -o casket . FROM alpine:3 diff --git a/casket/casketmain/run.go b/casket/casketmain/run.go index 2ad603a4..bef1d6b6 100644 --- a/casket/casketmain/run.go +++ b/casket/casketmain/run.go @@ -81,16 +81,24 @@ func init() { func Run() { flag.Parse() - module := getBuildModule() - cleanModVersion := strings.TrimPrefix(module.Version, "v") - casket.AppName = appName - casket.AppVersion = module.Version casket.OnProcessExit = append(casket.OnProcessExit, func() { // TODO: Redirect to our own logger instead of zap.NewNop() certmagic.CleanUpOwnLocks(context.TODO(), zap.NewNop()) }) - certmagic.UserAgent = appName + "/" + cleanModVersion + + // AppVersion may be set by ldflags or by the module + module := getBuildModule() + if module.Version != "" && module.Version != "(devel)" { + casket.AppVersion = module.Version + } + + if casket.AppVersion == "" { + casket.AppVersion = "unknown" + } + + cleanVersion := strings.TrimPrefix(casket.AppVersion, "v") + certmagic.UserAgent = appName + "/" + cleanVersion if !logTimestamps { // Disable timestamps for logging @@ -161,9 +169,9 @@ func Run() { if version { if module.Sum != "" { // a build with a known version will also have a checksum - fmt.Printf("Casket %s (%s)\n", module.Version, module.Sum) + fmt.Printf("Casket %s (%s)\n", casket.AppVersion, module.Sum) } else { - fmt.Println(module.Version) + fmt.Println("Casket " + casket.AppVersion) } os.Exit(0) } @@ -202,7 +210,7 @@ func Run() { } // Log Casket version before start - log.Printf("[INFO] Casket version: %s", module.Version) + log.Printf("[INFO] Casket version: %s", casket.AppVersion) // Start your engines instance, err := casket.Start(casketfileinput) @@ -211,7 +219,7 @@ func Run() { } // Begin telemetry (these are no-ops if telemetry disabled) - telemetry.Set("casket_version", module.Version) + telemetry.Set("casket_version", casket.AppVersion) telemetry.Set("num_listeners", len(instance.Servers())) telemetry.Set("server_type", serverType) telemetry.Set("os", runtime.GOOS) diff --git a/casket/main.go b/casket/main.go index 64a044d7..323f73ec 100644 --- a/casket/main.go +++ b/casket/main.go @@ -20,14 +20,22 @@ package main import ( + "github.com/tmpim/casket" "log" "github.com/tmpim/casket/casket/casketmain" ) +var ( + // Populated by ldflags (Docker builds) and goreleaser - https://goreleaser.com/cookbooks/using-main.version/ + version = "devel" +) + var run = casketmain.Run // replaced for tests func main() { + casket.AppVersion = version + log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Llongfile) run() }