Skip to content

Commit

Permalink
chore: propagate build info to Go binary in Docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Oct 30, 2024
1 parent d80de82 commit 7b58758
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ jobs:
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5.5.1
with:
images: ctferio/ctfd-setup

- name: Git commit date
id: infos
run: |
# trim version prefix
version=${{ github.ref_name }}
version="${version#"v"}"
echo "version=$version" >> "$GITHUB_OUTPUT"
# output date per RFC 3339
date="$(git log -1 --format=%cd --date=format:%Y-%m-%dT%H:%M:%SZ)"
echo "date=$date" >> "$GITHUB_OUTPUT"
- name: Build and push Docker image
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0
Expand All @@ -40,6 +52,14 @@ jobs:
sbom: true # may not produce SBOM in manifest if the image has no filesystem (e.g. "FROM scratch")
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
VERSION=$VERSION
COMMIT=$COMMIT
COMMIT_DATE=$COMMIT_DATE
env:
VERSION: ${{ steps.infos.outputs.version }}
COMMIT: ${{ github.sha }}
COMMIT_DATE: ${{ steps.infos.outputs.date }}

# This step calls the container workflow to generate provenance and push it to
# the container registry.
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ jobs:
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION_LDFLAGS: ${{ steps.ldflags.outputs.version }}

- name: Generate subject
id: hash
Expand Down
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ RUN go mod download
COPY . .

ENV CGO_ENABLED=0
RUN go build -cover -o /go/bin/ctfd-setup cmd/ctfd-setup/main.go
ARG VERSION="dev"
ARG COMMIT
ARG COMMIT_DATE
RUN go build -cover \
-ldflags="-s -w -X 'main.Version="$VERSION"' -X 'main.Commit="$COMMIT"' -X 'main.CommitDate="$COMMIT_DATE"'" \
-o /go/bin/ctfd-setup \
cmd/ctfd-setup/main.go



Expand Down
8 changes: 3 additions & 5 deletions cmd/ctfd-setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
Version = "dev"
Commit = ""
CommitDate = ""
TreeState = ""
)

const (
Expand Down Expand Up @@ -437,10 +436,9 @@ func main() {
},
Version: Version,
Metadata: map[string]any{
"version": Version,
"commit": Commit,
"date": CommitDate,
"treeState": TreeState,
"version": Version,
"commit": Commit,
"date": CommitDate,
},
}

Expand Down

0 comments on commit 7b58758

Please sign in to comment.