Skip to content

Commit cc21b30

Browse files
committed
Change flow to use docker-bake
1 parent 1d56601 commit cc21b30

26 files changed

+2453
-447
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Docker Bake Composite Action
2+
3+
Builds per-platform images from `templates.yml` using `docker buildx bake`, uploads the resulting metadata, and exposes the release identifiers for follow-up jobs.
4+
5+
## What It Does
6+
- Detects (or accepts) the target platform and computes bake variables for the requested `family` and `distro`.
7+
- Authenticates to Docker Hub and/or GHCR when credentials are supplied.
8+
- Runs `docker/bake-action@v6` to build or push the image targets.
9+
- Persists the bake metadata as an artifact so the merge job can create multi-arch manifests.
10+
11+
## Inputs
12+
| Name | Required | Description |
13+
| --- | --- | --- |
14+
| `family` || Repository folder that defines the image family. |
15+
| `distro` || Distro/release name (used for Dockerfile and tags). |
16+
| `platform` | | Override build platform (`os/arch[/variant]`). Defaults to daemon platform. |
17+
| `docker-username` / `docker-password` | | Docker Hub credentials used for `docker login`. |
18+
| `ghcr-username` / `ghcr-password` | | GHCR credentials used for `docker login`. |
19+
| `push` | | Set to `false` to skip pushing digests (defaults to `true`). |
20+
21+
## Outputs
22+
| Name | Description |
23+
| --- | --- |
24+
| `platform` | Canonical build platform used for the bake. |
25+
| `group` | Bake target group (`family-distro-platform`). |
26+
| `release` | Release identifier (`family-distro`). |
27+
| `stage-targets` | JSON array of bake targets that were built. |
28+
| `metadata-path` | Path on disk to the saved bake metadata JSON. |
29+
30+
## Usage
31+
```yaml
32+
- name: Build ROS2 Rolling
33+
uses: ./.github/actions/docker-bake
34+
with:
35+
family: ros2
36+
distro: rolling
37+
platform: linux/amd64
38+
ghcr-username: ${{ github.repository_owner }}
39+
ghcr-password: ${{ secrets.GITHUB_TOKEN }}
40+
push: ${{ github.ref == 'refs/heads/main' }}
41+
```
42+
43+
The uploaded `bake-metadata-<group>` artifact and the `metadata-path` output can be consumed by the merge action to publish multi-architecture manifests.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
name: "Build & Push Docker images"
2+
description: "Build per-platform images from templates.yml with docker bake and push-by-digest artifacts."
3+
4+
inputs:
5+
family:
6+
description: "Image family (repo folder name)."
7+
required: true
8+
distro:
9+
description: "Image distro (used in Dockerfile name and tags)."
10+
required: true
11+
platform:
12+
description: "Optional os/arch[/variant]; defaults to the native daemon platform."
13+
required: false
14+
default: ""
15+
docker-username:
16+
description: "dockerhub username"
17+
default: ""
18+
required: false
19+
docker-password:
20+
description: "dockerhub password"
21+
required: false
22+
ghcr-username:
23+
description: "ghcr username"
24+
default: ""
25+
required: false
26+
ghcr-password:
27+
description: "ghcr password"
28+
default: ""
29+
required: false
30+
push:
31+
description: "Push digests to repo"
32+
default: "true"
33+
required: false
34+
35+
outputs:
36+
platform:
37+
description: "Canonical build platform (os/arch[/variant])."
38+
value: ${{ steps.detect.outputs.platform }}
39+
group:
40+
description: "Bake group (family-distro-platform)."
41+
value: ${{ steps.gen.outputs.group }}
42+
release:
43+
description: "Release identifier (family-distro)."
44+
value: ${{ steps.gen.outputs.release }}
45+
stage-targets:
46+
description: "JSON array of Bake targets built in this run."
47+
value: ${{ steps.gen.outputs.stage_targets }}
48+
metadata-path:
49+
description: "Path to the stored bake metadata JSON."
50+
value: ${{ steps.persist.outputs.metadata_path }}
51+
52+
runs:
53+
using: "composite"
54+
steps:
55+
- name: Set up Docker Buildx
56+
uses: docker/setup-buildx-action@v3
57+
58+
- name: Login to Docker Hub
59+
if: ${{ inputs.docker-password }}
60+
uses: docker/login-action@v3
61+
with:
62+
username: ${{ inputs.docker-username }}
63+
password: ${{ inputs.docker-password }}
64+
65+
- name: Log in to GHCR
66+
if: ${{ inputs.ghcr-password }}
67+
uses: docker/login-action@v3
68+
with:
69+
registry: ghcr.io
70+
username: ${{ inputs.ghcr-username }}
71+
password: ${{ inputs.ghcr-password }}
72+
73+
- name: Set up Python
74+
uses: actions/setup-python@v6
75+
with:
76+
python-version: "3.x"
77+
cache: "pip"
78+
79+
- name: Install dependencies
80+
shell: bash
81+
run: pip install -r "${{ github.action_path }}/requirements.txt"
82+
83+
- name: Detect build platform
84+
id: detect
85+
shell: bash
86+
run: |
87+
set -euo pipefail
88+
plat="${{ inputs.platform }}"
89+
if [[ -z "$plat" ]]; then
90+
plat="$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}')"
91+
echo "Detected platform: $plat"
92+
fi
93+
echo "platform=$plat" >> "$GITHUB_OUTPUT"
94+
key="${plat//\//-}"
95+
echo "platform_key=$key" >> "$GITHUB_OUTPUT"
96+
97+
- name: Compute bake variables
98+
id: gen
99+
shell: bash
100+
run: |
101+
set -euo pipefail
102+
cmd=(python .github/actions/docker-bake/get_variables.py
103+
--family "${{ inputs.family }}"
104+
--distro "${{ inputs.distro }}"
105+
--platform "${{ steps.detect.outputs.platform }}"
106+
${{ inputs.ghcr-password && format('--ghcr-username "{0}"', inputs.ghcr-username) || '' }}
107+
${{ inputs.docker-password && format('--docker-username "{0}"', inputs.docker-username) || '' }}
108+
--digest
109+
)
110+
output=$("${cmd[@]}")
111+
echo "$output"
112+
113+
- name: Bake
114+
id: bake
115+
uses: docker/bake-action@v6
116+
with:
117+
files: docker-bake.hcl
118+
targets: ${{ steps.gen.outputs.group }}
119+
push: ${{ inputs.push }}
120+
set: |
121+
${{ steps.gen.outputs.release }}-*.platform=${{ steps.detect.outputs.platform }}
122+
*.cache-to=type=gha,mode=max,scope=${{ steps.gen.outputs.group }}
123+
*.cache-from=type=gha,scope=${{ steps.gen.outputs.group }}
124+
${{ steps.gen.outputs.set_lines }}
125+
126+
- name: Persist Bake metadata for merge
127+
id: persist
128+
shell: bash
129+
run: |
130+
set -euo pipefail
131+
meta_dir="${{ github.workspace }}/.tmp"
132+
mkdir -p "$meta_dir"
133+
meta_file="bake-metadata-${{ inputs.family }}-${{ inputs.distro }}-${{ steps.detect.outputs.platform_key }}.json"
134+
metadata='${{ steps.bake.outputs.metadata }}'
135+
printf '%s' "$metadata" > "$meta_dir/$meta_file"
136+
echo "metadata_path=$meta_dir/$meta_file" >> "$GITHUB_OUTPUT"
137+
echo "Saved $meta_file ($(wc -c < "$meta_dir/$meta_file") bytes)"
138+
139+
- uses: actions/upload-artifact@v4
140+
with:
141+
name: bake-metadata-${{ steps.gen.outputs.group }}
142+
path: ${{ steps.persist.outputs.metadata_path }}
143+
144+
- name: Summary
145+
if: always()
146+
shell: bash
147+
run: |
148+
{
149+
echo "### Bake summary"
150+
echo "- Family: \`${{ inputs.family }}\`"
151+
echo "- Distro: \`${{ inputs.distro }}\`"
152+
echo "- Platform: \`${{ steps.detect.outputs.platform }}\`"
153+
echo "- Group: \`${{ steps.gen.outputs.group }}\`"
154+
echo "- Targets: \`${{ steps.gen.outputs.stage_targets }}\`"
155+
echo "- Artifact: \`bake-metadata-${{ steps.gen.outputs.group }}\`"
156+
} >> "$GITHUB_STEP_SUMMARY"

0 commit comments

Comments
 (0)