From 539088359c2082b3f8f1548aeea57d15d63d806b Mon Sep 17 00:00:00 2001 From: Ayu Date: Tue, 3 Sep 2024 21:23:26 +0300 Subject: [PATCH] ci: include version and commit in docker images (#136) * ci: include version and commit in docker images * ci: add checkout again * ci: generate client bundles for docker --- .github/workflows/docker-edge.yml | 11 ++++++++++- .github/workflows/docker-latest.yml | 11 ++++++++++- Dockerfile | 11 ++++++++++- core/Taskfile.yaml | 15 +++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/.github/workflows/docker-edge.yml b/.github/workflows/docker-edge.yml index 9342cfc4..e9533795 100644 --- a/.github/workflows/docker-edge.yml +++ b/.github/workflows/docker-edge.yml @@ -22,11 +22,17 @@ jobs: id-token: write steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + - name: Save GitHub tag and commit sha to environment + run: | + echo "VERSION=Edge" >> $GITHUB_ENV + echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + - name: Log in to the Container registry uses: docker/login-action@v3 with: @@ -53,6 +59,9 @@ jobs: with: context: . push: true + build-args: | + VERSION=${{ env.VERSION }} + COMMIT_SHA=${{ env.COMMIT_SHA }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64 diff --git a/.github/workflows/docker-latest.yml b/.github/workflows/docker-latest.yml index bea01055..d60096a6 100644 --- a/.github/workflows/docker-latest.yml +++ b/.github/workflows/docker-latest.yml @@ -19,11 +19,17 @@ jobs: id-token: write steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + - name: Save GitHub tag and commit sha to environment + run: | + echo "VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV + echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + - name: Log in to the Container registry uses: docker/login-action@v3 with: @@ -49,6 +55,9 @@ jobs: with: context: . push: true + build-args: | + VERSION=${{ env.VERSION }} + COMMIT_SHA=${{ env.COMMIT_SHA }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64,linux/arm64 diff --git a/Dockerfile b/Dockerfile index c43d1909..c2b1ceb3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,12 @@ FROM jdxcode/mise:latest AS build +ARG VERSION=development +ARG COMMIT_SHA=development + +ENV VERSION=${VERSION} +ENV COMMIT_SHA=${COMMIT_SHA} + WORKDIR /app # Install unzip dependency for bun @@ -28,9 +34,12 @@ RUN --mount=type=cache,target=${GOCACHE} \ RUN bun install --frozen-lockfile +# Verify environment variables +RUN echo "VERSION=${VERSION}" && echo "COMMIT_SHA=${COMMIT_SHA}" + # Copy the rest of the source code COPY . . -RUN --mount=type=cache,target=${GOCACHE} ~/bin/task core:release +RUN --mount=type=cache,target=${GOCACHE} ~/bin/task core:release:docker # Build the final image FROM gcr.io/distroless/cc-debian12 diff --git a/core/Taskfile.yaml b/core/Taskfile.yaml index 1a292cdb..fdb55598 100644 --- a/core/Taskfile.yaml +++ b/core/Taskfile.yaml @@ -88,7 +88,19 @@ tasks: env: CGO_ENABLED: "1" + # Centralise Docker release command into here for simpler maintenance instead of specifying + # within Dockerfile + release:docker: + deps: [generate] + cmds: + - go build -o ./bin/main -ldflags "-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT_SHA" -trimpath -tags "no_duckdb_arrow" ./cmd/ + - chmod +x ./bin/main + - echo "docker build done" + env: + CGO_ENABLED: "1" + release:linux:amd64: + deps: [generate] cmds: - go build -o ./bin/medama_linux_amd64 -ldflags "-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT_SHA" -trimpath -tags "no_duckdb_arrow" ./cmd/ - chmod +x ./bin/medama_linux_amd64 @@ -99,6 +111,7 @@ tasks: GOARCH: "amd64" release:linux:arm64: + deps: [generate] cmds: - go build -o ./bin/medama_linux_arm64 -ldflags "-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT_SHA" -trimpath -tags "no_duckdb_arrow" ./cmd/ - chmod +x ./bin/medama_linux_arm64 @@ -111,6 +124,7 @@ tasks: GOARCH: "arm64" release:darwin:amd64: + deps: [generate] cmds: - go build -o ./bin/medama_darwin_amd64 -ldflags "-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT_SHA" -trimpath -tags "no_duckdb_arrow" ./cmd/ - chmod +x ./bin/medama_darwin_amd64 @@ -121,6 +135,7 @@ tasks: GOARCH: "amd64" release:darwin:arm64: + deps: [generate] cmds: - go build -o ./bin/medama_darwin_arm64 -ldflags "-s -w -X main.Version=$VERSION -X main.Commit=$COMMIT_SHA" -trimpath -tags "no_duckdb_arrow" ./cmd/ - chmod +x ./bin/medama_darwin_arm64