Skip to content

Commit ed07365

Browse files
authored
fix: improve Docker build caching strategy (#336)
* fix: improve Docker build caching strategy * fix: enhance Docker build caching with GitHub Container registry login
1 parent ba7ccac commit ed07365

3 files changed

Lines changed: 64 additions & 10 deletions

File tree

.github/workflows/publish-version.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ jobs:
4242
username: ${{ secrets.DOCKER_USERNAME }}
4343
password: ${{ secrets.DOCKER_PASSWORD }}
4444

45+
# Required for the registry-backed build cache below
46+
- name: Log in to the GitHub Container registry
47+
uses: docker/login-action@v4
48+
with:
49+
registry: ghcr.io
50+
username: ${{ github.actor }}
51+
password: ${{ secrets.GITHUB_TOKEN }}
52+
4553
- name: Build and push Docker image
4654
id: push
4755
uses: docker/build-push-action@v7
@@ -54,8 +62,12 @@ jobs:
5462
tags: |
5563
digitalenvironments/hawki:latest
5664
digitalenvironments/hawki:${{ steps.version.outputs.version }}
57-
cache-from: type=gha,scope=${{ steps.version.outputs.version }}
58-
cache-to: type=gha,mode=max,scope=${{ steps.version.outputs.version }}
65+
cache-from: |
66+
type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-amd64
67+
type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-arm64
68+
# No cache-to on purpose: exporting a combined multi-arch cache here
69+
# is what triggered the 504s, and nothing reads such a ref anyway -
70+
# the test matrix keeps both per-arch refs up to date.
5971

6072
- name: Generate artifact attestation
6173
uses: actions/attest@v4

.github/workflows/rebuild-latest-image.yml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ jobs:
4949
username: ${{ secrets.DOCKER_USERNAME }}
5050
password: ${{ secrets.DOCKER_PASSWORD }}
5151

52+
# Required to read the registry-backed build cache below
53+
- name: Log in to the GitHub Container registry
54+
uses: docker/login-action@v4
55+
with:
56+
registry: ghcr.io
57+
username: ${{ github.actor }}
58+
password: ${{ secrets.GITHUB_TOKEN }}
59+
5260
- name: Build and push Docker image
5361
id: push
5462
uses: docker/build-push-action@v7
@@ -61,8 +69,14 @@ jobs:
6169
tags: |
6270
digitalenvironments/hawki:latest
6371
digitalenvironments/hawki:${{ steps.get_version.outputs.version }}
64-
cache-from: type=gha,scope=${{ steps.get_version.outputs.version }}
65-
cache-to: type=gha,mode=max,scope=${{ steps.get_version.outputs.version }}
72+
# Imports the per-arch caches written by the release gate in
73+
# trigger-release.yml. A hotfix rebuild of main may only hit these
74+
# partially, which just means a slower build - never a failure.
75+
cache-from: |
76+
type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-amd64
77+
type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-arm64
78+
# No cache-to on purpose: exporting a combined multi-arch cache is
79+
# what triggered the 504s, and nothing reads such a ref anyway.
6680

6781
- name: Generate artifact attestation
6882
uses: actions/attest@v4

.github/workflows/trigger-release.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@ jobs:
2222
uses: hawk-digital-environments/hawk-pipeline-actions/validate-release-branch@v1
2323

2424
test-docker-build:
25-
name: Test Docker Build
26-
runs-on: ubuntu-latest
25+
name: Test Docker Build (${{ matrix.arch }})
26+
runs-on: ${{ matrix.runner }}
2727
needs: validate
28+
permissions:
29+
contents: read
30+
packages: write
31+
strategy:
32+
# Always test both architectures, so an arch-specific breakage is
33+
# visible instead of being masked by the other job failing first.
34+
fail-fast: false
35+
matrix:
36+
include:
37+
- platform: linux/amd64
38+
arch: amd64
39+
runner: ubuntu-latest
40+
- platform: linux/arm64
41+
arch: arm64
42+
runner: ubuntu-latest
2843
steps:
2944
- name: Checkout release branch
3045
uses: actions/checkout@v7
@@ -35,17 +50,30 @@ jobs:
3550
- name: Set up Docker Buildx
3651
uses: docker/setup-buildx-action@v4
3752

53+
# Required for the registry-backed build cache below
54+
- name: Log in to the GitHub Container registry
55+
uses: docker/login-action@v4
56+
with:
57+
registry: ghcr.io
58+
username: ${{ github.actor }}
59+
password: ${{ secrets.GITHUB_TOKEN }}
60+
3861
- name: Test Docker build
3962
uses: docker/build-push-action@v7
4063
with:
4164
context: .
4265
file: ./Dockerfile
4366
push: false
67+
# Runs the full build, then discards the image. Keeps this a real
68+
# build test while populating the cache for the publish pipeline.
69+
outputs: type=cacheonly
4470
target: app_prod
45-
platforms: linux/amd64,linux/arm64
46-
tags: digitalenvironments/hawki:release-test
47-
cache-from: type=gha,scope=${{ needs.validate.outputs.version }}
48-
cache-to: type=gha,mode=max,scope=${{ needs.validate.outputs.version }}
71+
platforms: ${{ matrix.platform }}
72+
# One cache ref per architecture: keeps each export small enough to
73+
# avoid the 504s seen when exporting one combined multi-arch cache.
74+
cache-from: type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-${{ matrix.arch }}
75+
# ignore-error: a flaky cache export must not fail the release gate
76+
cache-to: type=registry,ref=ghcr.io/hawk-digital-environments/hawki:buildcache-${{ matrix.arch }},mode=max,ignore-error=true
4977

5078
release:
5179
name: Merge and Tag Release

0 commit comments

Comments
 (0)