Skip to content

Commit

Permalink
Support to retain cache
Browse files Browse the repository at this point in the history
Signed-off-by: Balaji Arun <[email protected]>
  • Loading branch information
ibalajiarun committed Apr 19, 2023
1 parent a946f06 commit ff70196
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
28 changes: 27 additions & 1 deletion __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -50,6 +51,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -67,6 +69,7 @@ describe('getCreateArgs', () => {
['use', 'false'],
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
['cleanup', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -86,6 +89,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -105,6 +109,7 @@ describe('getCreateArgs', () => {
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false']
]),
[
'create',
Expand All @@ -123,6 +128,7 @@ describe('getCreateArgs', () => {
['use', 'false'],
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
['cleanup', 'true'],
['keep-state', 'false'],
]),
[
'create',
Expand All @@ -140,6 +146,7 @@ describe('getCreateArgs', () => {
['use', 'false'],
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
['cleanup', 'true'],
['keep-state', 'false'],
]),
[
'create',
Expand All @@ -148,7 +155,25 @@ describe('getCreateArgs', () => {
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7'
]
]
],
[
7,
'v0.10.3',
new Map<string, string>([
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false'],
['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 buildx %s and %p as inputs, returns %p',
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
Expand Down Expand Up @@ -185,6 +210,7 @@ describe('getAppendArgs', () => {
['install', 'false'],
['use', 'true'],
['cleanup', 'true'],
['keep-state', 'false']
]),
{
"name": "aws_graviton2",
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ inputs:
cleanup:
description: 'Cleanup temp files and remove builder at the end of a job'
default: 'true'
keep-state:
description: 'Keep state on cleanup'
default: 'false'
required: false
name:
description: 'Builder name use use when creating the builder. If not specified, one will be generated. Specifying a name enables reusing a buildkit volume in persistent self-hosted runners.'
required: false

outputs:
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.

13 changes: 9 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ export interface Inputs {
configInline: string;
append: string;
cleanup: boolean;
keepState: boolean;
}

export async function getInputs(): Promise<Inputs> {
return {
version: core.getInput('version'),
name: await getBuilderName(core.getInput('driver') || 'docker-container'),
name: getBuilderName(core.getInput('name'), core.getInput('driver') || 'docker-container'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}),
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
Expand All @@ -37,12 +38,16 @@ export async function getInputs(): Promise<Inputs> {
config: core.getInput('config'),
configInline: core.getInput('config-inline'),
append: core.getInput('append'),
cleanup: core.getBooleanInput('cleanup')
cleanup: core.getBooleanInput('cleanup'),
keepState: core.getBooleanInput('keep-state')
};
}

export async function getBuilderName(driver: string): Promise<string> {
return driver == 'docker' ? await Docker.context() : `builder-${uuid.v4()}`;
export function getBuilderName(name: string, driver: string): string {
if (name) {
return name;
}
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
}

export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ actionsToolkit.run(
fs.mkdirSync(Buildx.certsDir, {recursive: true});
stateHelper.setCertsDir(Buildx.certsDir);

stateHelper.setKeepState(inputs.keepState);

if (inputs.driver !== 'docker') {
await core.group(`Creating a new builder instance`, async () => {
const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, {
Expand Down Expand Up @@ -176,7 +178,7 @@ actionsToolkit.run(
const buildx = new Buildx({standalone: stateHelper.standalone});
const builder = new Builder({buildx: buildx});
if (await builder.exists(stateHelper.builderName)) {
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]);
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName, stateHelper.keepState ? '--keep-state' : '']);
await exec
.getExecOutput(rmCmd.command, rmCmd.args, {
ignoreReturnCode: true
Expand Down
6 changes: 5 additions & 1 deletion src/state-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const builderDriver = process.env['STATE_builderDriver'] || '';
export const containerName = process.env['STATE_containerName'] || '';
export const certsDir = process.env['STATE_certsDir'] || '';
export const cleanup = /true/i.test(process.env['STATE_cleanup'] || '');

export const keepState = !!process.env['STATE_keepState'];
export function setDebug(debug: string) {
core.saveState('isDebug', debug);
}
Expand Down Expand Up @@ -35,3 +35,7 @@ export function setCertsDir(certsDir: string) {
export function setCleanup(cleanup: boolean) {
core.saveState('cleanup', cleanup);
}

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

0 comments on commit ff70196

Please sign in to comment.