Skip to content

Commit 0cb3e97

Browse files
authored
Clarify github-owner instead of login in docker bake#745 (#745)
1 parent 6811ff6 commit 0cb3e97

5 files changed

Lines changed: 35 additions & 28 deletions

File tree

.github/actions/docker-bake/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Builds per-platform images from `templates.yml` using `docker buildx bake`, uplo
88
- Runs `docker/bake-action@v6` to build or push the image targets.
99
- Reuses BuildKit cache from GHCR when credentials are provided (while still priming the GitHub Actions cache as a fallback).
1010
- Registry cache export is enabled only when `push=true`.
11+
- GHA cache behavior is configurable via `gha-cache`:
12+
- `all`: enable GHA cache import and export
13+
- `from`: enable only GHA cache import
14+
- `none`: disable GHA cache import and export
1115
- GHCR cache/image namespace parts are normalized for valid registry references.
1216
- Persists the bake metadata as an artifact so the merge job can create multi-arch manifests.
1317

@@ -19,6 +23,7 @@ Builds per-platform images from `templates.yml` using `docker buildx bake`, uplo
1923
| `platform` | | Override build platform (`os/arch[/variant]`). Defaults to daemon platform. |
2024
| `docker-username` / `docker-password` | | Docker Hub credentials used for `docker login`. |
2125
| `ghcr-username` / `ghcr-password` | | GHCR credentials used for `docker login`. |
26+
| `gha-cache` | | `all` (default), `from`, or `none` for GHA cache mode. |
2227
| `push` | | Set to `false` to skip pushing digests (defaults to `true`). |
2328

