2020-11-17 20:38:45 +00:00
|
|
|
import csvparse from 'csv-parse/lib/sync';
|
2020-08-17 20:18:15 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-08-23 01:31:38 +00:00
|
|
|
import * as semver from 'semver';
|
2020-10-19 19:17:06 +00:00
|
|
|
import * as tmp from 'tmp';
|
2020-09-10 23:23:49 +00:00
|
|
|
|
2020-08-16 15:18:08 +00:00
|
|
|
import * as core from '@actions/core';
|
2020-09-02 08:07:11 +00:00
|
|
|
import * as github from '@actions/github';
|
2020-08-16 15:18:08 +00:00
|
|
|
|
2020-09-10 23:23:49 +00:00
|
|
|
import * as buildx from './buildx';
|
|
|
|
|
2020-10-21 00:46:41 +00:00
|
|
|
let _defaultContext, _tmpDir: string;
|
|
|
|
|
2020-08-16 15:18:08 +00:00
|
|
|
export interface Inputs {
|
2021-04-06 11:54:58 +00:00
|
|
|
allow: string[];
|
|
|
|
buildArgs: string[];
|
|
|
|
builder: string;
|
|
|
|
cacheFrom: string[];
|
|
|
|
cacheTo: string[];
|
2020-08-16 15:18:08 +00:00
|
|
|
context: string;
|
|
|
|
file: string;
|
|
|
|
labels: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
load: boolean;
|
2020-08-16 15:18:08 +00:00
|
|
|
noCache: boolean;
|
2021-04-06 11:54:58 +00:00
|
|
|
outputs: string[];
|
2020-08-17 00:32:27 +00:00
|
|
|
platforms: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
pull: boolean;
|
2020-08-16 15:18:08 +00:00
|
|
|
push: boolean;
|
2020-09-02 08:07:11 +00:00
|
|
|
secrets: string[];
|
2021-02-15 09:08:19 +00:00
|
|
|
secretFiles: string[];
|
2020-09-10 23:23:49 +00:00
|
|
|
ssh: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
tags: string[];
|
|
|
|
target: string;
|
|
|
|
githubToken: string;
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
export function defaultContext(): string {
|
2020-10-21 00:46:41 +00:00
|
|
|
if (!_defaultContext) {
|
2021-04-01 18:07:51 +00:00
|
|
|
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${
|
2020-10-21 00:46:41 +00:00
|
|
|
github.context.repo.repo
|
|
|
|
}.git#${github.context?.ref?.replace(/^refs\//, '')}`;
|
|
|
|
}
|
|
|
|
return _defaultContext;
|
2020-10-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpDir(): string {
|
2020-10-21 00:46:41 +00:00
|
|
|
if (!_tmpDir) {
|
|
|
|
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep);
|
|
|
|
}
|
|
|
|
return _tmpDir;
|
2020-10-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpNameSync(options?: tmp.TmpNameOptions): string {
|
|
|
|
return tmp.tmpNameSync(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getInputs(defaultContext: string): Promise<Inputs> {
|
2020-08-16 15:18:08 +00:00
|
|
|
return {
|
2021-04-06 11:54:58 +00:00
|
|
|
allow: await getInputList('allow'),
|
|
|
|
buildArgs: await getInputList('build-args', true),
|
|
|
|
builder: core.getInput('builder'),
|
|
|
|
cacheFrom: await getInputList('cache-from', true),
|
|
|
|
cacheTo: await getInputList('cache-to', true),
|
2020-09-22 18:49:18 +00:00
|
|
|
context: core.getInput('context') || defaultContext,
|
2020-12-18 15:05:47 +00:00
|
|
|
file: core.getInput('file'),
|
2020-10-20 17:04:54 +00:00
|
|
|
labels: await getInputList('labels', true),
|
2021-04-06 11:54:58 +00:00
|
|
|
load: /true/i.test(core.getInput('load')),
|
2020-08-16 15:18:08 +00:00
|
|
|
noCache: /true/i.test(core.getInput('no-cache')),
|
2021-04-06 11:54:58 +00:00
|
|
|
outputs: await getInputList('outputs', true),
|
2020-08-17 00:32:27 +00:00
|
|
|
platforms: await getInputList('platforms'),
|
2021-04-06 11:54:58 +00:00
|
|
|
pull: /true/i.test(core.getInput('pull')),
|
2020-08-16 15:18:08 +00:00
|
|
|
push: /true/i.test(core.getInput('push')),
|
2020-09-22 18:49:18 +00:00
|
|
|
secrets: await getInputList('secrets', true),
|
2021-02-15 09:08:19 +00:00
|
|
|
secretFiles: await getInputList('secret-files', true),
|
2021-04-06 11:54:58 +00:00
|
|
|
ssh: await getInputList('ssh'),
|
|
|
|
tags: await getInputList('tags'),
|
|
|
|
target: core.getInput('target'),
|
|
|
|
githubToken: core.getInput('github-token')
|
2020-08-16 15:18:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
|
2020-08-16 15:18:08 +00:00
|
|
|
let args: Array<string> = ['buildx'];
|
2020-10-19 19:17:06 +00:00
|
|
|
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
|
2020-08-16 15:24:31 +00:00
|
|
|
args.push.apply(args, await getCommonArgs(inputs));
|
2020-08-17 16:26:35 +00:00
|
|
|
args.push(inputs.context);
|
2020-08-16 15:18:08 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
|
2020-08-16 15:18:08 +00: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 20:18:15 +00:00
|
|
|
if (inputs.allow.length > 0) {
|
2020-08-17 00:32:27 +00:00
|
|
|
args.push('--allow', inputs.allow.join(','));
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
2020-08-17 20:18:15 +00:00
|
|
|
if (inputs.platforms.length > 0) {
|
2020-08-17 00:32:27 +00:00
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
2020-08-23 01:31:38 +00:00
|
|
|
}
|
2020-08-16 15:18:08 +00:00
|
|
|
await asyncForEach(inputs.outputs, async output => {
|
|
|
|
args.push('--output', output);
|
|
|
|
});
|
2020-10-19 20:12:33 +00:00
|
|
|
if (
|
|
|
|
!buildx.isLocalOrTarExporter(inputs.outputs) &&
|
2020-10-21 07:51:06 +00:00
|
|
|
(inputs.platforms.length == 0 || semver.satisfies(buildxVersion, '>=0.4.2'))
|
2020-10-19 20:12:33 +00:00
|
|
|
) {
|
2020-10-19 19:17:06 +00:00
|
|
|
args.push('--iidfile', await buildx.getImageIDFile());
|
|
|
|
}
|
2020-08-16 15:18:08 +00:00
|
|
|
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
|
|
|
args.push('--cache-from', cacheFrom);
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
2020-08-16 20:43:08 +00:00
|
|
|
args.push('--cache-to', cacheTo);
|
2020-08-16 15:18:08 +00:00
|
|
|
});
|
2020-09-02 08:07:11 +00:00
|
|
|
await asyncForEach(inputs.secrets, async secret => {
|
2020-11-17 20:38:45 +00:00
|
|
|
try {
|
2021-02-15 09:08:19 +00:00
|
|
|
args.push('--secret', await buildx.getSecretString(secret));
|
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.secretFiles, async secretFile => {
|
|
|
|
try {
|
|
|
|
args.push('--secret', await buildx.getSecretFile(secretFile));
|
2020-11-17 20:38:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
2020-09-02 08:07:11 +00:00
|
|
|
});
|
2020-10-19 20:12:33 +00:00
|
|
|
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) {
|
2021-02-15 09:08:19 +00:00
|
|
|
args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
|
2020-09-22 18:49:18 +00:00
|
|
|
}
|
2020-09-10 23:23:49 +00:00
|
|
|
await asyncForEach(inputs.ssh, async ssh => {
|
|
|
|
args.push('--ssh', ssh);
|
|
|
|
});
|
2020-08-16 15:18:08 +00:00
|
|
|
if (inputs.file) {
|
|
|
|
args.push('--file', inputs.file);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-17 20:18:15 +00:00
|
|
|
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
|
|
|
|
let args: Array<string> = [];
|
|
|
|
if (inputs.noCache) {
|
|
|
|
args.push('--no-cache');
|
|
|
|
}
|
2020-09-03 09:49:39 +00:00
|
|
|
if (inputs.builder) {
|
|
|
|
args.push('--builder', inputs.builder);
|
|
|
|
}
|
2020-08-17 20:18:15 +00:00
|
|
|
if (inputs.pull) {
|
|
|
|
args.push('--pull');
|
|
|
|
}
|
|
|
|
if (inputs.load) {
|
|
|
|
args.push('--load');
|
|
|
|
}
|
|
|
|
if (inputs.push) {
|
|
|
|
args.push('--push');
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-29 15:15:26 +00:00
|
|
|
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
|
2020-11-17 20:38:45 +00:00
|
|
|
let res: Array<string> = [];
|
|
|
|
|
2020-08-16 15:18:08 +00:00
|
|
|
const items = core.getInput(name);
|
|
|
|
if (items == '') {
|
2020-11-17 20:38:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let output of (await csvparse(items, {
|
|
|
|
columns: false,
|
|
|
|
relaxColumnCount: true,
|
|
|
|
skipLinesWithEmptyValues: true
|
|
|
|
})) as Array<string[]>) {
|
|
|
|
if (output.length == 1) {
|
|
|
|
res.push(output[0]);
|
|
|
|
continue;
|
|
|
|
} else if (!ignoreComma) {
|
|
|
|
res.push(...output);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
res.push(output.join(','));
|
|
|
|
}
|
|
|
|
|
2020-12-05 02:40:39 +00:00
|
|
|
return res.filter(item => item).map(pat => pat.trim());
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 00:32:27 +00:00
|
|
|
export const asyncForEach = async (array, callback) => {
|
2020-08-16 15:18:08 +00:00
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
};
|