2020-08-27 02:41:25 +02:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 17:40:31 +02:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-09-03 21:02:36 +02:00
|
|
|
import * as semver from 'semver';
|
2020-08-18 17:40:31 +02:00
|
|
|
import * as buildx from './buildx';
|
2020-09-03 16:23:15 +02:00
|
|
|
import * as context from './context';
|
2020-08-18 17:40:31 +02:00
|
|
|
import * as mexec from './exec';
|
|
|
|
import * as stateHelper from './state-helper';
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2021-04-23 22:43:09 +02:00
|
|
|
core.startGroup(`Docker info`);
|
|
|
|
await exec.exec('docker', ['version']);
|
|
|
|
await exec.exec('docker', ['info']);
|
|
|
|
core.endGroup();
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
const inputs: context.Inputs = await context.getInputs();
|
2020-08-18 17:40:31 +02:00
|
|
|
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (!(await buildx.isAvailable()) || inputs.version) {
|
2021-04-02 01:19:14 +02:00
|
|
|
core.startGroup(`Installing buildx`);
|
2020-09-03 16:23:15 +02:00
|
|
|
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
2020-10-20 22:29:53 +02:00
|
|
|
core.endGroup();
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 21:02:36 +02:00
|
|
|
const buildxVersion = await buildx.getVersion();
|
2021-04-02 01:19:14 +02:00
|
|
|
core.info(`Using buildx ${buildxVersion}`);
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2021-01-02 00:54:52 +01:00
|
|
|
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${require('uuid').v4()}`;
|
2021-04-23 18:14:38 +02:00
|
|
|
context.setOutput('name', builderName);
|
2020-08-18 17:40:31 +02:00
|
|
|
stateHelper.setBuilderName(builderName);
|
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.driver !== 'docker') {
|
2021-04-02 01:19:14 +02:00
|
|
|
core.startGroup(`Creating a new builder instance`);
|
2020-09-03 16:23:15 +02:00
|
|
|
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
2020-10-05 20:19:21 +02: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 02:41:25 +02:00
|
|
|
}
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.use) {
|
2020-08-27 02:41:25 +02:00
|
|
|
createArgs.push('--use');
|
|
|
|
}
|
2020-09-08 15:52:09 +02:00
|
|
|
if (inputs.endpoint) {
|
|
|
|
createArgs.push(inputs.endpoint);
|
2020-09-06 16:37:52 +02:00
|
|
|
}
|
2021-04-21 19:37:54 +02:00
|
|
|
if (inputs.config) {
|
|
|
|
createArgs.push('--config', inputs.config);
|
|
|
|
}
|
2020-08-27 02:41:25 +02:00
|
|
|
await exec.exec('docker', createArgs);
|
2020-10-20 22:29:53 +02:00
|
|
|
core.endGroup();
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2021-04-02 01:19:14 +02:00
|
|
|
core.startGroup(`Booting builder`);
|
2021-01-04 19:03:58 +01: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 22:29:53 +02:00
|
|
|
core.endGroup();
|
2020-08-27 02:41:25 +02:00
|
|
|
}
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.install) {
|
2021-04-02 01:19:14 +02:00
|
|
|
core.startGroup(`Setting buildx as default builder`);
|
2020-08-18 17:40:31 +02:00
|
|
|
await exec.exec('docker', ['buildx', 'install']);
|
2020-10-20 22:29:53 +02:00
|
|
|
core.endGroup();
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2021-04-23 18:14:38 +02: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 22:29:53 +02:00
|
|
|
core.endGroup();
|
2021-04-23 22:08:40 +02: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 17:40:31 +02:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanup(): Promise<void> {
|
2021-04-23 22:08:40 +02: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 17:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|