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