1
0
Files
build-push-action/src/main.ts
T

60 lines
1.7 KiB
TypeScript
Raw Normal View History

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