Skip to content

Commit 7f93389

Browse files
authored
Merge pull request #34 from wayofdev/feat/docker-bake
feat!: migrate to docker-bake.hcl approach
2 parents e8ae9cf + bf18890 commit 7f93389

File tree

6 files changed

+389
-91
lines changed

6 files changed

+389
-91
lines changed

.dive-ci.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
3+
rules:
4+
# If the efficiency is measured below X%, mark as failed.
5+
# Expressed as a ratio between 0-1.
6+
lowestEfficiency: 0.95
7+
8+
# If the amount of wasted space is at least X or larger than X, mark as failed.
9+
# Expressed in B, KB, MB, and GB.
10+
highestWastedBytes: 20MB
11+
12+
# If the amount of wasted space makes up for X% or more of the image, mark as failed.
13+
# Note: the base image layer is NOT included in the total image size.
14+
# Expressed as a ratio between 0-1; fails if the threshold is met or crossed.
15+
highestUserWastedPercent: 0.20
16+
17+
...

.github/workflows/build-latest.yml

-42
This file was deleted.

.github/workflows/build-release.yml

-49
This file was deleted.

.github/workflows/build.yml

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
3+
on: # yamllint disable-line rule:truthy
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
release:
9+
types:
10+
- released
11+
schedule:
12+
- cron: "30 10 * * *"
13+
14+
env:
15+
DOCKER_NAMESPACE: wayofdev/nginx
16+
GHCR_NAMESPACE: ghcr.io/wayofdev/docker-nginx
17+
18+
name: 🚀 Build docker images with latest tag
19+
20+
concurrency:
21+
group: "${{ github.workflow }}-${{ github.ref }}"
22+
cancel-in-progress: true
23+
24+
jobs:
25+
build:
26+
strategy:
27+
fail-fast: false
28+
matrix:
29+
os_name: ["alpine"]
30+
nginx_type: ["dev", "k8s"]
31+
builder: [{arch: "amd64", os: "ubuntu-latest"}, {arch: "arm64", os: "ubuntu-latest"}]
32+
runs-on: ${{ matrix.builder.os }}
33+
steps:
34+
35+
- name: 🌎 Set environment variables
36+
run: |
37+
tag="${{ matrix.nginx_type }}-${{ matrix.os_name }}-${{ matrix.builder.arch }}"
38+
target="nginx-${{ matrix.nginx_type }}-${{ matrix.os_name }}"
39+
echo "TARGET=${target}" >> "$GITHUB_ENV"
40+
echo "PLATFORM_CACHE_TAG=${tag}" >> "$GITHUB_ENV"
41+
42+
- name: 📦 Check out the codebase
43+
uses: actions/[email protected]
44+
45+
- name: 🤖 Generate dist files
46+
run: ansible-playbook src/playbook.yml -l ${{ matrix.nginx_type }}-${{ matrix.os_name }}
47+
48+
- name: 🖥️ Setup docker QEMU
49+
uses: docker/setup-qemu-action@v3
50+
with:
51+
platforms: linux/${{ matrix.builder.arch }}
52+
53+
- name: 🛠️ Set up Docker Buildx
54+
uses: docker/setup-buildx-action@v3
55+
with:
56+
platforms: linux/${{ matrix.builder.arch }}
57+
buildkitd-flags: "--debug"
58+
59+
- name: 🐳 Extract docker meta data
60+
id: meta
61+
uses: docker/metadata-action@v5
62+
with:
63+
images: |
64+
${{ env.DOCKER_NAMESPACE }}
65+
${{ env.GHCR_NAMESPACE }}
66+
tags: |
67+
type=raw,event=branch,value=latest
68+
type=ref,event=tag
69+
type=semver,pattern={{version}}
70+
type=semver,pattern={{major}}.{{minor}}
71+
flavor: |
72+
latest=false
73+
prefix=${{ matrix.nginx_type }}-${{ matrix.os_name }}-
74+
75+
- name: ⚙️ Rename meta bake definition file
76+
run: |
77+
mv "${{ steps.meta.outputs.bake-file }}" "/tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json"
78+
79+
- name: 📤 Upload meta bake definition
80+
uses: actions/upload-artifact@v4
81+
with:
82+
name: bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json
83+
path: /tmp/bake-meta-*.json
84+
if-no-files-found: error
85+
retention-days: 1
86+
87+
- name: 🔑 Login to docker-hub
88+
uses: docker/login-action@v3
89+
with:
90+
registry: docker.io
91+
username: ${{ secrets.DOCKER_USERNAME }}
92+
password: ${{ secrets.DOCKER_TOKEN }}
93+
94+
- name: 🔑 Login to GHCR
95+
uses: docker/login-action@v3
96+
with:
97+
registry: ghcr.io
98+
username: ${{ github.repository_owner }}
99+
password: ${{ secrets.GITHUB_TOKEN }}
100+
101+
- name: 🚀 Bake image and push to docker-hub and GHCR
102+
id: bake
103+
uses: docker/[email protected]
104+
with:
105+
targets: ${{ env.TARGET }}
106+
files: |
107+
./docker-bake.hcl
108+
/tmp/bake-meta-${{ env.PLATFORM_CACHE_TAG }}.json
109+
set: |
110+
*.tags=
111+
*.platform=linux/${{ matrix.builder.arch }}
112+
*.cache-from=type=gha,scope=build-${{ env.PLATFORM_CACHE_TAG }}
113+
*.cache-to=type=gha,scope=build-${{ env.PLATFORM_CACHE_TAG }}
114+
*.output=type=image,"name=${{ env.DOCKER_NAMESPACE }},${{ env.GHCR_NAMESPACE }}",push-by-digest=true,name-canonical=true,push=true
115+
116+
- name: 📥 Export digest
117+
run: |
118+
mkdir -p /tmp/digests
119+
echo "Bake Metadata: ${{ steps.bake.outputs.metadata }}"
120+
digest=$(echo '${{ steps.bake.outputs.metadata }}' | jq -r '.["${{ env.TARGET }}"]["containerimage.digest"]')
121+
if [[ -z "$digest" || "$digest" == "null" ]]; then
122+
echo "Digest not found."
123+
exit 1
124+
fi
125+
echo "Digest: $digest"
126+
touch "/tmp/digests/${digest#sha256:}"
127+
128+
- name: 📤 Upload digest
129+
uses: actions/upload-artifact@v4
130+
with:
131+
name: digests-${{ env.PLATFORM_CACHE_TAG }}
132+
path: /tmp/digests/*
133+
if-no-files-found: error
134+
retention-days: 1
135+
136+
merge:
137+
needs: build
138+
runs-on: ubuntu-latest
139+
steps:
140+
141+
- name: 📥 Download meta bake definitions
142+
uses: actions/download-artifact@v4
143+
with:
144+
pattern: bake-meta-*
145+
path: /tmp
146+
merge-multiple: true
147+
148+
- name: 📥 Download meta bake definitions
149+
uses: actions/download-artifact@v4
150+
with:
151+
pattern: digests-*
152+
path: /tmp/digests
153+
154+
- name: 🔑 Login to docker-hub
155+
uses: docker/login-action@v3
156+
with:
157+
username: ${{ secrets.DOCKER_USERNAME }}
158+
password: ${{ secrets.DOCKER_TOKEN }}
159+
160+
- name: 🔑 Login to GHCR
161+
uses: docker/login-action@v3
162+
with:
163+
registry: ghcr.io
164+
username: ${{ github.repository_owner }}
165+
password: ${{ secrets.GITHUB_TOKEN }}
166+
167+
- name: 🖥️ Setup docker QEMU
168+
uses: docker/setup-qemu-action@v3
169+
with:
170+
platforms: linux/amd64,linux/arm64
171+
172+
- name: 🛠️ Set up Docker Buildx
173+
uses: docker/setup-buildx-action@v3
174+
with:
175+
platforms: linux/amd64,linux/arm64
176+
buildkitd-flags: "--debug"
177+
178+
- name: 📦 Check out the codebase
179+
uses: actions/[email protected]
180+
181+
- name: 🚀 Create manifest list and push
182+
working-directory: /tmp
183+
run: |
184+
variants=($(ls bake-meta-*.json | sed -E 's/bake-meta-//; s/-amd64.json|-arm64.json//g' | sort -u))
185+
for variant in "${variants[@]}"; do
186+
187+
# Fetch digests for amd64 and arm64 architectures
188+
DIGEST_AMD64=$(basename $(ls /tmp/digests/digests-${variant}-amd64/*))
189+
DIGEST_ARM64=$(basename $(ls /tmp/digests/digests-${variant}-arm64/*))
190+
echo "Digest AMD64: $DIGEST_AMD64"
191+
echo "Digest ARM64: $DIGEST_ARM64"
192+
193+
# Create the manifest list for Docker Hub
194+
docker buildx imagetools create $(jq -cr ".target.\"docker-metadata-action\".tags | map(select(startswith(\"${DOCKER_NAMESPACE}\")) | \"-t \" + .) | join(\" \")" /tmp/bake-meta-${variant}-amd64.json) \
195+
"${DOCKER_NAMESPACE}@sha256:${DIGEST_AMD64}" \
196+
"${DOCKER_NAMESPACE}@sha256:${DIGEST_ARM64}"
197+
198+
# Create the manifest list for GHCR
199+
docker buildx imagetools create $(jq -cr ".target.\"docker-metadata-action\".tags | map(select(startswith(\"${GHCR_NAMESPACE}\")) | \"-t \" + .) | join(\" \")" /tmp/bake-meta-${variant}-amd64.json) \
200+
"${GHCR_NAMESPACE}@sha256:${DIGEST_AMD64}" \
201+
"${GHCR_NAMESPACE}@sha256:${DIGEST_ARM64}"
202+
done
203+
204+
...

0 commit comments

Comments
 (0)