Skip to content

Commit 4143b58

Browse files
ibalajiaruncrazy-max
authored andcommitted
Support to retain cache
Signed-off-by: Balaji Arun <[email protected]>
1 parent 3f1544e commit 4143b58

File tree

6 files changed

+88
-11
lines changed

6 files changed

+88
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ The following inputs can be used as `step.with` keys:
100100
| `append` | YAML | | [Append additional nodes](https://docs.docker.com/build/ci/github-actions/configure-builder/#append-additional-nodes-to-the-builder) to the builder |
101101
| `cache-binary` | Bool | `true` | Cache buildx binary to GitHub Actions cache backend |
102102
| `cleanup` | Bool | `true` | Cleanup temp files and remove builder at the end of a job |
103+
| `name` | String | Default Docker Context | Name of the builder to create or use. If a builder with this name already exists, it will be used instead of creating a new one. |
103104

104105
> [!IMPORTANT]
105106
> If you set the `buildkitd-flags` input, the default flags (`--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host`)

__tests__/context.test.ts

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('getCreateArgs', () => {
5656
['use', 'true'],
5757
['cache-binary', 'true'],
5858
['cleanup', 'true'],
59+
['keep-state', 'false']
5960
]),
6061
[
6162
'create',
@@ -74,6 +75,7 @@ describe('getCreateArgs', () => {
7475
['use', 'true'],
7576
['cache-binary', 'true'],
7677
['cleanup', 'true'],
78+
['keep-state', 'false']
7779
]),
7880
[
7981
'create',
@@ -92,6 +94,7 @@ describe('getCreateArgs', () => {
9294
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
9395
['cache-binary', 'true'],
9496
['cleanup', 'true'],
97+
['keep-state', 'false']
9598
]),
9699
[
97100
'create',
@@ -112,6 +115,7 @@ describe('getCreateArgs', () => {
112115
['use', 'true'],
113116
['cache-binary', 'true'],
114117
['cleanup', 'true'],
118+
['keep-state', 'false']
115119
]),
116120
[
117121
'create',
@@ -132,6 +136,7 @@ describe('getCreateArgs', () => {
132136
['use', 'true'],
133137
['cache-binary', 'true'],
134138
['cleanup', 'true'],
139+
['keep-state', 'false']
135140
]),
136141
[
137142
'create',
@@ -151,6 +156,7 @@ describe('getCreateArgs', () => {
151156
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
152157
['cache-binary', 'true'],
153158
['cleanup', 'true'],
159+
['keep-state', 'false'],
154160
]),
155161
[
156162
'create',
@@ -169,6 +175,7 @@ describe('getCreateArgs', () => {
169175
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
170176
['cache-binary', 'true'],
171177
['cleanup', 'true'],
178+
['keep-state', 'false'],
172179
]),
173180
[
174181
'create',
@@ -187,6 +194,7 @@ describe('getCreateArgs', () => {
187194
['driver', 'unknown'],
188195
['cache-binary', 'true'],
189196
['cleanup', 'true'],
197+
['keep-state', 'false'],
190198
]),
191199
[
192200
'create',
@@ -203,6 +211,7 @@ describe('getCreateArgs', () => {
203211
['buildkitd-config', path.join(fixturesDir, 'buildkitd.toml')],
204212
['cache-binary', 'true'],
205213
['cleanup', 'true'],
214+
['keep-state', 'false'],
206215
]),
207216
[
208217
'create',
@@ -221,6 +230,7 @@ describe('getCreateArgs', () => {
221230
['buildkitd-config-inline', 'debug = true'],
222231
['cache-binary', 'true'],
223232
['cleanup', 'true'],
233+
['keep-state', 'false'],
224234
]),
225235
[
226236
'create',
@@ -240,14 +250,53 @@ describe('getCreateArgs', () => {
240250
['buildkitd-flags', '--allow-insecure-entitlement network.host'],
241251
['cache-binary', 'true'],
242252
['cleanup', 'true'],
253+
['keep-state', 'false'],
243254
]),
244255
[
245256
'create',
246257
'--name', 'builder-9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d',
247258
'--driver', 'cloud',
248259
'--buildkitd-flags', '--allow-insecure-entitlement network.host',
249260
]
250-
]
261+
],
262+
[
263+
11,
264+
'v0.10.3',
265+
new Map<string, string>([
266+
['install', 'false'],
267+
['use', 'true'],
268+
['cleanup', 'true'],
269+
['cache-binary', 'true'],
270+
['keep-state', 'false'],
271+
['name', 'test-builder-name'],
272+
]),
273+
[
274+
'create',
275+
'--name', 'test-builder-name',
276+
'--driver', 'docker-container',
277+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
278+
'--use'
279+
]
280+
],
281+
[
282+
12,
283+
'v0.10.3',
284+
new Map<string, string>([
285+
['install', 'false'],
286+
['use', 'true'],
287+
['cleanup', 'true'],
288+
['cache-binary', 'true'],
289+
['keep-state', 'true'],
290+
['name', 'test-builder-name'],
291+
]),
292+
[
293+
'create',
294+
'--name', 'test-builder-name',
295+
'--driver', 'docker-container',
296+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
297+
'--use',
298+
]
299+
],
251300
])(
252301
'[%d] given buildx %s and %p as inputs, returns %p',
253302
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
@@ -285,6 +334,7 @@ describe('getAppendArgs', () => {
285334
['use', 'true'],
286335
['cache-binary', 'true'],
287336
['cleanup', 'true'],
337+
['keep-state', 'false']
288338
]),
289339
{
290340
"name": "aws_graviton2",
@@ -343,6 +393,7 @@ describe('getVersion', () => {
343393
['use', 'true'],
344394
['cache-binary', 'true'],
345395
['cleanup', 'true'],
396+
['keep-state', 'false']
346397
]),
347398
''
348399
],
@@ -354,7 +405,8 @@ describe('getVersion', () => {
354405
['install', 'false'],
355406
['use', 'true'],
356407
['cache-binary', 'true'],
357-
['cleanup', 'true']
408+
['cleanup', 'true'],
409+
['keep-state', 'false']
358410
]),
359411
'latest'
360412
],
@@ -366,7 +418,8 @@ describe('getVersion', () => {
366418
['install', 'false'],
367419
['use', 'true'],
368420
['cache-binary', 'true'],
369-
['cleanup', 'true']
421+
['cleanup', 'true'],
422+
['keep-state', 'false']
370423
]),
371424
'edge'
372425
],
@@ -378,7 +431,8 @@ describe('getVersion', () => {
378431
['install', 'false'],
379432
['use', 'true'],
380433
['cache-binary', 'true'],
381-
['cleanup', 'true']
434+
['cleanup', 'true'],
435+
['keep-state', 'false']
382436
]),
383437
'v0.19.2'
384438
],
@@ -391,7 +445,8 @@ describe('getVersion', () => {
391445
['install', 'false'],
392446
['use', 'true'],
393447
['cache-binary', 'true'],
394-
['cleanup', 'true']
448+
['cleanup', 'true'],
449+
['keep-state', 'false']
395450
]),
396451
'cloud:latest'
397452
],
@@ -404,7 +459,8 @@ describe('getVersion', () => {
404459
['install', 'false'],
405460
['use', 'true'],
406461
['cache-binary', 'true'],
407-
['cleanup', 'true']
462+
['cleanup', 'true'],
463+
['keep-state', 'false']
408464
]),
409465
'cloud:edge'
410466
],
@@ -417,6 +473,7 @@ describe('getVersion', () => {
417473
['use', 'true'],
418474
['cache-binary', 'true'],
419475
['cleanup', 'true'],
476+
['keep-state', 'false']
420477
]),
421478
'cloud:latest'
422479
],
@@ -430,6 +487,7 @@ describe('getVersion', () => {
430487
['use', 'true'],
431488
['cache-binary', 'true'],
432489
['cleanup', 'true'],
490+
['keep-state', 'false']
433491
]),
434492
'cloud:v0.11.2-desktop.2'
435493
],
@@ -442,6 +500,7 @@ describe('getVersion', () => {
442500
['use', 'true'],
443501
['cache-binary', 'true'],
444502
['cleanup', 'true'],
503+
['keep-state', 'false']
445504
]),
446505
'cloud:v0.11.2-desktop.2'
447506
],

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ inputs:
5050
cleanup:
5151
description: 'Cleanup temp files and remove builder at the end of a job'
5252
default: 'true'
53+
keep-state:
54+
description: 'Keep BuildKit state on cleanup. This is only useful on persistent self-hosted runners.'
55+
default: 'false'
56+
required: false
57+
name:
58+
description: 'Builder name set when creating the builder. If not specified, one will be generated.'
5359
required: false
5460
# TODO: remove deprecated config and config-inline inputs
5561
config:

