From 060b5705c8d548146479121723dae74ec05c1dd6 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 23 Nov 2024 17:01:50 -0800 Subject: [PATCH] Fix version --- .forgejo/workflows/build-and-release.yaml | 2 ++ Containerfile | 3 ++- config/version.go | 30 +++++++++++++++++++++++ frontend/frontend.go | 5 +++- 4 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 config/version.go diff --git a/.forgejo/workflows/build-and-release.yaml b/.forgejo/workflows/build-and-release.yaml index 83131e2..3351bbe 100644 --- a/.forgejo/workflows/build-and-release.yaml +++ b/.forgejo/workflows/build-and-release.yaml @@ -21,6 +21,8 @@ jobs: labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64,linux/arm/v7 push: true + build-args: | + VERSION_STRING=${{ env.GITHUB_REF_NAME }} - name: update hassio-addons if: startsWith(github.ref, 'refs/tags/v') run: | diff --git a/Containerfile b/Containerfile index 35afa7a..67272bd 100644 --- a/Containerfile +++ b/Containerfile @@ -18,7 +18,8 @@ FROM alpine:latest AS build RUN apk add --no-cache go ADD . /go/lockserver WORKDIR /go/lockserver -RUN CGO_ENABLED=0 go build ./cmd/lockserver +ARG VERSION_STRING +RUN CGO_ENABLED=0 go build -ldflags "-X git.devhack.net/devhack/member-services/config.Version=${VERSION_STRING}" ./cmd/lockserver FROM scratch COPY --from=build /go/lockserver/lockserver /lockserver diff --git a/config/version.go b/config/version.go new file mode 100644 index 0000000..d611b7c --- /dev/null +++ b/config/version.go @@ -0,0 +1,30 @@ +package config + +import ( + "runtime/debug" + + "github.com/sirupsen/logrus" +) + +var ( + BuildInfo *debug.BuildInfo + Version string +) + +func init() { + var ok bool + BuildInfo, ok = debug.ReadBuildInfo() + if !ok { + logrus.Error("failed to read build info") + return + } + + if Version == "" { + for _, setting := range BuildInfo.Settings { + if setting.Key == "vcs.revision" { + Version = setting.Value + break + } + } + } +} diff --git a/frontend/frontend.go b/frontend/frontend.go index eb6b1f2..a57ea99 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -2,8 +2,11 @@ package frontend import ( "embed" + "fmt" "html/template" "io/fs" + + "git.janky.solutions/finn/lockserver/config" ) var ( @@ -16,7 +19,7 @@ var ( Templates *template.Template funcs = template.FuncMap{ - "version": func() string { return "better-zwave-locks v0.x.x aaaaaaaa" }, + "version": func() string { return fmt.Sprintf("better-zwave-locks %s", config.Version) }, } )