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 {
|
|
|
|
if (os.platform() !== 'linux') {
|
|
|
|
core.setFailed('Only supported on linux platform');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 21:02:36 +02:00
|
|
|
const buildxVersion = await buildx.getVersion();
|
|
|
|
core.info(`📣 Buildx version: ${buildxVersion}`);
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2020-08-27 02:41:25 +02:00
|
|
|
const builderName: string =
|
2020-09-03 16:23:15 +02:00
|
|
|
inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`;
|
2020-08-18 17:40:31 +02:00
|
|
|
core.setOutput('name', builderName);
|
|
|
|
stateHelper.setBuilderName(builderName);
|
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.driver !== 'docker') {
|
2020-08-27 02:41:25 +02:00
|
|
|
core.info('🔨 Creating a new builder instance...');
|
2020-09-03 16:23:15 +02:00
|
|
|
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
|
|
|
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
|
|
|
createArgs.push('--driver-opt', driverOpt);
|
|
|
|
});
|
2020-09-03 21:02:36 +02:00
|
|
|
if (inputs.buildkitdFlags && semver.satisfies(buildxVersion, '>=0.3.0')) {
|
2020-09-03 16:23:15 +02:00
|
|
|
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
|
|
|
}
|
2020-08-27 02:41:25 +02:00
|
|
|
await exec.exec('docker', createArgs);
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2020-08-27 02:41:25 +02:00
|
|
|
core.info('🏃 Booting builder...');
|
|
|
|
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
|
|
|
|
}
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.install) {
|
2020-08-18 17:40:31 +02:00
|
|
|
core.info('🤝 Setting buildx as default builder...');
|
|
|
|
await exec.exec('docker', ['buildx', 'install']);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.info('🛒 Extracting available platforms...');
|
|
|
|
const platforms = await buildx.platforms();
|
|
|
|
core.info(`${platforms}`);
|
|
|
|
core.setOutput('platforms', platforms);
|
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanup(): Promise<void> {
|
|
|
|
if (stateHelper.builderName.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await mexec.exec('docker', ['buildx', 'rm', `${stateHelper.builderName}`], false).then(res => {
|
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
core.warning(res.stderr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|