2429
## Outputs
@@ -40,6 +45,7 @@ Builds per-platform images from `templates.yml` using `docker buildx bake`, uplo
4045
platform: linux/amd64
4146
ghcr-username: ${{ github.repository_owner }}
4247
ghcr-password: ${{ secrets.GITHUB_TOKEN }}
48+
gha-cache: all
4349
push: ${{ github.ref == 'refs/heads/main' }}
4450
```
4551

.github/actions/docker-bake/action.yml

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ inputs:
3131
description: "Push digests to registry"
3232
default: "true"
3333
required: false
34+
gha-cache:
35+
description: "GitHub Actions cache mode: all|none|from"
36+
default: "all"
37+
required: false
3438

3539
outputs:
3640
platform:
@@ -60,25 +64,11 @@ runs:
6064
shell: bash
6165
run: |
6266
set -euo pipefail
63-
ghcr_login_user="${{ inputs.ghcr-username }}"
64-
if [[ -z "$ghcr_login_user" ]]; then
65-
ghcr_login_user="${{ github.actor }}"
66-
fi
6767
68-
ghcr_owner="${{ inputs.ghcr-username }}"
69-
# Dependabot usernames include brackets (e.g. dependabot[bot]) which are
70-
# invalid in image/cache references; fall back to repo owner namespace.
71-
if [[ -z "$ghcr_owner" || "$ghcr_owner" == *"["* || "$ghcr_owner" == *"]"* ]]; then
72-
ghcr_owner="${{ github.repository_owner }}"
73-
fi
74-
ghcr_owner="$(printf '%s' "$ghcr_owner" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/^-+//; s/-+$//')"
75-
if [[ -z "$ghcr_owner" ]]; then
76-
ghcr_owner="$(printf '%s' "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9._-]+/-/g; s/^-+//; s/-+$//')"
77-
fi
68+
ghcr_owner="${{ github.repository_owner }}"
7869
7970
family="${{ inputs.family }}"
8071
distro="${{ inputs.distro }}"
81-
echo "ghcr_login_user=$ghcr_login_user" >> "$GITHUB_OUTPUT"
8272
echo "ghcr_owner=$ghcr_owner" >> "$GITHUB_OUTPUT"
8373
echo "family=${family,,}" >> "$GITHUB_OUTPUT"
8474
echo "distro=${distro,,}" >> "$GITHUB_OUTPUT"
@@ -91,11 +81,11 @@ runs:
9181
password: ${{ inputs.docker-password }}
9282

9383
- name: Log in to GHCR
94-
if: ${{ inputs.ghcr-password && steps.refs.outputs.ghcr_login_user }}
84+
if: ${{ inputs.ghcr-username && inputs.ghcr-password }}
9585
uses: docker/login-action@v3
9686
with:
9787
registry: ghcr.io
98-
username: ${{ steps.refs.outputs.ghcr_login_user }}
88+
username: ${{ inputs.ghcr-username }}
9989
password: ${{ inputs.ghcr-password }}
10090

10191
- name: Set up Python
@@ -131,7 +121,7 @@ runs:
131121
--family "${{ inputs.family }}"
132122
--distro "${{ inputs.distro }}"
133123
--platform "${{ steps.detect.outputs.platform }}"
134-
${{ inputs.ghcr-password && format('--ghcr-username "{0}"', steps.refs.outputs.ghcr_owner) || '' }}
124+
${{ inputs.ghcr-password && format('--ghcr-owner "{0}"', steps.refs.outputs.ghcr_owner) || '' }}
135125
${{ inputs.docker-password && format('--docker-username "{0}"', inputs.docker-username) || '' }}
136126
${{ inputs.push == 'true' && '--digest' || '' }}
137127
)
@@ -147,8 +137,8 @@ runs:
147137
push: ${{ inputs.push }}
148138
set: |
149139
${{ steps.gen.outputs.release }}-*.platform=${{ steps.detect.outputs.platform }}
150-
*.cache-to=type=gha,mode=max,scope=${{ steps.gen.outputs.group }}
151-
*.cache-from=type=gha,scope=${{ steps.gen.outputs.group }}
140+
${{ inputs.gha-cache == 'all' && (inputs.push == 'true' && format('*.cache-to=type=gha,mode=max,scope={0},ignore-error=true', steps.gen.outputs.group) || format('*.cache-to=type=gha,mode=min,scope={0},ignore-error=true', steps.gen.outputs.group)) || '' }}
141+
${{ (inputs.gha-cache == 'all' || inputs.gha-cache == 'from') && format('*.cache-from=type=gha,scope={0}', steps.gen.outputs.group) || '' }}
152142
${{ (inputs.push == 'true' && inputs.ghcr-password && steps.refs.outputs.ghcr_owner) && format('*.cache-to=type=registry,ref=ghcr.io/{0}/{1}:{2}-{3}-buildcache,mode=max', steps.refs.outputs.ghcr_owner, steps.refs.outputs.family, steps.refs.outputs.distro, steps.detect.outputs.platform_key) || '' }}
153143
${{ (inputs.ghcr-password && steps.refs.outputs.ghcr_owner) && format('*.cache-from=type=registry,ref=ghcr.io/{0}/{1}:{2}-{3}-buildcache', steps.refs.outputs.ghcr_owner, steps.refs.outputs.family, steps.refs.outputs.distro, steps.detect.outputs.platform_key) || '' }}
154144
${{ steps.gen.outputs.set_lines }}
@@ -161,8 +151,9 @@ runs:
161151
meta_dir="${{ github.workspace }}/.tmp"
162152
mkdir -p "$meta_dir"
163153
meta_file="bake-metadata-${{ inputs.family }}-${{ inputs.distro }}-${{ steps.detect.outputs.platform_key }}.json"
164-
metadata='${{ steps.bake.outputs.metadata }}'
165-
printf '%s' "$metadata" > "$meta_dir/$meta_file"
154+
cat > "$meta_dir/$meta_file" <<'__DOCKER_BAKE_METADATA__'
155+
${{ steps.bake.outputs.metadata }}
156+
__DOCKER_BAKE_METADATA__
166157
echo "metadata_path=$meta_dir/$meta_file" >> "$GITHUB_OUTPUT"
167158
echo "Saved $meta_file ($(wc -c < "$meta_dir/$meta_file") bytes)"
168159

.github/actions/docker-bake/get_variables.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
--family ros2 \
1212
--distro rolling \
1313
--platform linux/amd64 \
14-
--ghcr-username <user> \
14+
--ghcr-owner <owner> \
1515
--docker-username <user> \
1616
--digest
1717
@@ -142,7 +142,15 @@ def main() -> int:
142142
help="Docker platform os/arch[/variant] (e.g., linux/amd64).",
143143
)
144144
parser.add_argument(
145-
"--ghcr-username", default="", help="GHCR owner/org for final tags."
145+
"--ghcr-owner",
146+
dest="ghcr_owner",
147+
default="",
148+
help="GHCR owner/org for final tags.",
149+
)
150+
parser.add_argument(
151+
"--ghcr-username",
152+
dest="ghcr_owner",
153+
help=argparse.SUPPRESS,
146154
)
147155
parser.add_argument(
148156
"--docker-username",
@@ -187,7 +195,7 @@ def main() -> int:
187195
stage_targets: list[str] = []
188196
set_lines: list[str] = []
189197
normalized_family = args.family.strip().lower()
190-
normalized_ghcr_user = args.ghcr_username.strip().lower()
198+
normalized_ghcr_owner = args.ghcr_owner.strip().lower()
191199
normalized_docker_user = args.docker_username.strip().lower()
192200
for tgt in entry.get("targets", []):
193201
if not platforms_support(tgt.get("platforms", ""), platform):
@@ -198,9 +206,9 @@ def main() -> int:
198206
stage_targets.append(tname)
199207

200208
destinations: list[str] = []
201-
if normalized_ghcr_user:
209+
if normalized_ghcr_owner:
202210
destinations.append(
203-
f"ghcr.io/{normalized_ghcr_user}/{normalized_family}"
211+
f"ghcr.io/{normalized_ghcr_owner}/{normalized_family}"
204212
)
205213
if normalized_docker_user:
206214
destinations.append(

.github/actions/docker-bake/tests/test_get_variables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_main_emits_expected_outputs(self):
7575
"rolling",
7676
"--platform",
7777
"linux/amd64",
78-
"--ghcr-username",
78+
"--ghcr-owner",
7979
"GhUser",
8080
"--docker-username",
8181
"DockerUSER",

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ jobs:
127127
distro: ${{ matrix.distro }} # e.g. rolling
128128
ghcr-username: ${{ github.actor }} # optional
129129
ghcr-password: ${{ secrets.GITHUB_TOKEN }} # optional
130+
gha-cache: ${{ contains(matrix.distro, 'cuda') && 'from' || 'all' }}
130131
push: "false"
131132

132133
bake-build-arm64:
@@ -149,6 +150,7 @@ jobs:
149150
distro: ${{ matrix.distro }} # e.g. rolling
150151
ghcr-username: ${{ github.actor }} # optional
151152
ghcr-password: ${{ secrets.GITHUB_TOKEN }} # optional
153+
gha-cache: ${{ contains(matrix.distro, 'cuda') && 'from' || 'all' }}
152154
push: "false"
153155

154156
merge-manifests:

0 commit comments

Comments
 (0)