From 8959e53270495bb9bc39fc56f0a35549ac275dd5 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Wed, 13 Dec 2023 01:09:21 +0100 Subject: [PATCH] Generate version and commit refs for built docker image --- .github/workflows/release.yml | 15 +++++++++++++++ Dockerfile | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3cb9cbf..caef26b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,6 +29,18 @@ jobs: type=semver,pattern={{major}} type=edge,branch=${{ github.event.repository.default_branch }} + - id: version + name: Set up build version + run: | + if [[ $GITHUB_REF_TYPE == "tag" ]]; then + VERSION=${GITHUB_REF#refs/tags/} + else + BRANCH_NAME=${GITHUB_REF#refs/heads/} + SHORT_SHA=$(git rev-parse --short $GITHUB_SHA) + VERSION="${BRANCH_NAME}-${SHORT_SHA}" + fi + echo "version=$VERSION" >> $GITHUB_ENV + - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -48,3 +60,6 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64 + build-args: | + VERSION=${{ steps.version.output.version }} + COMMIT=${{ github.ref }} diff --git a/Dockerfile b/Dockerfile index 5e74ded..67a85a4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,9 @@ FROM golang:1.21-alpine AS builder +ARG VERSION=unversioned +ARG COMMIT=unspecified + COPY . /build WORKDIR /build RUN go mod download @@ -9,6 +12,7 @@ RUN go mod download RUN CGO_ENABLED=0 \ go build \ -trimpath \ + -ldflags "-X github.com/elyby/chrly/version.version=$VERSION -X github.com/elyby/chrly/version.commit=$COMMIT" \ -ldflags="-w -s" \ -o chrly \ main.go