Skip to content

Commit bf28c3b

Browse files
Prevent re-tagging from rebuilding image
Pass the build image around a given GHA workflow run using artifacts rather than the registry, manually pushing all tags at the end of the build.
1 parent 2954f78 commit bf28c3b

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

.github/workflows/build.yml

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ name: Build / Test / Push
33
on:
44
push:
55
branches:
6-
- '*'
6+
- '**'
77
workflow_dispatch:
88

9+
env:
10+
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
11+
DOCKER_METADATA_SET_OUTPUT_ENV: "true"
12+
IMAGE_ARTIFACT_NAME: iipsrv-${{ github.run_id }}_${{ github.run_attempt }}.tar
13+
914
jobs:
1015
build:
1116
runs-on: ubuntu-latest
1217
outputs:
13-
build-image: ${{ steps.build-meta.outputs.tags }}
18+
build-image: ${{ steps.id-image-tag.outputs.build_image }}
1419
steps:
1520
- name: Checkout code
1621
uses: actions/checkout@v4
@@ -21,38 +26,43 @@ jobs:
2126
- name: Set up Docker Buildx
2227
uses: docker/setup-buildx-action@v3
2328

24-
- name: Login to GitHub Container Registry
25-
uses: docker/login-action@v3
26-
with:
27-
registry: ghcr.io
28-
username: ${{ github.actor }}
29-
password: ${{ secrets.GITHUB_TOKEN }}
30-
31-
- name: Produce the build image tag
32-
id: build-meta
29+
- id: build-meta
30+
name: Produce the build image tag
3331
uses: docker/metadata-action@v5
3432
with:
3533
images: ghcr.io/${{ github.repository }}
36-
tags: type=sha,suffix=-build-${{ github.run_id }}_${{ github.run_attempt }}
34+
tags: | # sorted most to least specific
35+
type=sha,suffix=-build-${{ github.run_id }}_${{ github.run_attempt }}
36+
type=sha
37+
type=ref,event=branch
38+
type=raw,value=latest,enable={{is_default_branch}}
39+
40+
- id: id-image-tag
41+
name: Identify the build-specific image tag
42+
run: |
43+
echo build_image="$(echo $DOCKER_METADATA_OUTPUT_TAGS | tr ' ' '\n' | grep -E 'sha-\w+-build-${{ github.run_id }}_${{ github.run_attempt }}')" >> "$GITHUB_OUTPUT"
3744
38-
- name: Build and push the untested image
45+
- name: Build the untested image
3946
uses: docker/build-push-action@v6
4047
with:
4148
cache-from: type=gha
4249
cache-to: type=gha
4350
labels: ${{ steps.build-meta.outputs.labels }}
44-
platforms: linux/amd64,linux/arm64
45-
provenance: true
46-
push: true
47-
sbom: true
51+
outputs: type=docker,dest=${{ runner.temp }}/${{ env.IMAGE_ARTIFACT_NAME }}
52+
push: false
4853
tags: ${{ steps.build-meta.outputs.tags }}
4954

55+
- name: Upload untested image as an artifact
56+
uses: actions/upload-artifact@v4
57+
with:
58+
name: ${{ env.IMAGE_ARTIFACT_NAME }}
59+
path: ${{ runner.temp }}/${{ env.IMAGE_ARTIFACT_NAME }}
60+
5061
test:
5162
runs-on: ubuntu-latest
5263
needs:
5364
- build
5465
env:
55-
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
5666
DOCKER_APP_IMAGE: ${{ needs.build.outputs.build-image }}
5767
steps:
5868
- name: Checkout code
@@ -61,12 +71,16 @@ jobs:
6171
- name: Set up Docker Compose
6272
uses: docker/setup-compose-action@v1
6373

64-
- name: Login to GitHub Container Registry
65-
uses: docker/login-action@v3
74+
- name: Download untested image
75+
uses: actions/download-artifact@v4
6676
with:
67-
registry: ghcr.io
68-
username: ${{ github.actor }}
69-
password: ${{ secrets.GITHUB_TOKEN }}
77+
name: ${{ env.IMAGE_ARTIFACT_NAME }}
78+
path: ${{ runner.temp }}
79+
80+
- name: Load the image
81+
run: |
82+
docker image load --input "${{ runner.temp }}/$IMAGE_ARTIFACT_NAME"
83+
docker image ls --all
7084
7185
- name: Run the test script
7286
run: |
@@ -78,31 +92,30 @@ jobs:
7892
needs:
7993
- build
8094
- test
95+
env:
96+
DOCKER_APP_IMAGE: ${{ needs.build.outputs.build-image }}
8197
steps:
8298
- name: Checkout code
8399
uses: actions/checkout@v4
84100

101+
- name: Download tested image
102+
uses: actions/download-artifact@v4
103+
with:
104+
name: ${{ env.IMAGE_ARTIFACT_NAME }}
105+
path: ${{ runner.temp }}
106+
107+
- name: Load the image
108+
run: |
109+
docker image load --input "${{ runner.temp }}/$IMAGE_ARTIFACT_NAME"
110+
docker image ls --all
111+
85112
- name: Login to GitHub Container Registry
86113
uses: docker/login-action@v3
87114
with:
88115
registry: ghcr.io
89116
username: ${{ github.actor }}
90117
password: ${{ secrets.GITHUB_TOKEN }}
91118

92-
- name: Produce permanent image tags
93-
id: branch-meta
94-
uses: docker/metadata-action@v5
95-
with:
96-
images: ghcr.io/${{ github.repository }}
97-
tags: |
98-
type=sha
99-
type=ref,event=branch
100-
type=raw,value=latest,enable={{is_default_branch}}
101-
102-
- name: Retag and push the image
103-
uses: docker/build-push-action@v6
104-
with:
105-
cache-from: type=registry,ref=${{ needs.build.outputs.build-image }}
106-
labels: ${{ steps.branch-meta.outputs.labels }}
107-
push: true
108-
tags: ${{ steps.branch-meta.outputs.tags }}
119+
- name: Push the image
120+
run: |
121+
docker push --all-tags "$(echo $DOCKER_APP_IMAGE | cut -f1 -d:)"

0 commit comments

Comments
 (0)