src/context.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ export interface Inputs {
2525
append: string;
2626
cacheBinary: boolean;
2727
cleanup: boolean;
28+
keepState: boolean;
2829
}
2930

3031
export async function getInputs(): Promise<Inputs> {
3132
return {
3233
version: core.getInput('version'),
33-
name: await getBuilderName(core.getInput('driver') || 'docker-container'),
34+
name: await getBuilderName(core.getInput('name'), core.getInput('driver') || 'docker-container'),
3435
driver: core.getInput('driver') || 'docker-container',
3536
driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}),
3637
buildkitdFlags: core.getInput('buildkitd-flags'),
@@ -42,12 +43,16 @@ export async function getInputs(): Promise<Inputs> {
4243
buildkitdConfigInline: core.getInput('buildkitd-config-inline') || core.getInput('config-inline'),
4344
append: core.getInput('append'),
4445
cacheBinary: core.getBooleanInput('cache-binary'),
45-
cleanup: core.getBooleanInput('cleanup')
46+
cleanup: core.getBooleanInput('cleanup'),
47+
keepState: core.getBooleanInput('keep-state')
4648
};
4749
}
4850

49-
export async function getBuilderName(driver: string): Promise<string> {
50-
return driver == 'docker' ? await Docker.context() : `builder-${crypto.randomUUID()}`;
51+
export async function getBuilderName(name: string, driver: string): Promise<string> {
52+
if (driver == 'docker') {
53+
return await Docker.context();
54+
}
55+
return name || `builder-${crypto.randomUUID()}`;
5156
}
5257

5358
export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ actionsToolkit.run(
115115
});
116116
}
117117
}
118+
stateHelper.setKeepState(inputs.keepState);
118119

