2020-08-16 11:18:08 -04:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
|
|
|
|
export interface Inputs {
|
|
|
|
context: string;
|
|
|
|
file: string;
|
|
|
|
buildArgs: string[];
|
|
|
|
labels: string[];
|
|
|
|
tags: string[];
|
|
|
|
pull: boolean;
|
|
|
|
target: string;
|
2020-08-16 20:32:27 -04:00
|
|
|
allow: string[];
|
2020-08-16 11:18:08 -04:00
|
|
|
noCache: boolean;
|
|
|
|
builder: string;
|
2020-08-16 20:32:27 -04:00
|
|
|
platforms: string[];
|
2020-08-16 11:18:08 -04:00
|
|
|
load: boolean;
|
|
|
|
push: boolean;
|
|
|
|
outputs: string[];
|
|
|
|
cacheFrom: string[];
|
|
|
|
cacheTo: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getInputs(): Promise<Inputs> {
|
|
|
|
return {
|
2020-08-17 11:31:15 -04:00
|
|
|
context: core.getInput('context') || '.',
|
2020-08-16 11:18:08 -04:00
|
|
|
file: core.getInput('file') || './Dockerfile',
|
|
|
|
buildArgs: await getInputList('build-args'),
|
|
|
|
labels: await getInputList('labels'),
|
|
|
|
tags: await getInputList('tags'),
|
|
|
|
pull: /true/i.test(core.getInput('pull')),
|
|
|
|
target: core.getInput('target'),
|
2020-08-16 20:32:27 -04:00
|
|
|
allow: await getInputList('allow'),
|
2020-08-16 11:18:08 -04:00
|
|
|
noCache: /true/i.test(core.getInput('no-cache')),
|
|
|
|
builder: core.getInput('builder'),
|
2020-08-16 20:32:27 -04:00
|
|
|
platforms: await getInputList('platforms'),
|
2020-08-16 11:18:08 -04:00
|
|
|
load: /true/i.test(core.getInput('load')),
|
|
|
|
push: /true/i.test(core.getInput('push')),
|
|
|
|
outputs: await getInputList('outputs'),
|
|
|
|
cacheFrom: await getInputList('cache-from'),
|
2020-08-17 12:35:15 -04:00
|
|
|
cacheTo: await getInputList('cache-to')
|
2020-08-16 11:18:08 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:24:31 -04:00
|
|
|
export async function getArgs(inputs: Inputs): Promise<Array<string>> {
|
2020-08-16 11:18:08 -04:00
|
|
|
let args: Array<string> = ['buildx'];
|
2020-08-17 12:26:35 -04:00
|
|
|
args.push.apply(args, await getBuildArgs(inputs));
|
2020-08-16 11:24:31 -04:00
|
|
|
args.push.apply(args, await getCommonArgs(inputs));
|
2020-08-17 12:26:35 -04:00
|
|
|
args.push(inputs.context);
|
2020-08-16 11:18:08 -04:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:24:31 -04:00
|
|
|
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
|
2020-08-16 11:18:08 -04:00
|
|
|
let args: Array<string> = [];
|
|
|
|
if (inputs.noCache) {
|
|
|
|
args.push('--no-cache');
|
|
|
|
}
|
|
|
|
if (inputs.pull) {
|
|
|
|
args.push('--pull');
|
|
|
|
}
|
|
|
|
if (inputs.load) {
|
|
|
|
args.push('--load');
|
|
|
|
}
|
|
|
|
if (inputs.push) {
|
|
|
|
args.push('--push');
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-16 11:24:31 -04:00
|
|
|
async function getBuildArgs(inputs: Inputs): Promise<Array<string>> {
|
2020-08-16 11:18:08 -04:00
|
|
|
let args: Array<string> = ['build'];
|
|
|
|
await asyncForEach(inputs.buildArgs, async buildArg => {
|
|
|
|
args.push('--build-arg', buildArg);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.labels, async label => {
|
|
|
|
args.push('--label', label);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.tags, async tag => {
|
|
|
|
args.push('--tag', tag);
|
|
|
|
});
|
|
|
|
if (inputs.target) {
|
|
|
|
args.push('--target', inputs.target);
|
|
|
|
}
|
|
|
|
if (inputs.allow) {
|
2020-08-16 20:32:27 -04:00
|
|
|
args.push('--allow', inputs.allow.join(','));
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
if (inputs.platforms) {
|
2020-08-16 20:32:27 -04:00
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
await asyncForEach(inputs.outputs, async output => {
|
|
|
|
args.push('--output', output);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
|
|
|
args.push('--cache-from', cacheFrom);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
2020-08-16 16:43:08 -04:00
|
|
|
args.push('--cache-to', cacheTo);
|
2020-08-16 11:18:08 -04:00
|
|
|
});
|
|
|
|
if (inputs.file) {
|
|
|
|
args.push('--file', inputs.file);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:32:27 -04:00
|
|
|
export async function getInputList(name: string): Promise<string[]> {
|
2020-08-16 11:18:08 -04:00
|
|
|
const items = core.getInput(name);
|
|
|
|
if (items == '') {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return items.split(/\r?\n/).reduce<string[]>((acc, line) => acc.concat(line.split(',')).map(pat => pat.trim()), []);
|
|
|
|
}
|
|
|
|
|
2020-08-16 20:32:27 -04:00
|
|
|
export const asyncForEach = async (array, callback) => {
|
2020-08-16 11:18:08 -04:00
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
};
|