Skip to content

Commit 1a0213d

Browse files
committed
add cache-scope input
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent cef0e41 commit 1a0213d

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

.github/workflows/bake.yml

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ on:
3232
description: "Enable cache to GitHub Actions cache backend"
3333
required: false
3434
default: false
35+
cache-scope:
36+
type: string
37+
description: "Which scope cache object belongs to if cache enabled (default is target name)"
38+
required: false
3539
cache-mode:
3640
type: string
3741
description: "Cache layers to export if cache enabled (min or max)"
@@ -176,6 +180,7 @@ jobs:
176180
INPUT_TARGET: ${{ inputs.target }}
177181
INPUT_OUTPUT: ${{ inputs.output }}
178182
INPUT_CACHE: ${{ inputs.cache }}
183+
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
179184
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
180185
INPUT_META-IMAGES: ${{ inputs.meta-images }}
181186
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
@@ -200,6 +205,7 @@ jobs:
200205
const inpTarget = core.getInput('target');
201206
const inpOutput = core.getInput('output');
202207
const inpCache = core.getBooleanInput('cache');
208+
const inpCacheScope = core.getInput('cache-scope');
203209
const inpCacheMode = core.getInput('cache-mode');
204210
const inpMetaImages = core.getMultilineInput('meta-images');
205211
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
@@ -221,6 +227,29 @@ jobs:
221227
core.setOutput('source', bakeSource);
222228
});
223229
230+
let target;
231+
await core.group(`Validating definition`, async () => {
232+
const bake = new Bake();
233+
const def = await bake.getDefinition({
234+
allow: inpBakeAllow,
235+
files: inpBakeFiles,
236+
overrides: inpBakeSet,
237+
sbom: inpBakeSbom,
238+
source: bakeSource,
239+
targets: [inpTarget],
240+
githubToken: inpGitHubToken
241+
});
242+
if (!def) {
243+
throw new Error('Bake definition not set');
244+
}
245+
const targets = Object.keys(def.target);
246+
if (targets.length > 1) {
247+
throw new Error(`Only one target can be built at once, found: ${targets.join(', ')}`);
248+
}
249+
target = targets[0];
250+
core.setOutput('target', target);
251+
});
252+
224253
let bakeFiles = inpBakeFiles;
225254
await core.group(`Set bake files`, async () => {
226255
if (bakeFiles.length === 0) {
@@ -261,34 +290,12 @@ jobs:
261290
await core.group(`Set bake overrides`, async () => {
262291
bakeOverrides.push('*.attest=type=provenance,mode=max,version=v1', '*.tags=');
263292
if (inpCache) {
264-
bakeOverrides.push(`*.cache-from=type=gha,scope=docker-github-builder`);
265-
bakeOverrides.push(`*.cache-to=type=gha,scope=docker-github-builder,mode=${inpCacheMode}`);
293+
bakeOverrides.push(`*.cache-from=type=gha,scope=${inpCacheScope || target}`);
294+
bakeOverrides.push(`*.cache-to=type=gha,scope=${inpCacheScope || target},mode=${inpCacheMode}`);
266295
}
267296
core.info(JSON.stringify(bakeOverrides, null, 2));
268297
core.setOutput('overrides', bakeOverrides.join(os.EOL));
269298
});
270-
271-
let def;
272-
await core.group(`Validating definition`, async () => {
273-
const bake = new Bake();
274-
def = await bake.getDefinition({
275-
allow: inpBakeAllow,
276-
files: bakeFiles,
277-
overrides: bakeOverrides,
278-
sbom: inpBakeSbom,
279-
source: bakeSource,
280-
targets: [inpTarget],
281-
githubToken: inpGitHubToken
282-
});
283-
if (!def) {
284-
throw new Error('Bake definition not set');
285-
}
286-
const targets = Object.keys(def.target);
287-
if (targets.length > 1) {
288-
throw new Error(`Only one target can be built at once, found: ${targets.join(', ')}`);
289-
}
290-
core.setOutput('target', targets[0]);
291-
});
292299
-
293300
name: Login to registry
294301
if: ${{ inputs.output == 'registry' }}

.github/workflows/build.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ on:
1818
description: "Enable cache to GitHub Actions cache backend"
1919
required: false
2020
default: false
21+
cache-scope:
22+
type: string
23+
description: "Which scope cache object belongs to if cache enabled (default is target name if set)"
24+
required: false
2125
cache-mode:
2226
type: string
2327
description: "Cache layers to export if cache enabled (min or max)"
@@ -159,6 +163,7 @@ jobs:
159163
env:
160164
INPUT_LOCAL-EXPORT-DIR: ${{ env.LOCAL_EXPORT_DIR }}
161165
INPUT_CACHE: ${{ inputs.cache }}
166+
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
162167
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
163168
INPUT_META-IMAGES: ${{ inputs.meta-images }}
164169
INPUT_BUILD-OUTPUT: ${{ inputs.output }}
@@ -173,6 +178,7 @@ jobs:
173178
script: |
174179
const inpLocalExportDir = core.getInput('local-export-dir');
175180
const inpCache = core.getBooleanInput('cache');
181+
const inpCacheScope = core.getInput('cache-scope');
176182
const inpCacheMode = core.getInput('cache-mode');
177183
const inpMetaImages = core.getMultilineInput('meta-images');
178184
const inpBuildOutput = core.getInput('build-output');
@@ -202,8 +208,8 @@ jobs:
202208
}
203209
204210
if (inpCache) {
205-
core.setOutput('cache-from', `type=gha,scope=docker-github-builder`);
206-
core.setOutput('cache-to', `type=gha,scope=docker-github-builder,mode=${inpCacheMode}`);
211+
core.setOutput('cache-from', `type=gha,scope=${inpCacheScope || inpBuildTarget || 'buildkit'}`);
212+
core.setOutput('cache-to', `type=gha,scope=${inpCacheScope || inpBuildTarget || 'buildkit'},mode=${inpCacheMode}`);
207213
}
208214
209215
if (inpSetMetaAnnotations && inpMetaAnnotations.length > 0) {

0 commit comments

Comments
 (0)