119120
if (inputs.driver !== 'docker') {
120121
await core.group(`Creating a new builder instance`, async () => {
@@ -249,7 +250,7 @@ actionsToolkit.run(
249250
const buildx = new Buildx({standalone: stateHelper.standalone});
250251
const builder = new Builder({buildx: buildx});
251252
if (await builder.exists(stateHelper.builderName)) {
252-
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]);
253+
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName, ...(stateHelper.keepState ? ['--keep-state'] : [])]);
253254
await Exec.getExecOutput(rmCmd.command, rmCmd.args, {
254255
ignoreReturnCode: true
255256
}).then(res => {

src/state-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const certsDir = process.env['STATE_certsDir'] || '';
99
export const tmpDockerContext = process.env['STATE_tmpDockerContext'] || '';
1010
export const cleanup = /true/i.test(process.env['STATE_cleanup'] || '');
1111
export const buildxIsDefaultBuilder = /true/i.test(process.env['STATE_buildxIsDefaultBuilder'] || '');
12+
export const keepState = !!process.env['STATE_keepState'];
1213

1314
export function setDebug(debug: string) {
1415
core.saveState('isDebug', debug);
@@ -45,3 +46,7 @@ export function setCleanup(cleanup: boolean) {
4546
export function setBuildxIsDefaultBuilder(buildxIsDefaultBuilder: boolean) {
4647
core.saveState('buildxIsDefaultBuilder', buildxIsDefaultBuilder);
4748
}
49+
50+
export function setKeepState(retain: boolean) {
51+
core.saveState('keepState', retain);
52+
}

0 commit comments

Comments
 (0)