Files
bake-action/src/context.ts

133 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

import {parse} from 'csv-parse/sync';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as tmp from 'tmp';
2020-10-08 00:52:52 +02:00
import * as buildx from './buildx';
import * as core from '@actions/core';
2021-04-30 11:50:06 +02:00
import {issueCommand} from '@actions/core/lib/command';
2020-10-08 00:52:52 +02:00
let _tmpDir: string;
2020-10-08 00:52:52 +02:00
export interface Inputs {
builder: string;
files: string[];
targets: string[];
noCache: boolean;
pull: boolean;
load: boolean;
push: boolean;
set: string[];
source: string;
2020-10-08 00:52:52 +02:00
}
export function tmpDir(): string {
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);
}
2020-10-08 00:52:52 +02:00
export async function getInputs(): Promise<Inputs> {
return {
builder: core.getInput('builder'),
files: getInputList('files'),
targets: getInputList('targets'),
2021-05-26 15:22:18 +02:00
noCache: core.getBooleanInput('no-cache'),
pull: core.getBooleanInput('pull'),
load: core.getBooleanInput('load'),
push: core.getBooleanInput('push'),
set: getInputList('set', true),
source: core.getInput('source')
2020-10-08 00:52:52 +02:00
};
}
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
// prettier-ignore
return [
...await getBakeArgs(inputs, buildxVersion),
...await getCommonArgs(inputs),
...inputs.targets
];
2020-10-08 00:52:52 +02:00
}
async function getBakeArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
const args: Array<string> = ['bake'];
if (inputs.source) {
args.push(inputs.source);
}
2020-10-08 00:52:52 +02:00
await asyncForEach(inputs.files, async file => {
args.push('--file', file);
});
await asyncForEach(inputs.set, async set => {
args.push('--set', set);
});
if (buildx.satisfies(buildxVersion, '>=0.6.0')) {
args.push('--metadata-file', await buildx.getMetadataFile());
}
2020-10-08 00:52:52 +02:00
return args;
}
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
const args: Array<string> = [];
2020-10-08 00:52:52 +02:00
if (inputs.noCache) {
args.push('--no-cache');
}
if (inputs.builder) {
args.push('--builder', inputs.builder);
}
if (inputs.pull) {
args.push('--pull');
}
if (inputs.load) {
args.push('--load');
}
if (inputs.push) {
args.push('--push');
}
return args;
}
export function getInputList(name: string, ignoreComma?: boolean): string[] {
const res: Array<string> = [];
2020-10-08 00:52:52 +02:00
const items = core.getInput(name);
if (items == '') {
return res;
}
const records = parse(items, {
columns: false,
relaxColumnCount: true,
skipEmptyLines: true
});
for (const record of records as Array<string[]>) {
if (record.length == 1) {
res.push(record[0]);
continue;
} else if (!ignoreComma) {
res.push(...record);
continue;
}
res.push(record.join(','));
2020-10-08 00:52:52 +02:00
}
return res.filter(item => item).map(pat => pat.trim());
2020-10-08 00:52:52 +02:00
}
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};
2021-04-30 11:50:06 +02:00
// FIXME: Temp fix https://github.com/actions/toolkit/issues/777
export function setOutput(name: string, value: unknown): void {
2021-04-30 11:50:06 +02:00
issueCommand('set-output', {name}, value);
}