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