2020-09-02 08:07:11 +00:00
|
|
|
import * as fs from 'fs';
|
2020-08-15 22:36:41 +00:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as buildx from './buildx';
|
2020-09-02 08:07:11 +00:00
|
|
|
import * as context from './context';
|
2020-10-21 19:07:52 +00:00
|
|
|
import * as exec from './exec';
|
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';
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
|
|
|
if (os.platform() !== 'linux') {
|
2020-10-21 19:07:52 +00:00
|
|
|
throw new Error(`Only supported on linux platform`);
|
2020-08-15 22:36:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-16 03:53:50 +00:00
|
|
|
if (!(await buildx.isAvailable())) {
|
2020-10-21 19:07:52 +00:00
|
|
|
throw new Error(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
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
|
|
|
|
2020-08-23 01:31:38 +00:00
|
|
|
const buildxVersion = await buildx.getVersion();
|
|
|
|
core.info(`📣 Buildx version: ${buildxVersion}`);
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
const defContext = context.defaultContext();
|
|
|
|
let inputs: context.Inputs = await context.getInputs(defContext);
|
2020-08-16 20:31:37 +00:00
|
|
|
|
2020-08-15 22:36:41 +00:00
|
|
|
core.info(`🏃 Starting build...`);
|
2020-10-19 19:17:06 +00:00
|
|
|
const args: string[] = await context.getArgs(inputs, defContext, buildxVersion);
|
2020-10-21 19:07:52 +00: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 20:18:15 +00:00
|
|
|
|
|
|
|
const imageID = await buildx.getImageID();
|
|
|
|
if (imageID) {
|
|
|
|
core.info('🛒 Extracting digest...');
|
|
|
|
core.info(`${imageID}`);
|
|
|
|
core.setOutput('digest', imageID);
|
|
|
|
}
|
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) {
|
|
|
|
core.info(`🚿 Removing temp folder ${stateHelper.tmpDir}`);
|
|
|
|
fs.rmdirSync(stateHelper.tmpDir, {recursive: true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stateHelper.IsPost) {
|
|
|
|
run();
|
|
|
|
} else {
|
|
|
|
cleanup();
|
|
|
|
}
|