Skip to content

Commit 15bd031

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

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

.github/workflows/.test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
id-token: write
2727
with:
2828
output: ${{ github.event_name != 'pull_request' && 'registry' || 'cacheonly' }}
29+
cache: true
2930
meta-images: |
3031
public.ecr.aws/q3b5f1u4/test-docker-action
3132
meta-tags: |
@@ -46,6 +47,8 @@ jobs:
4647
id-token: write
4748
with:
4849
output: ${{ github.event_name != 'pull_request' && 'registry' || 'cacheonly' }}
50+
cache: true
51+
cache-scope: build-aws
4952
meta-images: |
5053
public.ecr.aws/q3b5f1u4/test-docker-action
5154
meta-tags: |
@@ -230,6 +233,8 @@ jobs:
230233
context: test
231234
target: hello-cross
232235
output: ${{ github.event_name != 'pull_request' && 'registry' || 'cacheonly' }}
236+
cache: true
237+
cache-scope: bake-aws
233238
meta-images: |
234239
public.ecr.aws/q3b5f1u4/test-docker-action
235240
meta-tags: |
@@ -284,6 +289,7 @@ jobs:
284289
context: test
285290
target: hello-cross
286291
output: ${{ github.event_name != 'pull_request' && 'local' || 'cacheonly' }}
292+
cache: true
287293
artifact-name: bake-output
288294
bake-sbom: true
289295

.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)"
@@ -181,6 +185,7 @@ jobs:
181185
INPUT_TARGET: ${{ inputs.target }}
182186
INPUT_OUTPUT: ${{ inputs.output }}
183187
INPUT_CACHE: ${{ inputs.cache }}
188+
INPUT_CACHE-SCOPE: ${{ inputs.cache-scope }}
184189
INPUT_CACHE-MODE: ${{ inputs.cache-mode }}
185190
INPUT_META-IMAGES: ${{ inputs.meta-images }}
186191
INPUT_SET-META-ANNOTATIONS: ${{ inputs.set-meta-annotations }}
@@ -205,6 +210,7 @@ jobs:
205210
const inpTarget = core.getInput('target');
206211
const inpOutput = core.getInput('output');
207212
const inpCache = core.getBooleanInput('cache');
213+
const inpCacheScope = core.getInput('cache-scope');
208214
const inpCacheMode = core.getInput('cache-mode');
209215
const inpMetaImages = core.getMultilineInput('meta-images');
210216
const inpSetMetaAnnotations = core.getBooleanInput('set-meta-annotations');
@@ -226,6 +232,29 @@ jobs:
226232
core.setOutput('source', bakeSource);
227233
});
228234
235+
let target;
236+
await core.group(`Validating definition`, async () => {
237+
const bake = new Bake();
238+
const def = await bake.getDefinition({
239+
allow: inpBakeAllow,
240+
files: inpBakeFiles,
241+
overrides: inpBakeSet,
242+
sbom: inpBakeSbom,
243+
source: bakeSource,
244+
targets: [inpTarget],
245+
githubToken: inpGitHubToken
246+
});
247+
if (!def) {
248+
throw new Error('Bake definition not set');
249+
}
250+
const targets = Object.keys(def.target);
251+
if (targets.length > 1) {
252+
throw new Error(`Only one target can be built at once, found: ${targets.join(', ')}`);
253+
}
254+
target = targets[0];
255+
core.setOutput('target', target);
256+
});
257+
229258
let bakeFiles = inpBakeFiles;
230259
await core.group(`Set bake files`, async () => {
231260
if (bakeFiles.length === 0) {
@@ -266,34 +295,12 @@ jobs:
266295
await core.group(`Set bake overrides`, async () => {
267296
bakeOverrides.push('*.attest=type=provenance,mode=max,version=v1', '*.tags=');
268297
if (inpCache) {
269-
bakeOverrides.push(`*.cache-from=type=gha,scope=docker-github-builder`);
270-
bakeOverrides.push(`*.cache-to=type=gha,scope=docker-github-builder,mode=${inpCacheMode}`);
298+
bakeOverrides.push(`*.cache-from=type=gha,scope=${inpCacheScope || target}`);
299+
bakeOverrides.push(`*.cache-to=type=gha,scope=${inpCacheScope || target},mode=${inpCacheMode}`);
271300
}
272301
core.info(JSON.stringify(bakeOverrides, null, 2));
273302
core.setOutput('overrides', bakeOverrides.join(os.EOL));
274303
});
275-
276-
let def;
277-
await core.group(`Validating definition`, async () => {
278-
const bake = new Bake();
279-
def = await bake.getDefinition({
280-
allow: inpBakeAllow,
281-
files: bakeFiles,
282-
overrides: bakeOverrides,
283-
sbom: inpBakeSbom,
284-
source: bakeSource,
285-
targets: [inpTarget],
286-
githubToken: inpGitHubToken
287-
});
288-
if (!def) {
289-
throw new Error('Bake definition not set');
290-
}
291-
const targets = Object.keys(def.target);
292-
if (targets.length > 1) {
293-
throw new Error(`Only one target can be built at once, found: ${targets.join(', ')}`);
294-
}
295-
core.setOutput('target', targets[0]);
296-
});
297304
-
298305
name: Login to registry
299306
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)