Skip to content

Commit ab9783f

Browse files
committed
feat(docker-images): add cache-registry and buildkit configuration support
Add new inputs to support: - cache-registry: separate registry for Docker build cache - cache-registry-username/password: credentials for cache registry - buildkitd-config-inline: custom BuildKit daemon configuration When cache-registry is specified, the cache image path is automatically constructed using this registry instead of the main oci-registry. This allows using a local/private registry for build cache while pushing final images to a different registry (e.g., ghcr.io).
1 parent d04604b commit ab9783f

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

.github/workflows/docker-build-images.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,28 @@ on: # yamllint disable-line rule:truthy
9393
default: "gha"
9494
type: string
9595
required: false
96+
buildkitd-config-inline:
97+
description: |
98+
Inline BuildKit daemon configuration.
99+
See https://github.com/docker/setup-buildx-action#inputs.
100+
Example for insecure registry:
101+
[registry."my-registry.local:5000"]
102+
http = true
103+
insecure = true
104+
type: string
105+
required: false
106+
cache-registry:
107+
description: |
108+
Optional separate registry for Docker build cache.
109+
Use this when cache is stored on a different registry than the final image.
110+
type: string
111+
required: false
112+
cache-registry-username:
113+
description: |
114+
Username for the cache registry.
115+
Required if cache-registry is set and requires authentication.
116+
type: string
117+
required: false
96118
sign:
97119
description: |
98120
Sign built images.
@@ -116,6 +138,11 @@ on: # yamllint disable-line rule:truthy
116138
GitHub App private key to generate GitHub token to be passed as build secret env.
117139
See https://github.com/actions/create-github-app-token.
118140
required: false
141+
cache-registry-password:
142+
description: |
143+
Password for the cache registry.
144+
Required if cache-registry is set and requires authentication.
145+
required: false
119146
outputs:
120147
built-images:
121148
description: |
@@ -414,6 +441,10 @@ jobs:
414441
secret-envs: ${{ steps.prepare-secret-envs.outputs.secret-envs }}
415442
secrets: ${{ secrets.build-secrets }}
416443
cache-type: ${{ inputs.cache-type }}
444+
cache-registry: ${{ inputs.cache-registry }}
445+
cache-registry-username: ${{ inputs.cache-registry-username }}
446+
cache-registry-password: ${{ secrets.cache-registry-password }}
447+
buildkitd-config-inline: ${{ inputs.buildkitd-config-inline }}
417448
multi-platform: ${{ matrix.image.multi-platform }}
418449

419450
# FIXME: Set built images infos in file to be uploaded as artifacts, because github action does not handle job outputs for matrix

actions/docker/build-image/action.yml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,31 @@ inputs:
8787
See https://docs.docker.com/build/cache/backends.
8888
default: "gha"
8989
required: false
90+
cache-registry:
91+
description: |
92+
Optional separate registry for Docker build cache.
93+
Use this when cache is stored on a different registry than the final image.
94+
If not set, cache operations use the main oci-registry.
95+
required: false
96+
cache-registry-username:
97+
description: |
98+
Username for the cache registry.
99+
Required if cache-registry is set and requires authentication.
100+
required: false
101+
cache-registry-password:
102+
description: |
103+
Password for the cache registry.
104+
Required if cache-registry is set and requires authentication.
105+
required: false
106+
buildkitd-config-inline:
107+
description: |
108+
Inline BuildKit daemon configuration.
109+
See https://github.com/docker/setup-buildx-action#inputs.
110+
Example for insecure registry:
111+
[registry."my-registry.local:5000"]
112+
http = true
113+
insecure = true
114+
required: false
90115
multi-platform:
91116
description: |
92117
Whether this build participates in a multi-platform image publication.
@@ -174,7 +199,19 @@ runs:
174199
175200
const cacheType = `${{ inputs.cache-type }}`.trim();
176201
const metadataImage = `${{ steps.metadata.outputs.image }}`;
177-
const cacheImage = cacheType === 'registry' ? `${metadataImage}/cache` : metadataImage;
202+
const cacheRegistry = `${{ inputs.cache-registry }}`.trim();
203+
204+
let cacheImage;
205+
if (cacheRegistry) {
206+
// Use separate cache registry: replace the registry part of the image
207+
const imageParts = metadataImage.split('/');
208+
// Remove the original registry (first part) and join with cache registry
209+
imageParts.shift();
210+
cacheImage = `${cacheRegistry}/${imageParts.join('/')}/cache`;
211+
} else {
212+
// Use main registry for cache
213+
cacheImage = cacheType === 'registry' ? `${metadataImage}/cache` : metadataImage;
214+
}
178215
core.setOutput('cache-image', cacheImage);
179216
180217
try {
@@ -248,6 +285,7 @@ runs:
248285
# FIXME: upgrade version when available (https://hub.docker.com/r/moby/buildkit)
249286
driver-opts: |
250287
image=moby/buildkit:v0.27.0
288+
buildkitd-config-inline: ${{ inputs.buildkitd-config-inline }}
251289

252290
# Caching setup
253291
- id: cache-arguments
@@ -278,6 +316,13 @@ runs:
278316
registry: ${{ inputs.oci-registry }}
279317
username: ${{ inputs.oci-registry-username }}
280318
password: ${{ inputs.oci-registry-password }}
319+
320+
- uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
321+
if: inputs.cache-registry
322+
with:
323+
registry: ${{ inputs.cache-registry }}
324+
username: ${{ inputs.cache-registry-username }}
325+
password: ${{ inputs.cache-registry-password }}
281326
# jscpd:ignore-end
282327

283328
- id: build

0 commit comments

Comments
 (0)