2022-09-22 02:48:11 +02:00
|
|
|
import * as fs from 'fs';
|
2023-02-19 02:12:29 +01:00
|
|
|
import * as yaml from 'js-yaml';
|
2021-06-23 16:11:52 +02:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2023-02-19 02:12:29 +01:00
|
|
|
import * as actionsToolkit from '@docker/actions-toolkit';
|
|
|
|
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx';
|
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker';
|
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
|
|
|
import {Util} from '@docker/actions-toolkit/lib/util';
|
|
|
|
import {Node} from '@docker/actions-toolkit/lib/types/builder';
|
|
|
|
|
|
|
|
import * as context from './context';
|
|
|
|
import * as stateHelper from './state-helper';
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
actionsToolkit.run(
|
|
|
|
// main
|
|
|
|
async () => {
|
2020-09-03 16:23:15 +02:00
|
|
|
const inputs: context.Inputs = await context.getInputs();
|
2023-02-19 02:12:29 +01:00
|
|
|
const toolkit = new Toolkit();
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
const standalone = await toolkit.buildx.isStandalone();
|
2022-04-17 17:22:03 +02:00
|
|
|
stateHelper.setStandalone(standalone);
|
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Docker info`, async () => {
|
|
|
|
try {
|
|
|
|
await Docker.printVersion();
|
|
|
|
await Docker.printInfo();
|
|
|
|
} catch (e) {
|
|
|
|
core.info(e.message);
|
|
|
|
}
|
|
|
|
});
|
2022-04-17 17:22:03 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
let toolPath;
|
|
|
|
if (Util.isValidUrl(inputs.version)) {
|
2022-04-17 17:22:03 +02:00
|
|
|
if (standalone) {
|
|
|
|
throw new Error(`Cannot build from source without the Docker CLI`);
|
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Build buildx from source`, async () => {
|
|
|
|
toolPath = await toolkit.buildxInstall.build(inputs.version);
|
|
|
|
});
|
|
|
|
} else if (!(await toolkit.buildx.isAvailable()) || inputs.version) {
|
|
|
|
await core.group(`Download buildx from GitHub Releases`, async () => {
|
|
|
|
toolPath = await toolkit.buildxInstall.download(inputs.version || 'latest');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (toolPath) {
|
|
|
|
await core.group(`Install buildx`, async () => {
|
|
|
|
if (standalone) {
|
|
|
|
await toolkit.buildxInstall.installStandalone(toolPath);
|
|
|
|
} else {
|
|
|
|
await toolkit.buildxInstall.installPlugin(toolPath);
|
|
|
|
}
|
|
|
|
});
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2022-04-17 17:22:03 +02:00
|
|
|
await core.group(`Buildx version`, async () => {
|
2023-02-19 02:12:29 +01:00
|
|
|
await toolkit.buildx.printVersion();
|
2022-04-17 17:22:03 +02:00
|
|
|
});
|
|
|
|
|
2022-10-12 11:30:30 +02:00
|
|
|
core.setOutput('name', inputs.name);
|
2022-09-19 11:36:58 +02:00
|
|
|
stateHelper.setBuilderName(inputs.name);
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
fs.mkdirSync(Buildx.certsDir, {recursive: true});
|
|
|
|
stateHelper.setCertsDir(Buildx.certsDir);
|
2022-09-22 02:48:11 +02:00
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.driver !== 'docker') {
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Creating a new builder instance`, async () => {
|
|
|
|
const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, inputs.endpoint, {
|
|
|
|
cacert: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_CACERT`],
|
|
|
|
cert: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_CERT`],
|
|
|
|
key: process.env[`${context.builderNodeEnvPrefix}_0_AUTH_TLS_KEY`]
|
|
|
|
});
|
|
|
|
if (certsDriverOpts.length > 0) {
|
|
|
|
inputs.driverOpts = [...inputs.driverOpts, ...certsDriverOpts];
|
|
|
|
}
|
|
|
|
const createCmd = await toolkit.buildx.getCommand(await context.getCreateArgs(inputs, toolkit));
|
|
|
|
await exec.exec(createCmd.command, createCmd.args);
|
|
|
|
});
|
2020-08-27 02:41:25 +02:00
|
|
|
}
|
2020-08-18 17:40:31 +02:00
|
|
|
|
2022-09-19 11:34:47 +02:00
|
|
|
if (inputs.append) {
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Appending node(s) to builder`, async () => {
|
|
|
|
let nodeIndex = 1;
|
|
|
|
const nodes = yaml.load(inputs.append) as Node[];
|
|
|
|
for (const node of nodes) {
|
|
|
|
const certsDriverOpts = Buildx.resolveCertsDriverOpts(inputs.driver, `${node.endpoint}`, {
|
|
|
|
cacert: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_CACERT`],
|
|
|
|
cert: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_CERT`],
|
|
|
|
key: process.env[`${context.builderNodeEnvPrefix}_${nodeIndex}_AUTH_TLS_KEY`]
|
|
|
|
});
|
|
|
|
if (certsDriverOpts.length > 0) {
|
|
|
|
node['driver-opts'] = [...(node['driver-opts'] || []), ...certsDriverOpts];
|
|
|
|
}
|
|
|
|
const appendCmd = await toolkit.buildx.getCommand(await context.getAppendArgs(inputs, node, toolkit));
|
|
|
|
await exec.exec(appendCmd.command, appendCmd.args);
|
|
|
|
nodeIndex++;
|
2022-09-19 11:34:47 +02:00
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
});
|
2022-09-19 11:34:47 +02:00
|
|
|
}
|
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Booting builder`, async () => {
|
|
|
|
const inspectCmd = await toolkit.buildx.getCommand(await context.getInspectArgs(inputs, toolkit));
|
|
|
|
await exec.exec(inspectCmd.command, inspectCmd.args);
|
|
|
|
});
|
2022-09-19 11:36:58 +02:00
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
if (inputs.install) {
|
2022-04-17 17:22:03 +02:00
|
|
|
if (standalone) {
|
|
|
|
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
|
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
await core.group(`Setting buildx as default builder`, async () => {
|
|
|
|
const installCmd = await toolkit.buildx.getCommand(['install']);
|
|
|
|
await exec.exec(installCmd.command, installCmd.args);
|
|
|
|
});
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
const builderInspect = await toolkit.builder.inspect(inputs.name);
|
|
|
|
const firstNode = builderInspect.nodes[0];
|
|
|
|
|
|
|
|
await core.group(`Inspect builder`, async () => {
|
|
|
|
const reducedPlatforms: Array<string> = [];
|
|
|
|
for (const node of builderInspect.nodes) {
|
|
|
|
for (const platform of node.platforms?.split(',') || []) {
|
|
|
|
if (reducedPlatforms.indexOf(platform) > -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reducedPlatforms.push(platform);
|
2022-09-19 11:34:47 +02:00
|
|
|
}
|
2022-09-18 02:24:38 +02:00
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
core.info(JSON.stringify(builderInspect, undefined, 2));
|
|
|
|
core.setOutput('driver', builderInspect.driver);
|
|
|
|
core.setOutput('platforms', reducedPlatforms.join(','));
|
|
|
|
core.setOutput('nodes', JSON.stringify(builderInspect.nodes, undefined, 2));
|
|
|
|
core.setOutput('endpoint', firstNode.endpoint); // TODO: deprecated, to be removed in a later version
|
|
|
|
core.setOutput('status', firstNode.status); // TODO: deprecated, to be removed in a later version
|
|
|
|
core.setOutput('flags', firstNode['buildkitd-flags']); // TODO: deprecated, to be removed in a later version
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!standalone && builderInspect.driver == 'docker-container') {
|
|
|
|
stateHelper.setContainerName(`${Buildx.containerNamePrefix}${firstNode.name}`);
|
|
|
|
await core.group(`BuildKit version`, async () => {
|
|
|
|
for (const node of builderInspect.nodes) {
|
|
|
|
const buildkitVersion = await toolkit.buildkit.getVersion(node);
|
|
|
|
core.info(`${node.name}: ${buildkitVersion}`);
|
|
|
|
}
|
|
|
|
});
|
2021-04-23 22:08:40 +02:00
|
|
|
}
|
2022-09-18 02:24:38 +02:00
|
|
|
if (core.isDebug() || firstNode['buildkitd-flags']?.includes('--debug')) {
|
2021-04-23 22:08:40 +02:00
|
|
|
stateHelper.setDebug('true');
|
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
},
|
|
|
|
// post
|
|
|
|
async () => {
|
|
|
|
if (stateHelper.IsDebug && stateHelper.containerName.length > 0) {
|
|
|
|
await core.group(`BuildKit container logs`, async () => {
|
|
|
|
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-06-23 16:11:52 +02:00
|
|
|
});
|
2023-02-19 02:12:29 +01:00
|
|
|
}
|
2021-04-23 22:08:40 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
if (stateHelper.builderName.length > 0) {
|
|
|
|
await core.group(`Removing builder`, async () => {
|
|
|
|
const buildx = new Buildx({standalone: /true/i.test(stateHelper.standalone)});
|
|
|
|
const rmCmd = await buildx.getCommand(['rm', stateHelper.builderName]);
|
|
|
|
await exec
|
|
|
|
.getExecOutput(rmCmd.command, rmCmd.args, {
|
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
core.warning(res.stderr.trim());
|
|
|
|
}
|
|
|
|
});
|
2021-06-23 16:11:52 +02:00
|
|
|
});
|
2023-02-19 02:12:29 +01:00
|
|
|
}
|
2022-09-22 02:48:11 +02:00
|
|
|
|
2023-02-19 02:12:29 +01:00
|
|
|
if (stateHelper.certsDir.length > 0 && fs.existsSync(stateHelper.certsDir)) {
|
|
|
|
await core.group(`Cleaning up certificates`, async () => {
|
|
|
|
fs.rmSync(stateHelper.certsDir, {recursive: true});
|
|
|
|
});
|
|
|
|
}
|
2022-09-22 02:48:11 +02:00
|
|
|
}
|
2023-02-19 02:12:29 +01:00
|
|
|
);
|