Skip to content

Commit

Permalink
fix: retain version info in builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemmmy committed Feb 1, 2024
1 parent ceb8a7c commit d529531
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
17 changes: 11 additions & 6 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ 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 }}
password: ${{ secrets.GITHUB_TOKEN }}

- 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: |
Expand All @@ -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
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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 }}
4 changes: 0 additions & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions .prerelease.sh

This file was deleted.

18 changes: 15 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions casket/casketmain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions casket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit d529531

Please sign in to comment.