Skip to content

Commit 63216ab

Browse files
committed
Support to retain cache
Signed-off-by: Balaji Arun <[email protected]>
1 parent 2dd22fa commit 63216ab

File tree

7 files changed

+49
-6
lines changed

7 files changed

+49
-6
lines changed

__tests__/context.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('getCreateArgs', () => {
3535
new Map<string, string>([
3636
['install', 'false'],
3737
['use', 'true'],
38+
['keep-state', 'false']
3839
]),
3940
[
4041
'create',
@@ -50,6 +51,7 @@ describe('getCreateArgs', () => {
5051
['driver', 'docker'],
5152
['install', 'false'],
5253
['use', 'true'],
54+
['keep-state', 'false']
5355
]),
5456
[
5557
'create',
@@ -65,6 +67,7 @@ describe('getCreateArgs', () => {
6567
['install', 'false'],
6668
['use', 'false'],
6769
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
70+
['keep-state', 'false']
6871
]),
6972
[
7073
'create',
@@ -82,6 +85,7 @@ describe('getCreateArgs', () => {
8285
['endpoint', 'tls://foo:1234'],
8386
['install', 'false'],
8487
['use', 'true'],
88+
['keep-state', 'false']
8589
]),
8690
[
8791
'create',
@@ -99,6 +103,7 @@ describe('getCreateArgs', () => {
99103
['endpoint', 'tls://foo:1234'],
100104
['install', 'false'],
101105
['use', 'true'],
106+
['keep-state', 'false']
102107
]),
103108
[
104109
'create',
@@ -115,6 +120,7 @@ describe('getCreateArgs', () => {
115120
['install', 'false'],
116121
['use', 'false'],
117122
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
123+
['keep-state', 'false']
118124
]),
119125
[
120126
'create',
@@ -130,6 +136,7 @@ describe('getCreateArgs', () => {
130136
['install', 'false'],
131137
['use', 'false'],
132138
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
139+
['keep-state', 'false']
133140
]),
134141
[
135142
'create',
@@ -139,6 +146,22 @@ describe('getCreateArgs', () => {
139146
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7'
140147
]
141148
],
149+
[
150+
7,
151+
new Map<string, string>([
152+
['install', 'false'],
153+
['use', 'true'],
154+
['keep-state', 'false'],
155+
['custom-name', 'test-builder-name'],
156+
]),
157+
[
158+
'create',
159+
'--name', 'test-builder-name',
160+
'--driver', 'docker-container',
161+
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
162+
'--use'
163+
]
164+
],
142165
])(
143166
'[%d] given %p as inputs, returns %p',
144167
async (num: number, inputs: Map<string, string>, expected: Array<string>) => {
@@ -169,6 +192,7 @@ describe('getAppendArgs', () => {
169192
new Map<string, string>([
170193
['install', 'false'],
171194
['use', 'true'],
195+
['keep-state', 'false']
172196
]),
173197
{
174198
"name": "aws_graviton2",

action.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ inputs:
4444
append:
4545
description: 'Append additional nodes to the builder'
4646
required: false
47+
keep-state:
48+
description: 'Keep state on cleanup'
49+
default: 'false'
50+
required: false
51+
custom-name:
52+
description: 'Custom builder name'
53+
required: false
4754

4855
outputs:
4956
name:

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ export interface Inputs {
3636
config: string;
3737
configInline: string;
3838
append: string;
39+
keepState: boolean;
3940
}
4041

4142
export async function getInputs(): Promise<Inputs> {
4243
return {
4344
version: core.getInput('version'),
44-
name: getBuilderName(core.getInput('driver') || 'docker-container'),
45+
name: getBuilderName(core.getInput('custom-name'), core.getInput('driver') || 'docker-container'),
4546
driver: core.getInput('driver') || 'docker-container',
4647
driverOpts: await getInputList('driver-opts', true),
4748
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
@@ -51,11 +52,15 @@ export async function getInputs(): Promise<Inputs> {
5152
endpoint: core.getInput('endpoint'),
5253
config: core.getInput('config'),
5354
configInline: core.getInput('config-inline'),
54-
append: core.getInput('append')
55+
append: core.getInput('append'),
56+
keepState: core.getBooleanInput('keep-state')
5557
};
5658
}
5759

58-
export function getBuilderName(driver: string): string {
60+
export function getBuilderName(customName: string, driver: string): string {
61+
if (customName && customName.length > 0) {
62+
return customName;
63+
}
5964
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
6065
}
6166

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ async function run(): Promise<void> {
6161
fs.mkdirSync(credsdir, {recursive: true});
6262
stateHelper.setCredsDir(credsdir);
6363

64+
stateHelper.setKeepState(inputs.keepState);
65+
6466
if (inputs.driver !== 'docker') {
6567
core.startGroup(`Creating a new builder instance`);
6668
const authOpts = auth.setCredentials(credsdir, 0, inputs.driver, inputs.endpoint);
@@ -156,7 +158,7 @@ async function cleanup(): Promise<void> {
156158

157159
if (stateHelper.builderName.length > 0) {
158160
core.startGroup(`Removing builder`);
159-
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone));
161+
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName, stateHelper.keepState ? '--keep-state' : ''], /true/i.test(stateHelper.standalone));
160162
await exec
161163
.getExecOutput(rmCmd.commandLine, rmCmd.args, {
162164
ignoreReturnCode: true

src/state-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const standalone = process.env['STATE_standalone'] || '';
66
export const builderName = process.env['STATE_builderName'] || '';
77
export const containerName = process.env['STATE_containerName'] || '';
88
export const credsDir = process.env['STATE_credsDir'] || '';
9+
export const keepState = process.env['STATE_keepState'] || false;
910

1011
export function setDebug(debug: string) {
1112
core.saveState('isDebug', debug);
@@ -27,6 +28,10 @@ export function setCredsDir(credsDir: string) {
2728
core.saveState('credsDir', credsDir);
2829
}
2930

31+
export function setKeepState(retain: boolean) {
32+
core.saveState('keepState', retain);
33+
}
34+
3035
if (!IsPost) {
3136
core.saveState('isPost', 'true');
3237
}

0 commit comments

Comments
 (0)