2020-08-17 16:18:15 -04:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-08-22 21:31:38 -04:00
|
|
|
import * as semver from 'semver';
|
2020-10-19 15:17:06 -04:00
|
|
|
import * as tmp from 'tmp';
|
2020-08-17 16:18:15 -04:00
|
|
|
import * as buildx from './buildx';
|
2020-08-16 11:18:08 -04:00
|
|
|
import * as core from '@actions/core';
|
2020-09-02 04:07:11 -04:00
|
|
|
import * as github from '@actions/github';
|
2020-08-16 11:18:08 -04:00
|
|
|
|
|
|
|
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[];
|
2020-09-02 04:07:11 -04:00
|
|
|
secrets: string[];
|
2020-09-22 14:49:18 -04:00
|
|
|
githubToken: string;
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
|
2020-10-19 15:17:06 -04:00
|
|
|
export function defaultContext(): string {
|
|
|
|
return `https://github.com/${github.context.repo.owner}/${
|
|
|
|
github.context.repo.repo
|
|
|
|
}.git#${github.context?.ref?.replace(/^refs\//, '')}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpDir(): string {
|
|
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpNameSync(options?: tmp.TmpNameOptions): string {
|
|
|
|
return tmp.tmpNameSync(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getInputs(defaultContext: string): Promise<Inputs> {
|
2020-08-16 11:18:08 -04:00
|
|
|
return {
|
2020-09-22 14:49:18 -04:00
|
|
|
context: core.getInput('context') || defaultContext,
|
2020-09-02 04:07:11 -04:00
|
|
|
file: core.getInput('file') || 'Dockerfile',
|
2020-10-20 13:04:54 -04:00
|
|
|
buildArgs: await getInputList('build-args', true),
|
|
|
|
labels: await getInputList('labels', true),
|
2020-08-16 11:18:08 -04:00
|
|
|
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')),
|
2020-08-29 11:15:26 -04:00
|
|
|
outputs: await getInputList('outputs', true),
|
|
|
|
cacheFrom: await getInputList('cache-from', true),
|
2020-09-02 04:07:11 -04:00
|
|
|
cacheTo: await getInputList('cache-to', true),
|
2020-09-22 14:49:18 -04:00
|
|
|
secrets: await getInputList('secrets', true),
|
|
|
|
githubToken: core.getInput('github-token')
|
2020-08-16 11:18:08 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-19 15:17:06 -04:00
|
|
|
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
|
2020-08-16 11:18:08 -04:00
|
|
|
let args: Array<string> = ['buildx'];
|
2020-10-19 15:17:06 -04:00
|
|
|
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
|
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-10-19 15:17:06 -04:00
|
|
|
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): 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);
|
|
|
|
}
|
2020-08-17 16:18:15 -04:00
|
|
|
if (inputs.allow.length > 0) {
|
2020-08-16 20:32:27 -04:00
|
|
|
args.push('--allow', inputs.allow.join(','));
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
2020-08-17 16:18:15 -04:00
|
|
|
if (inputs.platforms.length > 0) {
|
2020-08-16 20:32:27 -04:00
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
2020-08-22 21:31:38 -04:00
|
|
|
}
|
2020-08-16 11:18:08 -04:00
|
|
|
await asyncForEach(inputs.outputs, async output => {
|
|
|
|
args.push('--output', output);
|
|
|
|
});
|
2020-10-19 15:17:06 -04:00
|
|
|
// TODO: Remove platforms length cond when buildx >0.4.2 available on runner (docker/buildx#351)
|
2020-10-19 16:12:33 -04:00
|
|
|
if (
|
|
|
|
inputs.platforms.length == 0 &&
|
|
|
|
!buildx.isLocalOrTarExporter(inputs.outputs) &&
|
|
|
|
semver.satisfies(buildxVersion, '>=0.4.2')
|
|
|
|
) {
|
2020-10-19 15:17:06 -04:00
|
|
|
args.push('--iidfile', await buildx.getImageIDFile());
|
|
|
|
}
|
2020-08-16 11:18:08 -04:00
|
|
|
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
|
|
|
});
|
2020-09-02 04:07:11 -04:00
|
|
|
await asyncForEach(inputs.secrets, async secret => {
|
|
|
|
args.push('--secret', await buildx.getSecret(secret));
|
|
|
|
});
|
2020-10-19 16:12:33 -04:00
|
|
|
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) {
|
2020-09-22 14:49:18 -04:00
|
|
|
args.push('--secret', await buildx.getSecret(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
|
|
|
|
}
|
2020-08-16 11:18:08 -04:00
|
|
|
if (inputs.file) {
|
|
|
|
args.push('--file', inputs.file);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:18:15 -04:00
|
|
|
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
|
|
|
|
let args: Array<string> = [];
|
|
|
|
if (inputs.noCache) {
|
|
|
|
args.push('--no-cache');
|
|
|
|
}
|
2020-09-03 05:49:39 -04:00
|
|
|
if (inputs.builder) {
|
|
|
|
args.push('--builder', inputs.builder);
|
|
|
|
}
|
2020-08-17 16:18:15 -04:00
|
|
|
if (inputs.pull) {
|
|
|
|
args.push('--pull');
|
|
|
|
}
|
|
|
|
if (inputs.load) {
|
|
|
|
args.push('--load');
|
|
|
|
}
|
|
|
|
if (inputs.push) {
|
|
|
|
args.push('--push');
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-29 11:15:26 -04:00
|
|
|
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
|
2020-08-16 11:18:08 -04:00
|
|
|
const items = core.getInput(name);
|
|
|
|
if (items == '') {
|
|
|
|
return [];
|
|
|
|
}
|
2020-08-29 11:15:26 -04:00
|
|
|
return items
|
|
|
|
.split(/\r?\n/)
|
2020-09-04 20:52:09 -04:00
|
|
|
.filter(x => x)
|
|
|
|
.reduce<string[]>(
|
|
|
|
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()),
|
|
|
|
[]
|
|
|
|
);
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|