1
0
Files
build-push-action/src/context.ts
T

219 lines
6.4 KiB
TypeScript
Raw Normal View History

2020-11-17 21:38:45 +01:00
import csvparse from 'csv-parse/lib/sync';
2020-08-17 22:18:15 +02:00
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as tmp from 'tmp';
2020-09-11 11:23:49 +12:00
import * as core from '@actions/core';
2021-04-27 16:30:22 +02:00
import {issueCommand} from '@actions/core/lib/command';
2020-09-02 10:07:11 +02:00
import * as github from '@actions/github';
2020-09-11 11:23:49 +12:00
import * as buildx from './buildx';
2020-10-21 02:46:41 +02:00
let _defaultContext, _tmpDir: string;
export interface Inputs {
2021-04-06 13:54:58 +02:00
allow: string[];
buildArgs: string[];
builder: string;
cacheFrom: string[];
cacheTo: string[];
context: string;
file: string;
labels: string[];
2021-04-06 13:54:58 +02:00
load: boolean;
2021-04-06 14:49:15 +02:00
network: string;
noCache: boolean;
2021-04-06 13:54:58 +02:00
outputs: string[];
2020-08-17 02:32:27 +02:00
platforms: string[];
2021-04-06 13:54:58 +02:00
pull: boolean;
push: boolean;
2020-09-02 10:07:11 +02:00
secrets: string[];
2021-02-15 10:08:19 +01:00
secretFiles: string[];
2020-09-11 11:23:49 +12:00
ssh: string[];
2021-04-06 13:54:58 +02:00
tags: string[];
target: string;
githubToken: string;
}
export function defaultContext(): string {
2020-10-21 02:46:41 +02:00
if (!_defaultContext) {
2021-04-26 11:02:09 +02:00
let ref = github.context.ref;
if (github.context.sha && ref && !ref.startsWith('refs/')) {
ref = `refs/heads/${github.context.ref}`;
}
if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
ref = github.context.sha;
}
2021-07-01 15:29:36 +02:00
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}.git#${ref}`;
2020-10-21 02:46:41 +02:00
}
return _defaultContext;
}
export function tmpDir(): string {
2020-10-21 02:46:41 +02:00
if (!_tmpDir) {
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep);
}
return _tmpDir;
}
export function tmpNameSync(options?: tmp.TmpNameOptions): string {
return tmp.tmpNameSync(options);
}
export async function getInputs(defaultContext: string): Promise<Inputs> {
return {
2021-04-06 13:54:58 +02: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),
context: core.getInput('context') || defaultContext,
file: core.getInput('file'),
labels: await getInputList('labels', true),
2021-06-22 17:25:52 +02:00
load: core.getBooleanInput('load'),
2021-04-06 14:49:15 +02:00
network: core.getInput('network'),
2021-06-22 17:25:52 +02:00
noCache: core.getBooleanInput('no-cache'),
2021-04-06 13:54:58 +02:00
outputs: await getInputList('outputs', true),
2020-08-17 02:32:27 +02:00
platforms: await getInputList('platforms'),
2021-06-22 17:25:52 +02:00
pull: core.getBooleanInput('pull'),
push: core.getBooleanInput('push'),
secrets: await getInputList('secrets', true),
2021-02-15 10:08:19 +01:00
secretFiles: await getInputList('secret-files', true),
2021-04-06 13:54:58 +02:00
ssh: await getInputList('ssh'),
tags: await getInputList('tags'),
target: core.getInput('target'),
githubToken: core.getInput('github-token')
};
}
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx'];
args.push.apply(args, await getBuildArgs(inputs, defaultContext, buildxVersion));
2021-11-16 05:19:44 +01:00
args.push.apply(args, await getCommonArgs(inputs, buildxVersion));
args.push(inputs.context);
return args;
}
async function getBuildArgs(inputs: Inputs, defaultContext: string, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['build'];
2021-11-16 05:19:44 +01:00
if (inputs.allow.length > 0) {
args.push('--allow', inputs.allow.join(','));
}
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
});
2021-11-16 05:19:44 +01:00
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
2021-11-16 05:19:44 +01:00
await asyncForEach(inputs.cacheTo, async cacheTo => {
args.push('--cache-to', cacheTo);
});
2021-11-16 05:19:44 +01:00
if (inputs.file) {
args.push('--file', inputs.file);
2020-08-23 03:31:38 +02:00
}
2021-07-01 15:29:36 +02:00
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
args.push('--iidfile', await buildx.getImageIDFile());
}
2021-11-16 05:19:44 +01:00
await asyncForEach(inputs.labels, async label => {
args.push('--label', label);
});
2021-11-16 05:19:44 +01:00
await asyncForEach(inputs.outputs, async output => {
args.push('--output', output);
});
2021-11-16 05:19:44 +01:00
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
}
2020-09-02 10:07:11 +02:00
await asyncForEach(inputs.secrets, async secret => {
2020-11-17 21:38:45 +01:00
try {
2021-02-15 10:08:19 +01: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 21:38:45 +01:00
} catch (err) {
core.warning(err.message);
}
2020-09-02 10:07:11 +02:00
});
2020-10-19 22:12:33 +02:00
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && inputs.context == defaultContext) {
2021-02-15 10:08:19 +01:00
args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
}
2020-09-11 11:23:49 +12:00
await asyncForEach(inputs.ssh, async ssh => {
args.push('--ssh', ssh);
});
2021-11-16 05:19:44 +01:00
await asyncForEach(inputs.tags, async tag => {
args.push('--tag', tag);
});
if (inputs.target) {
args.push('--target', inputs.target);
}
return args;
}
2021-11-16 05:19:44 +01:00
async function getCommonArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
2020-08-17 22:18:15 +02:00
let args: Array<string> = [];
2020-09-03 11:49:39 +02:00
if (inputs.builder) {
args.push('--builder', inputs.builder);
}
2020-08-17 22:18:15 +02:00
if (inputs.load) {
args.push('--load');
}
2021-11-16 05:19:44 +01:00
if (buildx.satisfies(buildxVersion, '>=0.6.0')) {
args.push('--metadata-file', await buildx.getMetadataFile());
}
2021-04-06 14:49:15 +02:00
if (inputs.network) {
args.push('--network', inputs.network);
}
2021-11-16 05:19:44 +01:00
if (inputs.noCache) {
args.push('--no-cache');
}
if (inputs.pull) {
args.push('--pull');
}
2020-08-17 22:18:15 +02:00
if (inputs.push) {
args.push('--push');
}
return args;
}
2020-08-29 17:15:26 +02:00
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
2020-11-17 21:38:45 +01:00
let res: Array<string> = [];
const items = core.getInput(name);
if (items == '') {
2020-11-17 21:38:45 +01:00
return res;
}
for (let output of (await csvparse(items, {
columns: false,
2021-05-23 02:41:02 +02:00
relax: true,
2020-11-17 21:38:45 +01:00
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 03:40:39 +01:00
return res.filter(item => item).map(pat => pat.trim());
}
2020-08-17 02:32:27 +02:00
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
2021-04-27 16:30:22 +02:00
// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
export function setOutput(name: string, value: any): void {
issueCommand('set-output', {name}, value);
}