Skip to content

Commit bec1b39

Browse files
committed
feat: enhance Docker build process and inject version at build time
1 parent 8932473 commit bec1b39

3 files changed

Lines changed: 125 additions & 9 deletions

File tree

.github/workflows/docker-build-dev.yml

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,31 @@ name: Build and Push Development Docker Images
33
on:
44
push:
55
branches: [ "main" ]
6+
7+
concurrency:
8+
group: docker
9+
610
jobs:
711
build-and-push:
8-
runs-on: ubuntu-latest
12+
runs-on: ${{ matrix.runner }}
913
timeout-minutes: 30
10-
concurrency:
11-
group: docker
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
include:
19+
- arch: amd64
20+
runner: ubuntu-24.04
21+
- arch: arm64
22+
runner: ubuntu-24.04-arm
1223

1324
permissions:
1425
contents: read
1526
packages: write
1627

1728
steps:
1829
- name: Checkout code
19-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
30+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2031
with:
2132
persist-credentials: false
2233

@@ -34,15 +45,75 @@ jobs:
3445
run: |
3546
echo "REPO_LC=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
3647
37-
- name: Build and push Docker image
48+
- name: Compute version string
49+
run: |
50+
SHA12="${GITHUB_SHA:0:12}"
51+
BRANCH="${GITHUB_REF_NAME}"
52+
if [ -z "${BRANCH}" ]; then
53+
VERSION="dev"
54+
else
55+
VERSION="dev/${BRANCH}@${SHA12}"
56+
fi
57+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
58+
59+
- name: Build and push Docker image (single arch)
3860
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
3961
with:
4062
context: .
4163
push: true
42-
platforms: linux/amd64,linux/arm64
43-
tags: ghcr.io/${{ env.REPO_LC }}-snapshot:dev
64+
platforms: linux/${{ matrix.arch }}
65+
tags: ghcr.io/${{ env.REPO_LC }}-snapshot:dev-${{ matrix.arch }}
66+
build-args: |
67+
VERSION=${{ env.VERSION }}
68+
69+
manifest:
70+
needs: build-and-push
71+
runs-on: ubuntu-24.04
72+
timeout-minutes: 10
73+
74+
permissions:
75+
contents: read
76+
packages: write
77+
78+
steps:
79+
- name: Set up Docker Buildx
80+
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
81+
82+
- name: Login to GitHub Container Registry
83+
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
84+
with:
85+
registry: ghcr.io
86+
username: ${{ github.actor }}
87+
password: ${{ secrets.GITHUB_TOKEN }}
4488

89+
- name: Set lowercase repository name
90+
run: |
91+
echo "REPO_LC=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV
92+
93+
- name: Create and push multi-arch manifest tag
94+
shell: bash
95+
run: |
96+
IMAGE="ghcr.io/${REPO_LC}-snapshot"
97+
98+
docker buildx imagetools create \
99+
-t "${IMAGE}:dev" \
100+
"${IMAGE}:dev-amd64" \
101+
"${IMAGE}:dev-arm64"
102+
103+
docker buildx imagetools inspect "${IMAGE}:dev"
104+
105+
cleanup:
106+
needs: manifest
107+
runs-on: ubuntu-latest
108+
timeout-minutes: 10
109+
110+
permissions:
111+
contents: read
112+
packages: write
113+
114+
steps:
45115
- name: Set lowercase package name
116+
shell: bash
46117
run: |
47118
PACKAGE_NAME=${GITHUB_REPOSITORY##*/}
48119
echo "PACKAGE_NAME_LC=${PACKAGE_NAME,,}" >> $GITHUB_ENV
@@ -55,3 +126,26 @@ jobs:
55126
package-type: container
56127
min-versions-to-keep: 10
57128
delete-only-untagged-versions: 'true'
129+
130+
trigger-rollout:
131+
needs: manifest
132+
runs-on: arc-runner-set
133+
timeout-minutes: 10
134+
135+
permissions:
136+
id-token: write
137+
contents: read
138+
139+
steps:
140+
- name: Set lowercase repository name
141+
shell: bash
142+
run: |
143+
echo "REPO_LC=${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
144+
145+
- name: Trigger rollout
146+
uses: unitvectory-labs/kuberollouttrigger-action@e95cb8eb9a669a16363c13734a3663a7bcca54f2 # v0.2.0
147+
with:
148+
audience: ${{ secrets.KUBEROLLOUTTRIGGER_AUD }}
149+
url: ${{ secrets.KUBEROLLOUTTRIGGER_URL }}
150+
image: ghcr.io/${{ env.REPO_LC }}-snapshot
151+
tags: dev,dev-amd64,dev-arm64

Dockerfile

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ FROM golang:1.25.7 AS builder
44
# Set the working directory inside the container
55
WORKDIR /app
66

7+
# Build argument for version injection
8+
ARG VERSION=dev
9+
710
# Copy the Go modules manifest and download dependencies
811
COPY go.mod go.sum ./
912
RUN go mod download
@@ -14,8 +17,8 @@ COPY . .
1417
# Ensures a statically linked binary
1518
ENV CGO_ENABLED=0
1619

17-
# Build the Go server
18-
RUN go build -mod=readonly -o server .
20+
# Build the Go server with version injection
21+
RUN go build -mod=readonly -o server -ldflags "-X 'main.Version=${VERSION}'" .
1922

2023
# Use a minimal base image for running the compiled binary
2124
FROM gcr.io/distroless/base-debian13
@@ -26,5 +29,8 @@ COPY --from=builder /app/server /server
2629
# Expose the port that the server will listen on
2730
EXPOSE 8080
2831

32+
# Run as non-root user
33+
USER 65532:65532
34+
2935
# Run the server binary
3036
CMD ["/server"]

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import (
77
"log"
88
"net/http"
99
"os"
10+
"runtime/debug"
1011
)
1112

13+
// Version is the application version, injected at build time via ldflags
14+
var Version = "dev"
15+
1216
type LogEntry struct {
1317
BodyBase64 string `json:"bodyBase64"`
1418
Headers map[string]string `json:"headers"`
@@ -49,12 +53,24 @@ func handler(w http.ResponseWriter, r *http.Request) {
4953
// Log the JSON
5054
log.Println(string(logJSON))
5155

56+
// Add application version to X-App-Version header
57+
w.Header().Set("X-App-Version", Version)
58+
5259
// Respond to the client
5360
w.WriteHeader(http.StatusOK)
5461
_, _ = w.Write([]byte("OK\n"))
5562
}
5663

5764
func main() {
65+
// Set the build version from the build info if not set by the build system
66+
if Version == "dev" || Version == "" {
67+
if bi, ok := debug.ReadBuildInfo(); ok {
68+
if bi.Main.Version != "" && bi.Main.Version != "(devel)" {
69+
Version = bi.Main.Version
70+
}
71+
}
72+
}
73+
5874
port := os.Getenv("PORT")
5975
if port == "" {
6076
port = "8080" // Default port if not specified

0 commit comments

Comments
 (0)