2020-08-18 15:40:31 +00:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2022-03-21 12:43:41 +00:00
|
|
|
import * as uuid from 'uuid';
|
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';
|
2022-04-17 15:22:03 +00:00
|
|
|
import * as docker from './docker';
|
2020-08-18 15:40:31 +00:00
|
|
|
import * as stateHelper from './state-helper';
|
2021-07-02 05:02:22 +00:00
|
|
|
import * as util from './util';
|
2021-06-23 14:11:52 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 15:40:31 +00:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
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');
|
|
|
|
|
2022-04-17 15:22:03 +00:00
|
|
|
// standalone if docker cli not available
|
|
|
|
const standalone = !(await docker.isAvailable());
|
|
|
|
stateHelper.setStandalone(standalone);
|
|
|
|
|
|
|
|
core.startGroup(`Docker info`);
|
|
|
|
if (standalone) {
|
|
|
|
core.info(`Docker info skipped in standalone mode`);
|
|
|
|
} else {
|
|
|
|
await exec.exec('docker', ['version'], {
|
|
|
|
failOnStdErr: false
|
|
|
|
});
|
|
|
|
await exec.exec('docker', ['info'], {
|
|
|
|
failOnStdErr: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
core.endGroup();
|
|
|
|
|
2021-07-02 05:02:22 +00:00
|
|
|
if (util.isValidUrl(inputs.version)) {
|
2022-04-17 15:22:03 +00:00
|
|
|
if (standalone) {
|
|
|
|
throw new Error(`Cannot build from source without the Docker CLI`);
|
|
|
|
}
|
2021-07-02 05:02:22 +00:00
|
|
|
core.startGroup(`Build and install buildx`);
|
2022-04-17 15:22:03 +00:00
|
|
|
await buildx.build(inputs.version, dockerConfigHome, standalone);
|
2021-07-02 05:02:22 +00:00
|
|
|
core.endGroup();
|
2022-04-17 15:22:03 +00:00
|
|
|
} else if (!(await buildx.isAvailable(standalone)) || inputs.version) {
|
2021-07-02 05:02:22 +00:00
|
|
|
core.startGroup(`Download and install buildx`);
|
2022-04-17 15:22:03 +00:00
|
|
|
await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone);
|
2020-10-20 20:29:53 +00:00
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 15:22:03 +00:00
|
|
|
const buildxVersion = await buildx.getVersion(standalone);
|
|
|
|
await core.group(`Buildx version`, async () => {
|
|
|
|
const versionCmd = buildx.getCommand(['version'], standalone);
|
|
|
|
await exec.exec(versionCmd.commandLine, versionCmd.args, {
|
|
|
|
failOnStdErr: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-21 12:43:41 +00:00
|
|
|
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${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`);
|
2022-04-17 15:22:03 +00:00
|
|
|
const createArgs: Array<string> = ['create', '--name', builderName, '--driver', inputs.driver];
|
2021-07-02 05:02:22 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
2020-10-05 18:19:21 +00:00
|
|
|
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
|
|
|
createArgs.push('--driver-opt', driverOpt);
|
|
|
|
});
|
2022-09-16 16:12:21 +00:00
|
|
|
if (inputs.driver != 'remote' && inputs.buildkitdFlags) {
|
2020-10-05 18:19:21 +00:00
|
|
|
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
|
|
|
}
|
2022-09-16 16:12:21 +00:00
|
|
|
if (inputs.driver != 'remote') {
|
|
|
|
if (inputs.config) {
|
|
|
|
createArgs.push('--config', await buildx.getConfigFile(inputs.config));
|
|
|
|
} else if (inputs.configInline) {
|
|
|
|
createArgs.push('--config', await buildx.getConfigInline(inputs.configInline));
|
|
|
|
}
|
2021-04-21 17:37:54 +00:00
|
|
|
}
|
2022-04-17 15:22:03 +00:00
|
|
|
const createCmd = buildx.getCommand(createArgs, standalone);
|
|
|
|
await exec.exec(createCmd.commandLine, createCmd.args);
|
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`);
|
2022-04-17 15:22:03 +00:00
|
|
|
const bootstrapArgs: Array<string> = ['inspect', '--bootstrap'];
|
2021-07-02 05:02:22 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
|
2021-01-04 18:03:58 +00:00
|
|
|
bootstrapArgs.push('--builder', builderName);
|
|
|
|
}
|
2022-04-17 15:22:03 +00:00
|
|
|
const bootstrapCmd = buildx.getCommand(bootstrapArgs, standalone);
|
|
|
|
await exec.exec(bootstrapCmd.commandLine, bootstrapCmd.args);
|
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) {
|
2022-04-17 15:22:03 +00:00
|
|
|
if (standalone) {
|
|
|
|
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
|
|
|
|
}
|
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`);
|
2022-04-17 15:22:03 +00:00
|
|
|
const builder = await buildx.inspect(builderName, standalone);
|
2021-04-23 16:14:38 +00:00
|
|
|
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
|
|
|
|
2022-04-17 15:22:03 +00:00
|
|
|
if (!standalone && inputs.driver == 'docker-container') {
|
2021-04-23 20:08:40 +00:00
|
|
|
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
|
2021-04-28 16:08:43 +00:00
|
|
|
core.startGroup(`BuildKit version`);
|
|
|
|
core.info(await buildx.getBuildKitVersion(`buildx_buildkit_${builder.node_name}`));
|
|
|
|
core.endGroup();
|
2021-04-23 20:08:40 +00:00
|
|
|
}
|
|
|
|
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`);
|
2021-06-23 14:11:52 +00: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-23 20:08:40 +00:00
|
|
|
core.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stateHelper.builderName.length > 0) {
|
|
|
|
core.startGroup(`Removing builder`);
|
2022-04-17 15:22:03 +00:00
|
|
|
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone));
|
2021-06-23 14:11:52 +00:00
|
|
|
await exec
|
2022-04-17 15:22:03 +00:00
|
|
|
.getExecOutput(rmCmd.commandLine, rmCmd.args, {
|
2021-06-23 14:11:52 +00:00
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
core.warning(res.stderr.trim());
|
|
|
|
}
|
|
|
|
});
|
2021-04-23 20:08:40 +00:00
|
|
|
core.endGroup();
|
2020-08-18 15:40:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|