2020-08-27 00:41:25 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 15:40:31 +00:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-09-03 19:02:36 +00:00
|
|
|
import * as semver from 'semver';
|
2020-08-18 15:40:31 +00:00
|
|
|
import * as buildx from './buildx';
|
2020-09-03 14:23:15 +00:00
|
|
|
import * as context from './context';
|
2020-08-18 15:40:31 +00:00
|
|
|
import * as mexec from './exec';
|
|
|
|
import * as stateHelper from './state-helper';
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2021-04-23 20:43:09 +00:00
|
|
|
core.startGroup(`Docker info`);
|
|
|
|
await exec.exec('docker', ['version']);
|
|
|
|
await exec.exec('docker', ['info']);
|
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2020-09-03 14:23:15 +00:00
|
|
|
const inputs: context.Inputs = await context.getInputs();
|
2020-08-18 15:40:31 +00:00
|
|
|
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
|
|
|
|
2020-09-03 14:23:15 +00:00
|
|
|
if (!(await buildx.isAvailable()) || inputs.version) {
|
2021-04-01 23:19:14 +00:00
|
|
|
core.startGroup(`Installing buildx`);
|
2020-09-03 14:23:15 +00:00
|
|
|
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 19:02:36 +00:00
|
|
|
const buildxVersion = await buildx.getVersion();
|
2021-04-01 23:19:14 +00:00
|
|
|
core.info(`Using buildx ${buildxVersion}`);
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2021-01-01 23:54:52 +00:00
|
|
|
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${require('uuid').v4()}`;
|
2021-04-23 16:14:38 +00:00
|
|
|
context.setOutput('name', builderName);
|
2020-08-18 15:40:31 +00:00
|
|
|
stateHelper.setBuilderName(builderName);
|
|
|
|
|
2020-09-03 14:23:15 +00:00
|
|
|
if (inputs.driver !== 'docker') {
|
2021-04-01 23:19:14 +00:00
|
|
|
core.startGroup(`Creating a new builder instance`);
|
2020-09-03 14:23:15 +00:00
|
|
|
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
2020-10-05 18:19:21 +00:00
|
|
|
if (semver.satisfies(buildxVersion, '>=0.3.0')) {
|
|
|
|
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
|
|
|
createArgs.push('--driver-opt', driverOpt);
|
|
|
|
});
|
|
|
|
if (inputs.buildkitdFlags) {
|
|
|
|
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
|
|
|
|
}
|
2020-08-27 00:41:25 +00:00
|
|
|
}
|
2020-09-03 14:23:15 +00:00
|
|
|
if (inputs.use) {
|
2020-08-27 00:41:25 +00:00
|
|
|
createArgs.push('--use');
|
|
|
|
}
|
2020-09-08 13:52:09 +00:00
|
|
|
if (inputs.endpoint) {
|
|
|
|
createArgs.push(inputs.endpoint);
|
2020-09-06 14:37:52 +00:00
|
|
|
}
|
2021-04-21 17:37:54 +00:00
|
|
|
if (inputs.config) {
|
|
|
|
createArgs.push('--config', inputs.config);
|
|
|
|
}
|
2020-08-27 00:41:25 +00:00
|
|
|
await exec.exec('docker', createArgs);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2021-04-01 23:19:14 +00:00
|
|
|
core.startGroup(`Booting builder`);
|
2021-01-04 18:03:58 +00:00
|
|
|
let bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
|
|
|
|
if (semver.satisfies(buildxVersion, '>=0.4.0')) {
|
|
|
|
bootstrapArgs.push('--builder', builderName);
|
|
|
|
}
|
|
|
|
await exec.exec('docker', bootstrapArgs);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2020-08-27 00:41:25 +00:00
|
|
|
}
|
2020-08-18 15:40:31 +00:00
|
|
|
|
2020-09-03 14:23:15 +00:00
|
|
|
if (inputs.install) {
|
2021-04-01 23:19:14 +00:00
|
|
|
core.startGroup(`Setting buildx as default builder`);
|
2020-08-18 15:40:31 +00:00
|
|
|
await exec.exec('docker', ['buildx', 'install']);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 16:14:38 +00:00
|
|
|
core.startGroup(`Inspect builder`);
|
|
|
|
const builder = await buildx.inspect(builderName);
|
|
|
|
core.info(JSON.stringify(builder, undefined, 2));
|
|
|
|
context.setOutput('driver', builder.driver);
|
|
|
|
context.setOutput('endpoint', builder.node_endpoint);
|
|
|
|
context.setOutput('status', builder.node_status);
|
|
|
|
context.setOutput('flags', builder.node_flags);
|
|
|
|
context.setOutput('platforms', builder.node_platforms);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2021-04-23 20:08:40 +00:00
|
|
|
|
|
|
|
if (inputs.driver == 'docker-container') {
|
|
|
|
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
|
|
|
|
}
|
|
|
|
if (core.isDebug() || builder.node_flags?.includes('--debug')) {
|
|
|
|
stateHelper.setDebug('true');
|
|
|
|
}
|
2020-08-18 15:40:31 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanup(): Promise<void> {
|
2021-04-23 20:08:40 +00:00
|
|
|
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
|
|
|
|
core.startGroup(`BuildKit container logs`);
|
|
|
|
await mexec.exec('docker', ['logs', `${stateHelper.containerName}`], false).then(res => {
|
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
core.warning(res.stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
core.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stateHelper.builderName.length > 0) {
|
|
|
|
core.startGroup(`Removing builder`);
|
|
|
|
await mexec.exec('docker', ['buildx', 'rm', `${stateHelper.builderName}`], false).then(res => {
|
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
core.warning(res.stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|