Skip to content

Commit 06ee766

Browse files
committed
distribute: gha cache
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 99087c3 commit 06ee766

2 files changed

Lines changed: 85 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,13 @@ jobs:
741741
push: false
742742
meta-image: user/app
743743
bake-files: ./test/config.hcl
744+
745+
distribute-cache:
746+
uses: ./.github/workflows/reusable-distribute.yml
747+
with:
748+
target: app-plus
749+
push: false
750+
cache: true
751+
cache-scope: app-plus-build
752+
meta-image: user/app
753+
bake-files: ./test/config.hcl

.github/workflows/reusable-distribute.yml

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ on:
2020
description: "Push image to registry"
2121
required: false
2222
default: false
23+
cache:
24+
type: boolean
25+
description: "Enable cache to GitHub Actions cache backend"
26+
required: false
27+
default: false
28+
cache-scope:
29+
type: string
30+
description: "Which scope cache object belongs to if cache enabled (default is target name)"
31+
required: false
32+
cache-mode:
33+
type: string
34+
description: "Cache layers to export if cache enabled (min or max)"
35+
required: false
36+
default: 'min'
2337
set-meta-annotations:
2438
type: boolean
2539
description: "Set metadata-action annotations"
@@ -258,10 +272,46 @@ jobs:
258272
core.setOutput('includes', JSON.stringify(includes));
259273
});
260274
275+
warmup:
276+
runs-on: ${{ inputs.runner == 'auto' && 'ubuntu-latest' || inputs.runner }}
277+
steps:
278+
-
279+
name: Set up QEMU
280+
uses: docker/setup-qemu-action@v3
281+
if: ${{ inputs.setup-qemu }}
282+
with:
283+
image: ${{ inputs.qemu-image }}
284+
-
285+
name: Set up Docker Buildx
286+
uses: docker/setup-buildx-action@v3
287+
with:
288+
version: ${{ inputs.buildx-version }}
289+
driver-opts: ${{ inputs.buildx-driver-opts }}
290+
buildkitd-flags: ${{ inputs.buildkitd-flags }}
291+
buildkitd-config: ${{ inputs.buildkitd-config }}
292+
buildkitd-config-inline: ${{ inputs.buildkitd-config-inline }}
293+
cache-binary: ${{ inputs.buildx-cache-binary }}
294+
-
295+
name: Warm up cache
296+
uses: docker/bake-action@v6
297+
if: ${{ inputs.cache }}
298+
with:
299+
source: ${{ inputs.bake-source }}
300+
files: ${{ inputs.bake-files }}
301+
targets: ${{ inputs.target }}
302+
set: |
303+
${{ inputs.bake-set }}
304+
*.platform=
305+
*.output=type=cacheonly
306+
*.cache-from=type=gha,scope=${{ inputs.cache-scope || inputs.target }}-warmup
307+
*.cache-to=type=gha,scope=${{ inputs.cache-scope || inputs.target }}-warmup,mode=${{ inputs.cache-mode }}
308+
github-token: ${{ secrets.github-token || github.token }}
309+
261310
build:
262311
runs-on: ${{ matrix.runner }}
263312
needs:
264313
- prepare
314+
- warmup
265315
outputs:
266316
# needs predefined outputs as we can't use dynamic ones atm: https://github.com/actions/runner/pull/2477
267317
# 100 is the maximum number of platforms supported by the matrix strategy
@@ -406,15 +456,19 @@ jobs:
406456
buildkitd-config-inline: ${{ inputs.buildkitd-config-inline }}
407457
cache-binary: ${{ inputs.buildx-cache-binary }}
408458
-
409-
name: Set bake files
410-
id: bake-files
459+
name: Set bake files and overrides
460+
id: bake-opts
411461
uses: actions/github-script@v7
412462
env:
463+
PLATFORM: ${{ matrix.platform }}
464+
INPUT_TARGET: ${{ inputs.target }}
465+
INPUT_CACHE: ${{ inputs.cache }}
466+
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
467+
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
413468
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
414469
INPUT_SET-META-LABELS: ${{ inputs.set-meta-labels }}
415470
INPUT_BAKE-FILES: ${{ inputs.bake-files }}
416471
with:
417-
result-encoding: string
418472
script: |
419473
await core.group(`Installing npm dependencies`, async () => {
420474
await exec.exec('npm', ['install',
@@ -425,6 +479,11 @@ jobs:
425479
const os = require('os');
426480
const { Util } = require('@docker/actions-toolkit/lib/util');
427481
482+
const platformPair = process.env.PLATFORM.replace(/\//g, '-');
483+
const inpTarget = core.getInput('target');
484+
const inpCache = core.getBooleanInput('cache');
485+
const inpCacheScope = core.getInput('cache-scope');
486+
const inpCacheMode = core.getInput('cache-mode');
428487
let bakeFiles = Util.getInputList('bake-files');
429488
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
430489
const inpSetMetaLabels = core.getBooleanInput('set-meta-labels');
@@ -441,16 +500,26 @@ jobs:
441500
bakeFiles.push(`cwd://${{ steps.meta.outputs.bake-file-labels }}`);
442501
}
443502
core.info(JSON.stringify(bakeFiles, null, 2));
503+
core.setOutput('files', bakeFiles.join(os.EOL));
444504
});
445505
446-
return bakeFiles.join(os.EOL);
506+
await core.group(`Set bake overrides`, async () => {
507+
let bakeOverrides = [];
508+
if (inpCache) {
509+
bakeOverrides.push(`*.cache-from=type=gha,scope=${inpCacheScope || inpTarget}-warmup`);
510+
bakeOverrides.push(`*.cache-from=type=gha,scope=${inpCacheScope || inpTarget}-${platformPair}`);
511+
bakeOverrides.push(`*.cache-to=type=gha,scope=${inpCacheScope || inpTarget}-${platformPair},mode=${inpCacheMode}`);
512+
}
513+
core.info(JSON.stringify(bakeOverrides, null, 2));
514+
core.setOutput('overrides', bakeOverrides.join(os.EOL));
515+
});
447516
-
448517
name: Build
449518
id: bake
450519
uses: docker/bake-action@v6
451520
with:
452521
source: ${{ inputs.bake-source }}
453-
files: ${{ steps.bake-files.outputs.result }}
522+
files: ${{ steps.bake-opts.outputs.files }}
454523
targets: ${{ inputs.target }}
455524
allow: ${{ inputs.bake-allow }}
456525
no-cache: ${{ inputs.bake-no-cache }}
@@ -459,6 +528,7 @@ jobs:
459528
sbom: ${{ inputs.bake-sbom }}
460529
set: |
461530
${{ inputs.bake-set }}
531+
${{ steps.bake-opts.outputs.overrides }}
462532
*.tags=
463533
*.platform=${{ matrix.platform }}
464534
*.output=type=image,"name=${{ inputs.meta-image }}",push-by-digest=true,name-canonical=true,push=${{ inputs.push }}

0 commit comments

Comments
 (0)