2020-09-02 08:07:11 +00:00
|
|
|
import * as fs from 'fs';
|
2020-08-15 22:36:41 +00:00
|
|
|
import * as buildx from './buildx';
|
2020-09-02 08:07:11 +00:00
|
|
|
import * as context from './context';
|
2022-04-28 07:31:47 +00:00
|
|
|
import * as docker from './docker';
|
2020-09-02 08:07:11 +00:00
|
|
|
import * as stateHelper from './state-helper';
|
2020-08-15 22:36:41 +00:00
|
|
|
import * as core from '@actions/core';
|
2021-06-22 17:52:21 +00:00
|
|
|
import * as exec from '@actions/exec';
|
2020-08-15 22:36:41 +00:00
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
2022-04-28 07:31:47 +00:00
|
|
|
const defContext = context.defaultContext();
|
|
|
|
const inputs: context.Inputs = await context.getInputs(defContext);
|
|
|
|
|
|
|
|
// standalone if docker cli not available
|
|
|
|
const standalone = !(await docker.isAvailable());
|
|
|
|
|
2021-04-27 14:16:22 +00:00
|
|
|
core.startGroup(`Docker info`);
|
2022-04-28 07:31:47 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
2021-04-27 14:16:22 +00:00
|
|
|
core.endGroup();
|
2020-08-15 22:36:41 +00:00
|
|
|
|
2022-04-28 07:31:47 +00:00
|
|
|
if (!(await buildx.isAvailable(standalone))) {
|
2021-04-27 14:16:22 +00:00
|
|
|
core.setFailed(`Docker buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
|
|
|
return;
|
2020-08-15 22:36:41 +00:00
|
|
|
}
|
2020-10-19 19:17:06 +00:00
|
|
|
stateHelper.setTmpDir(context.tmpDir());
|
2020-08-15 22:36:41 +00:00
|
|
|
|
2022-04-28 07:31:47 +00:00
|
|
|
const buildxVersion = await buildx.getVersion(standalone);
|
|
|
|
await core.group(`Buildx version`, async () => {
|
|
|
|
const versionCmd = buildx.getCommand(['version'], standalone);
|
|
|
|
await exec.exec(versionCmd.command, versionCmd.args, {
|
|
|
|
failOnStdErr: false
|
|
|
|
});
|
|
|
|
});
|
2020-08-16 20:31:37 +00:00
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
const args: string[] = await context.getArgs(inputs, defContext, buildxVersion);
|
2022-04-28 07:31:47 +00:00
|
|
|
const buildCmd = buildx.getCommand(args, standalone);
|
2021-06-22 17:52:21 +00:00
|
|
|
await exec
|
2022-04-28 07:31:47 +00:00
|
|
|
.getExecOutput(buildCmd.command, buildCmd.args, {
|
2021-06-22 17:52:21 +00:00
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
2022-03-15 20:59:52 +00:00
|
|
|
throw new Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
|
2021-06-22 17:52:21 +00:00
|
|
|
}
|
|
|
|
});
|
2020-08-17 20:18:15 +00:00
|
|
|
|
2022-02-09 10:32:35 +00:00
|
|
|
const imageID = await buildx.getImageID();
|
2022-03-14 18:30:50 +00:00
|
|
|
const metadata = await buildx.getMetadata();
|
|
|
|
const digest = await buildx.getDigest(metadata);
|
|
|
|
|
2022-02-09 10:32:35 +00:00
|
|
|
if (imageID) {
|
2022-03-14 18:30:50 +00:00
|
|
|
await core.group(`ImageID`, async () => {
|
2022-02-09 10:32:35 +00:00
|
|
|
core.info(imageID);
|
2022-10-12 04:56:31 +00:00
|
|
|
core.setOutput('imageid', imageID);
|
2022-03-14 18:30:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (digest) {
|
|
|
|
await core.group(`Digest`, async () => {
|
|
|
|
core.info(digest);
|
2022-10-12 04:56:31 +00:00
|
|
|
core.setOutput('digest', digest);
|
2022-02-09 10:32:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (metadata) {
|
2022-03-14 18:30:50 +00:00
|
|
|
await core.group(`Metadata`, async () => {
|
2022-02-09 10:32:35 +00:00
|
|
|
core.info(metadata);
|
2022-10-12 04:56:31 +00:00
|
|
|
core.setOutput('metadata', metadata);
|
2022-02-09 10:32:35 +00:00
|
|
|
});
|
|
|
|
}
|
2020-08-15 22:36:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 08:07:11 +00:00
|
|
|
async function cleanup(): Promise<void> {
|
|
|
|
if (stateHelper.tmpDir.length > 0) {
|
2021-04-27 14:16:22 +00:00
|
|
|
core.startGroup(`Removing temp folder ${stateHelper.tmpDir}`);
|
2022-07-27 23:36:32 +00:00
|
|
|
fs.rmSync(stateHelper.tmpDir, {recursive: true});
|
2021-04-27 14:16:22 +00:00
|
|
|
core.endGroup();
|
2020-09-02 08:07:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|