Skip to content

Commit 368acdc

Browse files
committed
feat(ci): reusable wflow for build + matrix runners
1 parent 06bfaf0 commit 368acdc

File tree

4 files changed

+140
-158
lines changed

4 files changed

+140
-158
lines changed

.github/workflows/build-and-push.yaml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
name: Build and Push
2+
3+
on:
4+
workflow_call:
5+
6+
env:
7+
REGISTRY_IMAGE: ghcr.io/${{ github.repository }}
8+
9+
permissions:
10+
packages: write
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
platform:
19+
- linux/amd64
20+
- linux/arm64
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v4
24+
25+
- name: Prepare
26+
run: |
27+
platform=${{ matrix.platform }}
28+
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
29+
30+
- name: Get Build timestamp and branch name
31+
run: |
32+
echo "BUILD_TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
33+
echo "VERSION=$( echo ${{ github.head_ref || github.ref_name }} | tr '/' '-' )" >> $GITHUB_ENV
34+
35+
- name: Docker tags & labels
36+
id: meta
37+
uses: docker/metadata-action@v5
38+
with:
39+
images: ${{ env.REGISTRY_IMAGE }}
40+
# generate Docker tags:
41+
# - type=raw,VERSION -> branch name
42+
# - type=ref,event=tag -> tag name
43+
# - type=sha,format=long,prefix= -> commit sha
44+
tags: |
45+
type=raw,${{ env.VERSION }}
46+
type=ref,event=tag
47+
type=sha,format=long,prefix=
48+
49+
- name: Set up QEMU
50+
uses: docker/setup-qemu-action@v3
51+
52+
- name: Set up Docker Buildx
53+
uses: docker/setup-buildx-action@v3
54+
55+
- name: Login to GHCR
56+
uses: docker/login-action@v3
57+
with:
58+
registry: ghcr.io
59+
username: ${{ github.repository_owner }}
60+
password: ${{ secrets.GITHUB_TOKEN }}
61+
62+
- name: Build and push by digest
63+
id: build
64+
uses: docker/build-push-action@v6
65+
with:
66+
push: true
67+
provenance: false
68+
platforms: ${{ matrix.platform }}
69+
build-args: |
70+
VERSION=${{ env.VERSION }}
71+
BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }}
72+
COMMIT_HASH=${{ github.sha }}
73+
tags: ${{ steps.meta.outputs.tags }}
74+
labels: ${{ steps.meta.outputs.labels }}
75+
cache-from: type=gha
76+
cache-to: type=gha,mode=max
77+
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
78+
79+
- name: Export digest
80+
run: |
81+
mkdir -p /tmp/digests
82+
digest="${{ steps.build.outputs.digest }}"
83+
touch "/tmp/digests/${digest#sha256:}"
84+
85+
- name: Upload digest
86+
uses: actions/upload-artifact@v4
87+
with:
88+
name: digests-${{ env.PLATFORM_PAIR }}
89+
path: /tmp/digests/*
90+
if-no-files-found: error
91+
retention-days: 1
92+
93+
merge:
94+
runs-on: ubuntu-latest
95+
needs:
96+
- build
97+
steps:
98+
- name: Download digests
99+
uses: actions/download-artifact@v4
100+
with:
101+
path: /tmp/digests
102+
pattern: digests-*
103+
merge-multiple: true
104+
105+
- name: Set up Docker Buildx
106+
uses: docker/setup-buildx-action@v3
107+
108+
- name: Docker tags & labels
109+
id: meta
110+
uses: docker/metadata-action@v5
111+
with:
112+
images: ${{ env.REGISTRY_IMAGE }}
113+
# generate Docker tags:
114+
# - type=raw,VERSION -> branch name
115+
# - type=ref,event=tag -> tag name
116+
# - type=sha,format=long,prefix= -> commit sha
117+
tags: |
118+
type=raw,${{ env.VERSION }}
119+
type=ref,event=tag
120+
type=sha,format=long,prefix=
121+
122+
- name: Login to GHCR
123+
uses: docker/login-action@v3
124+
with:
125+
registry: ghcr.io
126+
username: ${{ github.repository_owner }}
127+
password: ${{ secrets.GITHUB_TOKEN }}
128+
129+
- name: Create manifest list and push
130+
working-directory: /tmp/digests
131+
run: |
132+
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
133+
$(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' *)
134+
135+
- name: Inspect image
136+
run: |
137+
docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}

.github/workflows/main.yaml

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ permissions:
1010

1111
env:
1212
GO_VERSION: 1.22
13-
BUILD_PLATFORMS: linux/amd64,linux/arm64
1413

1514
jobs:
1615
unit-tests:
@@ -69,55 +68,4 @@ jobs:
6968
args: --timeout=5m
7069

7170
build-and-push:
72-
name: Build & Push
73-
runs-on: ubuntu-latest
74-
steps:
75-
- name: Checkout
76-
uses: actions/checkout@v4
77-
78-
- name: Get Build timestamp and branch name
79-
run: |
80-
echo "BUILD_TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
81-
echo "VERSION=$( echo ${{ github.head_ref || github.ref_name }} | tr '/' '-' )" >> $GITHUB_ENV
82-
83-
- name: Docker tags & labels
84-
id: meta
85-
uses: docker/metadata-action@v4
86-
with:
87-
images: ghcr.io/${{ github.repository }}
88-
# generate Docker tags:
89-
# - type=raw,VERSION -> branch name
90-
# - type=ref,event=tag -> tag name
91-
# - type=sha,format=long,prefix= -> commit sha
92-
tags: |
93-
type=raw,${{ env.VERSION }}
94-
type=ref,event=tag
95-
type=sha,format=long,prefix=
96-
97-
- name: Set up QEMU
98-
uses: docker/setup-qemu-action@v2
99-
100-
- name: Set up Docker Buildx
101-
uses: docker/setup-buildx-action@v2
102-
103-
- name: Login to GHCR
104-
uses: docker/login-action@v2
105-
with:
106-
registry: ghcr.io
107-
username: ${{ github.repository_owner }}
108-
password: ${{ secrets.GITHUB_TOKEN }}
109-
110-
- name: Build and push
111-
uses: docker/build-push-action@v4
112-
with:
113-
push: true
114-
provenance: false
115-
platforms: ${{ env.BUILD_PLATFORMS }}
116-
build-args: |
117-
VERSION=${{ env.VERSION }}
118-
BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }}
119-
COMMIT_HASH=${{ github.sha }}
120-
tags: ${{ steps.meta.outputs.tags }}
121-
labels: ${{ steps.meta.outputs.labels }}
122-
cache-from: type=gha
123-
cache-to: type=gha,mode=max
71+
uses: ./.github/workflows/build-and-push.yaml

.github/workflows/pr.yaml

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77

88
env:
99
GO_VERSION: 1.22
10-
BUILD_PLATFORMS: linux/amd64,linux/arm64
1110

1211
jobs:
1312
unit-tests:
@@ -66,54 +65,4 @@ jobs:
6665
args: --timeout=5m
6766

6867
build-and-push:
69-
name: Build & Push
70-
runs-on: ubuntu-latest
71-
steps:
72-
- name: Checkout
73-
uses: actions/checkout@v4
74-
75-
- name: Get Build timestamp and branch name
76-
run: |
77-
echo "BUILD_TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
78-
echo "VERSION=$( echo ${{ github.head_ref || github.ref_name }} | tr '/' '-' )" >> $GITHUB_ENV
79-
80-
- name: Docker tags & labels
81-
id: meta
82-
uses: docker/metadata-action@v4
83-
with:
84-
images: ghcr.io/${{ github.repository }}
85-
# generate Docker tags:
86-
# - type=raw,VERSION -> branch name
87-
# - type=ref,event=tag -> tag name
88-
# - type=sha,format=long,prefix= -> commit sha
89-
tags: |
90-
type=raw,${{ env.VERSION }}
91-
type=ref,event=tag
92-
type=sha,format=long,prefix=
93-
94-
- name: Set up QEMU
95-
uses: docker/setup-qemu-action@v2
96-
97-
- name: Set up Docker Buildx
98-
uses: docker/setup-buildx-action@v2
99-
100-
- name: Login to GHCR
101-
uses: docker/login-action@v2
102-
with:
103-
registry: ghcr.io
104-
username: ${{ github.repository_owner }}
105-
password: ${{ secrets.GITHUB_TOKEN }}
106-
107-
- name: Build and push
108-
uses: docker/build-push-action@v4
109-
with:
110-
provenance: false
111-
platforms: ${{ env.BUILD_PLATFORMS }}
112-
build-args: |
113-
VERSION=${{ env.VERSION }}
114-
BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }}
115-
COMMIT_HASH=${{ github.sha }}
116-
tags: ${{ steps.meta.outputs.tags }}
117-
labels: ${{ steps.meta.outputs.labels }}
118-
cache-from: type=gha
119-
cache-to: type=gha,mode=max
68+
uses: ./.github/workflows/build-and-push.yaml

.github/workflows/release.yaml

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77

88
env:
99
GO_VERSION: 1.22
10-
BUILD_PLATFORMS: linux/amd64,linux/arm64
1110

1211
permissions:
1312
contents: write
@@ -77,55 +76,4 @@ jobs:
7776
push_options: --force
7877

7978
build-and-push:
80-
name: Build & Push
81-
runs-on: ubuntu-latest
82-
steps:
83-
- name: Checkout
84-
uses: actions/checkout@v4
85-
86-
- name: Get Build timestamp and branch name
87-
run: |
88-
echo "BUILD_TIMESTAMP=$(date +'%s')" >> $GITHUB_ENV
89-
echo "VERSION=$( echo ${{ github.head_ref || github.ref_name }} | tr '/' '-' )" >> $GITHUB_ENV
90-
91-
- name: Docker tags & labels
92-
id: meta
93-
uses: docker/metadata-action@v4
94-
with:
95-
images: ghcr.io/${{ github.repository }}
96-
# generate Docker tags:
97-
# - type=raw,VERSION -> branch name
98-
# - type=ref,event=tag -> tag name
99-
# - type=sha,format=long,prefix= -> commit sha
100-
tags: |
101-
type=raw,${{ env.VERSION }}
102-
type=ref,event=tag
103-
type=sha,format=long,prefix=
104-
105-
- name: Set up QEMU
106-
uses: docker/setup-qemu-action@v2
107-
108-
- name: Set up Docker Buildx
109-
uses: docker/setup-buildx-action@v2
110-
111-
- name: Login to GHCR
112-
uses: docker/login-action@v2
113-
with:
114-
registry: ghcr.io
115-
username: ${{ github.repository_owner }}
116-
password: ${{ secrets.GITHUB_TOKEN }}
117-
118-
- name: Build and push
119-
uses: docker/build-push-action@v4
120-
with:
121-
push: true
122-
provenance: false
123-
platforms: ${{ env.BUILD_PLATFORMS }}
124-
build-args: |
125-
VERSION=${{ env.VERSION }}
126-
BUILD_TIMESTAMP=${{ env.BUILD_TIMESTAMP }}
127-
COMMIT_HASH=${{ github.sha }}
128-
tags: ${{ steps.meta.outputs.tags }}
129-
labels: ${{ steps.meta.outputs.labels }}
130-
cache-from: type=gha
131-
cache-to: type=gha,mode=max
79+
uses: ./.github/workflows/build-and-push.yaml

0 commit comments

Comments
 (0)