Skip to content

Commit e47f9cf

Browse files
Refactors build image identification
1 parent 049f36d commit e47f9cf

2 files changed

Lines changed: 71 additions & 37 deletions

File tree

.github/workflows/build.yml

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ name: Build / Test / Push
22

33
on:
44
push:
5-
workflow_call:
5+
branches:
6+
- '*'
67
workflow_dispatch:
78

8-
env:
9-
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
10-
DOCKER_METADATA_SET_OUTPUT_ENV: 'true'
11-
129
jobs:
1310
build:
1411
runs-on: ubuntu-latest
1512
outputs:
16-
image: ${{ steps.meta.outputs.tags }}
13+
build-image: ${{ steps.build-meta.outputs.tags }}
1714
steps:
1815
- name: Checkout code
1916
uses: actions/checkout@v4
@@ -31,20 +28,19 @@ jobs:
3128
username: ${{ github.actor }}
3229
password: ${{ secrets.GITHUB_TOKEN }}
3330

34-
- name: Produce image tag 'sha-*-pre'
35-
id: meta
31+
- name: Produce the build image tag
32+
id: build-meta
3633
uses: docker/metadata-action@v5
3734
with:
3835
images: ghcr.io/${{ github.repository }}
39-
tags: type=sha,suffix=-pre
36+
tags: type=sha,suffix=-build-${{ github.run_id }}_${{ github.run_attempt }}
4037

4138
- name: Build and push the untested image
42-
id: build
4339
uses: docker/build-push-action@v6
4440
with:
4541
push: true
46-
labels: ${{ steps.meta.outputs.labels }}
47-
tags: ${{ steps.meta.outputs.tags }}
42+
labels: ${{ steps.build-meta.outputs.labels }}
43+
tags: ${{ steps.build-meta.outputs.tags }}
4844
provenance: true
4945
cache-from: type=gha
5046
cache-to: type=gha
@@ -54,7 +50,7 @@ jobs:
5450
needs:
5551
- build
5652
env:
57-
DOCKER_APP_IMAGE: ${{ needs.build.outputs.image }}
53+
DOCKER_APP_IMAGE: ${{ needs.build.outputs.build-image }}
5854
steps:
5955
- name: Checkout code
6056
uses: actions/checkout@v4
@@ -74,10 +70,40 @@ jobs:
7470

7571
- name: Run the tests
7672
run: bin/test
73+
env:
74+
COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml
75+
76+
push:
77+
runs-on: ubuntu-latest
78+
needs:
79+
- build
80+
- test
81+
steps:
82+
- name: Checkout code
83+
uses: actions/checkout@v4
84+
85+
- name: Login to GitHub Container Registry
86+
uses: docker/login-action@v3
87+
with:
88+
registry: ghcr.io
89+
username: ${{ github.actor }}
90+
password: ${{ secrets.GITHUB_TOKEN }}
91+
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}}
77101
78-
# type=ref,event=branch
79-
# type=ref,event=tag
80-
# type=semver,pattern={{major}}
81-
# type=semver,pattern={{major}}.{{minor}}
82-
# type=semver,pattern={{version}}
83-
# type=raw,value=latest,enable={{is_default_branch}}
102+
- name: Retag and push the image
103+
uses: docker/build-push-action@v6
104+
with:
105+
push: true
106+
labels: ${{ steps.branch-meta.outputs.labels }}
107+
tags: ${{ steps.branch-meta.outputs.tags }}
108+
provenance: true
109+
cache-from: type=registry,ref=${{ needs.build.outputs.build-image }}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
name: Push Release Tags
22

33
on:
4-
workflow_call:
4+
push:
5+
tags:
6+
- '*'
57
workflow_dispatch:
68

79
jobs:
8-
tag:
10+
retag:
911
runs-on: ubuntu-latest
1012
steps:
1113
- name: Checkout code
1214
uses: actions/checkout@v4
1315

14-
- name: Output short SHA
15-
run: echo "GITHUB_SHORT_SHA=${GITHUB_SHA::7}" >> "$GITHUB_ENV"
16-
17-
- name: Output Docker repo to be pulled from
18-
run: echo "ghcr.io/${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]' | sed 's/^/DOCKER_REPO=/' >> "$GITHUB_ENV"
19-
20-
- name: Output Docker image to be pulled
21-
run: echo "DOCKER_IMAGE=${DOCKER_REPO}:sha-${GITHUB_SHORT_SHA}" >> "$GITHUB_ENV"
22-
2316
- name: Set up QEMU
2417
uses: docker/setup-qemu-action@v3
2518

@@ -33,19 +26,34 @@ jobs:
3326
username: ${{ github.actor }}
3427
password: ${{ secrets.GITHUB_TOKEN }}
3528

36-
- name: Determine long-lived Docker image tags
37-
id: meta
29+
- name: Determine the sha-based image tag to retag
30+
id: get-base-image
3831
uses: docker/metadata-action@v5
3932
with:
4033
images: ${{ github.env.DOCKER_IMAGE }}
34+
tags: type=sha
35+
36+
- name: Verify that the image was previously built
37+
run: docker pull "$BASE_IMAGE"
38+
env:
39+
BASE_IMAGE: ${{ steps.get-base-image.outputs.tags }}
40+
41+
- name: Produce release tags
42+
id: meta
43+
uses: docker/metadata-action@v5
44+
with:
45+
images: ghcr.io/${{ github.repository }}
4146
tags: |
42-
type=sha
43-
type=ref,event=branch
4447
type=ref,event=tag
4548
type=semver,pattern={{major}}
4649
type=semver,pattern={{major}}.{{minor}}
4750
type=semver,pattern={{version}}
48-
type=raw,value=latest,enable={{is_default_branch}}
4951
50-
- name: Pull the image to be retagged
51-
run: docker pull "${DOCKER_IMAGE}"
52+
- name: Retag and push the image
53+
uses: docker/build-push-action@v6
54+
with:
55+
push: true
56+
labels: ${{ steps.meta.outputs.labels }}
57+
tags: ${{ steps.meta.outputs.tags }}
58+
provenance: true
59+
cache-from: type=registry,ref=${{ steps.get-base-image.outputs.tags }}

0 commit comments

Comments
 (0)