Skip to content

Commit

Permalink
Support to retain cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalajiarun committed Feb 27, 2023
1 parent 2dd22fa commit c54303c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 6 deletions.
24 changes: 24 additions & 0 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('getCreateArgs', () => {
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -50,6 +51,7 @@ describe('getCreateArgs', () => {
['driver', 'docker'],
['install', 'false'],
['use', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -65,6 +67,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'false'],
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -82,6 +85,7 @@ describe('getCreateArgs', () => {
['endpoint', 'tls://foo:1234'],
['install', 'false'],
['use', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -99,6 +103,7 @@ describe('getCreateArgs', () => {
['endpoint', 'tls://foo:1234'],
['install', 'false'],
['use', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -115,6 +120,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'false'],
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -130,6 +136,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'false'],
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -139,6 +146,22 @@ describe('getCreateArgs', () => {
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7'
]
],
[
7,
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['keep-state', 'false'],
['custom-name', 'test-builder-name'],
]),
[
'create',
'--name', 'test-builder-name',
'--driver', 'docker-container',
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
'--use'
]
],
])(
'[%d] given %p as inputs, returns %p',
async (num: number, inputs: Map<string, string>, expected: Array<string>) => {
Expand Down Expand Up @@ -169,6 +192,7 @@ describe('getAppendArgs', () => {
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['keep-state', 'false']
]),
{
"name": "aws_graviton2",
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ inputs:
append:
description: 'Append additional nodes to the builder'
required: false
keep-state:
description: 'Keep state on cleanup'
default: 'false'
required: false
custom-name:
description: 'Custom builder name'
required: false

outputs:
name:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export interface Inputs {
config: string;
configInline: string;
append: string;
keepState: boolean;
}

export async function getInputs(): Promise<Inputs> {
return {
version: core.getInput('version'),
name: getBuilderName(core.getInput('driver') || 'docker-container'),
name: getBuilderName(core.getInput('custom-name'), core.getInput('driver') || 'docker-container'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: await getInputList('driver-opts', true),
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
Expand All @@ -51,11 +52,15 @@ export async function getInputs(): Promise<Inputs> {
endpoint: core.getInput('endpoint'),
config: core.getInput('config'),
configInline: core.getInput('config-inline'),
append: core.getInput('append')
append: core.getInput('append'),
keepState: core.getBooleanInput('keep-state')
};
}

export function getBuilderName(driver: string): string {
export function getBuilderName(customName: string, driver: string): string {
if (customName && customName.length > 0) {
return customName;
}
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
}

Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ async function run(): Promise<void> {
fs.mkdirSync(credsdir, {recursive: true});
stateHelper.setCredsDir(credsdir);

stateHelper.setKeepState(inputs.keepState);

if (inputs.driver !== 'docker') {
core.startGroup(`Creating a new builder instance`);
const authOpts = auth.setCredentials(credsdir, 0, inputs.driver, inputs.endpoint);
Expand Down Expand Up @@ -156,7 +158,7 @@ async function cleanup(): Promise<void> {

if (stateHelper.builderName.length > 0) {
core.startGroup(`Removing builder`);
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone));
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName, stateHelper.keepState ? '--keep-state' : ''], /true/i.test(stateHelper.standalone));
await exec
.getExecOutput(rmCmd.commandLine, rmCmd.args, {
ignoreReturnCode: true
Expand Down
5 changes: 5 additions & 0 deletions src/state-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const standalone = process.env['STATE_standalone'] || '';
export const builderName = process.env['STATE_builderName'] || '';
export const containerName = process.env['STATE_containerName'] || '';
export const credsDir = process.env['STATE_credsDir'] || '';
export const keepState = process.env['STATE_keepState'] || false;

export function setDebug(debug: string) {
core.saveState('isDebug', debug);
Expand All @@ -27,6 +28,10 @@ export function setCredsDir(credsDir: string) {
core.saveState('credsDir', credsDir);
}

export function setKeepState(retain: boolean) {
core.saveState('keepState', retain);
}

if (!IsPost) {
core.saveState('isPost', 'true');
}

0 comments on commit c54303c

Please sign in to comment.