Support to retain cache

Signed-off-by: Balaji Arun <balajia@vt.edu>
This commit is contained in:
Balaji Arun 2023-02-23 13:11:05 -08:00
parent a946f06b5d
commit ff70196977
7 changed files with 52 additions and 9 deletions

View File

@ -33,6 +33,7 @@ describe('getCreateArgs', () => {
['install', 'false'], ['install', 'false'],
['use', 'true'], ['use', 'true'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
[ [
'create', 'create',
@ -50,6 +51,7 @@ describe('getCreateArgs', () => {
['install', 'false'], ['install', 'false'],
['use', 'true'], ['use', 'true'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
[ [
'create', 'create',
@ -67,6 +69,7 @@ describe('getCreateArgs', () => {
['use', 'false'], ['use', 'false'],
['driver-opts', 'image=moby/buildkit:master\nnetwork=host'], ['driver-opts', 'image=moby/buildkit:master\nnetwork=host'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
[ [
'create', 'create',
@ -86,6 +89,7 @@ describe('getCreateArgs', () => {
['install', 'false'], ['install', 'false'],
['use', 'true'], ['use', 'true'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
[ [
'create', 'create',
@ -105,6 +109,7 @@ describe('getCreateArgs', () => {
['install', 'false'], ['install', 'false'],
['use', 'true'], ['use', 'true'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
[ [
'create', 'create',
@ -123,6 +128,7 @@ describe('getCreateArgs', () => {
['use', 'false'], ['use', 'false'],
['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`], ['driver-opts', `"env.no_proxy=localhost,127.0.0.1,.mydomain"`],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false'],
]), ]),
[ [
'create', 'create',
@ -140,6 +146,7 @@ describe('getCreateArgs', () => {
['use', 'false'], ['use', 'false'],
['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'], ['platforms', 'linux/amd64\n"linux/arm64,linux/arm/v7"'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false'],
]), ]),
[ [
'create', 'create',
@ -148,7 +155,25 @@ describe('getCreateArgs', () => {
'--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host', '--buildkitd-flags', '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
'--platform', 'linux/amd64,linux/arm64,linux/arm/v7' '--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', '[%d] given buildx %s and %p as inputs, returns %p',
async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => { async (num: number, buildxVersion: string, inputs: Map<string, string>, expected: Array<string>) => {
@ -185,6 +210,7 @@ describe('getAppendArgs', () => {
['install', 'false'], ['install', 'false'],
['use', 'true'], ['use', 'true'],
['cleanup', 'true'], ['cleanup', 'true'],
['keep-state', 'false']
]), ]),
{ {
"name": "aws_graviton2", "name": "aws_graviton2",

View File

@ -47,6 +47,12 @@ inputs:
cleanup: cleanup:
description: 'Cleanup temp files and remove builder at the end of a job' description: 'Cleanup temp files and remove builder at the end of a job'
default: 'true' 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 required: false
outputs: outputs:

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View File

@ -21,12 +21,13 @@ export interface Inputs {
configInline: string; configInline: string;
append: string; append: string;
cleanup: boolean; cleanup: boolean;
keepState: boolean;
} }
export async function getInputs(): Promise<Inputs> { export async function getInputs(): Promise<Inputs> {
return { return {
version: core.getInput('version'), 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', driver: core.getInput('driver') || 'docker-container',
driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}), driverOpts: Util.getInputList('driver-opts', {ignoreComma: true, quote: false}),
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host', buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
@ -37,12 +38,16 @@ export async function getInputs(): Promise<Inputs> {
config: core.getInput('config'), config: core.getInput('config'),
configInline: core.getInput('config-inline'), configInline: core.getInput('config-inline'),
append: core.getInput('append'), 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> { export function getBuilderName(name: string, driver: string): string {
return driver == 'docker' ? await Docker.context() : `builder-${uuid.v4()}`; if (name) {
return name;
}
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
} }
export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> { export async function getCreateArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {

View File

@ -66,6 +66,8 @@ actionsToolkit.run(
fs.mkdirSync(Buildx.certsDir, {recursive: true}); fs.mkdirSync(Buildx.certsDir, {recursive: true});
stateHelper.setCertsDir(Buildx.certsDir); stateHelper.setCertsDir(Buildx.certsDir);
stateHelper.setKeepState(inputs.keepState);
if (inputs.driver !== 'docker') { if (inputs.driver !== 'docker') {
await core.group(`Creating a new builder instance`, async () => { await core.group(`Creating a new builder instance`, async () => {
const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, { const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, {
@ -176,7 +178,7 @@ actionsToolkit.run(
const buildx = new Buildx({standalone: stateHelper.standalone}); const buildx = new Buildx({standalone: stateHelper.standalone});
const builder = new Builder({buildx: buildx}); const builder = new Builder({buildx: buildx});
if (await builder.exists(stateHelper.builderName)) { 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 await exec
.getExecOutput(rmCmd.command, rmCmd.args, { .getExecOutput(rmCmd.command, rmCmd.args, {
ignoreReturnCode: true ignoreReturnCode: true

View File

@ -7,7 +7,7 @@ export const builderDriver = process.env['STATE_builderDriver'] || '';
export const containerName = process.env['STATE_containerName'] || ''; export const containerName = process.env['STATE_containerName'] || '';
export const certsDir = process.env['STATE_certsDir'] || ''; export const certsDir = process.env['STATE_certsDir'] || '';
export const cleanup = /true/i.test(process.env['STATE_cleanup'] || ''); export const cleanup = /true/i.test(process.env['STATE_cleanup'] || '');
export const keepState = !!process.env['STATE_keepState'];
export function setDebug(debug: string) { export function setDebug(debug: string) {
core.saveState('isDebug', debug); core.saveState('isDebug', debug);
} }
@ -35,3 +35,7 @@ export function setCertsDir(certsDir: string) {
export function setCleanup(cleanup: boolean) { export function setCleanup(cleanup: boolean) {
core.saveState('cleanup', cleanup); core.saveState('cleanup', cleanup);
} }
export function setKeepState(retain: boolean) {
core.saveState('keepState', retain);
}