Skip to content

Commit 4a723e8

Browse files
committed
ci: Inject version at build time
1 parent f92cb3e commit 4a723e8

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

.github/workflows/release.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ jobs:
3333
- name: Set up Docker Buildx
3434
uses: docker/setup-buildx-action@v3
3535

36+
- name: Get version
37+
id: get_version
38+
run: |
39+
if [[ $GITHUB_REF == refs/tags/* ]]; then
40+
VERSION=${GITHUB_REF#refs/tags/}
41+
else
42+
VERSION=dev-${GITHUB_SHA::8}
43+
fi
44+
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
45+
3646
- name: Extract metadata (tags, labels) for Docker
3747
id: meta
3848
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
@@ -49,6 +59,8 @@ jobs:
4959
platforms: linux/amd64,linux/arm64
5060
tags: ${{ steps.meta.outputs.tags }}
5161
labels: ${{ steps.meta.outputs.labels }}
62+
build-args: |
63+
VERSION=${{ steps.get_version.outputs.VERSION }}
5264
5365
- name: Generate artifact attestation
5466
uses: actions/attest-build-provenance@v2

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ LABEL org.opencontainers.image.source=https://github.com/wille/haprovider
44

55
ARG TARGETOS
66
ARG TARGETARCH
7+
ARG VERSION=dev
78

89
WORKDIR /haprovider
910

@@ -13,7 +14,7 @@ RUN go mod download
1314

1415
COPY . .
1516

16-
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o haprovider cmd/haprovider/main.go
17+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -ldflags "-X github.com/wille/haprovider/internal.Version=${VERSION}" -o haprovider cmd/haprovider/main.go
1718

1819
FROM gcr.io/distroless/static:nonroot
1920
WORKDIR /

internal/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package internal
22

3-
const Version string = "1.0.0"
3+
var Version string = "dev"

0 commit comments

Comments
 (0)