2020-08-15 22:36:41 +00:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as buildx from './buildx';
|
|
|
|
import {Inputs, loadInputs, mustBuildx} from './context-helper';
|
2020-08-15 23:50:46 +00:00
|
|
|
import {Image, parseImage} from './docker';
|
2020-08-15 22:36:41 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
|
|
|
|
|
|
|
async function run(): Promise<void> {
|
|
|
|
try {
|
|
|
|
if (os.platform() !== 'linux') {
|
|
|
|
core.setFailed('Only supported on linux platform');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const inputs: Inputs = await loadInputs();
|
2020-08-16 00:30:03 +00:00
|
|
|
const buildxAvailable = await buildx.isAvailable();
|
|
|
|
const buildxInstalled = buildxAvailable && (await buildx.isInstalled());
|
|
|
|
const buildxEnabled = (await mustBuildx(inputs)) || buildxInstalled;
|
2020-08-15 22:36:41 +00:00
|
|
|
let buildArgs: Array<string> = [];
|
|
|
|
|
|
|
|
// Check buildx
|
2020-08-15 23:23:25 +00:00
|
|
|
if (buildxEnabled) {
|
2020-08-16 00:30:03 +00:00
|
|
|
if (!buildxAvailable) {
|
2020-08-15 23:23:25 +00:00
|
|
|
core.setFailed(`Buildx is required but not available`);
|
|
|
|
return;
|
2020-08-15 22:36:41 +00:00
|
|
|
}
|
|
|
|
core.info(`🚀 Buildx will be used to build your image`);
|
|
|
|
buildArgs.push('buildx', 'build');
|
|
|
|
} else {
|
|
|
|
buildArgs.push('build');
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:23:25 +00:00
|
|
|
// Global options
|
2020-08-15 22:36:41 +00:00
|
|
|
if (inputs.file) {
|
|
|
|
buildArgs.push('--file', inputs.file);
|
|
|
|
}
|
|
|
|
await asyncForEach(inputs.buildArgs, async buildArg => {
|
|
|
|
buildArgs.push('--build-arg', buildArg);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.labels, async label => {
|
|
|
|
buildArgs.push('--label', label);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.tags, async tag => {
|
|
|
|
buildArgs.push('--tag', tag);
|
|
|
|
});
|
|
|
|
if (inputs.pull) {
|
|
|
|
buildArgs.push('--pull');
|
|
|
|
}
|
|
|
|
if (inputs.target) {
|
|
|
|
buildArgs.push('--target', inputs.target);
|
|
|
|
}
|
|
|
|
if (inputs.noCache) {
|
|
|
|
buildArgs.push('--no-cache');
|
|
|
|
}
|
2020-08-15 23:23:25 +00:00
|
|
|
|
|
|
|
// Buildx options
|
|
|
|
if (buildxEnabled) {
|
|
|
|
if (inputs.builder) {
|
2020-08-16 02:20:04 +00:00
|
|
|
core.info(`📌 Using builder instance ${inputs.builder}`);
|
2020-08-15 23:23:25 +00:00
|
|
|
await buildx.use(inputs.builder);
|
|
|
|
}
|
|
|
|
if (inputs.platforms) {
|
|
|
|
buildArgs.push('--platform', inputs.platforms);
|
|
|
|
}
|
|
|
|
if (inputs.load) {
|
|
|
|
buildArgs.push('--load');
|
|
|
|
}
|
|
|
|
if (inputs.push) {
|
|
|
|
buildArgs.push('--push');
|
|
|
|
}
|
|
|
|
await asyncForEach(inputs.outputs, async output => {
|
|
|
|
buildArgs.push('--output', output);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
|
|
|
buildArgs.push('--cache-from', cacheFrom);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
|
|
|
buildArgs.push('--cache-from', cacheTo);
|
|
|
|
});
|
2020-08-15 22:36:41 +00:00
|
|
|
}
|
2020-08-15 23:23:25 +00:00
|
|
|
|
2020-08-15 23:06:24 +00:00
|
|
|
buildArgs.push(inputs.context);
|
2020-08-15 22:36:41 +00:00
|
|
|
|
|
|
|
core.info(`🏃 Starting build...`);
|
|
|
|
await exec.exec('docker', buildArgs);
|
2020-08-15 23:23:25 +00:00
|
|
|
|
|
|
|
if (!buildxEnabled && inputs.push) {
|
|
|
|
let pushRepos: Array<string> = [];
|
|
|
|
await asyncForEach(inputs.tags, async tag => {
|
2020-08-15 23:50:46 +00:00
|
|
|
const img: Image | undefined = await parseImage(tag);
|
|
|
|
if (!img) {
|
|
|
|
core.warning(`Cannot parse image reference ${tag}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const repo: string = `${img.registry}${img.namespace}${img.repository}`;
|
2020-08-15 23:23:25 +00:00
|
|
|
if (!pushRepos.includes(repo)) {
|
|
|
|
pushRepos.push(repo);
|
|
|
|
core.info(`⬆️ Pushing ${repo}...`);
|
|
|
|
await exec.exec('docker', ['push', repo]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-08-15 22:36:41 +00:00
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const asyncForEach = async (array, callback) => {
|
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
run();
|