2021-07-02 13:02:22 +08:00
|
|
|
import fs from 'fs';
|
2020-09-03 22:23:15 +08:00
|
|
|
import * as os from 'os';
|
2021-07-02 13:02:22 +08:00
|
|
|
import path from 'path';
|
2021-09-04 04:21:20 +08:00
|
|
|
import * as tmp from 'tmp';
|
2022-09-19 17:36:58 +08:00
|
|
|
import * as uuid from 'uuid';
|
2022-09-19 17:34:47 +08:00
|
|
|
import {parse} from 'csv-parse/sync';
|
2022-09-19 17:36:58 +08:00
|
|
|
import * as buildx from './buildx';
|
2022-09-19 17:34:47 +08:00
|
|
|
import * as nodes from './nodes';
|
2020-09-03 22:23:15 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
let _tmpDir: string;
|
2020-09-03 22:23:15 +08:00
|
|
|
export const osPlat: string = os.platform();
|
2020-09-05 22:30:41 +08:00
|
|
|
export const osArch: string = os.arch();
|
2020-09-03 22:23:15 +08:00
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
export function tmpDir(): string {
|
|
|
|
if (!_tmpDir) {
|
|
|
|
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
|
|
|
|
}
|
|
|
|
return _tmpDir;
|
|
|
|
}
|
|
|
|
|
2021-09-04 04:21:20 +08:00
|
|
|
export function tmpNameSync(options?: tmp.TmpNameOptions): string {
|
|
|
|
return tmp.tmpNameSync(options);
|
|
|
|
}
|
|
|
|
|
2020-09-03 22:23:15 +08:00
|
|
|
export interface Inputs {
|
|
|
|
version: string;
|
2022-09-19 17:36:58 +08:00
|
|
|
name: string;
|
2020-09-03 22:23:15 +08:00
|
|
|
driver: string;
|
|
|
|
driverOpts: string[];
|
|
|
|
buildkitdFlags: string;
|
2022-09-22 17:54:00 +08:00
|
|
|
platforms: string[];
|
2020-09-03 22:23:15 +08:00
|
|
|
install: boolean;
|
|
|
|
use: boolean;
|
2020-09-08 21:52:09 +08:00
|
|
|
endpoint: string;
|
2021-04-22 01:37:54 +08:00
|
|
|
config: string;
|
2021-09-04 04:21:20 +08:00
|
|
|
configInline: string;
|
2022-09-19 17:34:47 +08:00
|
|
|
append: string;
|
2020-09-03 22:23:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getInputs(): Promise<Inputs> {
|
|
|
|
return {
|
|
|
|
version: core.getInput('version'),
|
2022-09-19 17:36:58 +08:00
|
|
|
name: getBuilderName(core.getInput('driver') || 'docker-container'),
|
2020-09-03 22:23:15 +08:00
|
|
|
driver: core.getInput('driver') || 'docker-container',
|
|
|
|
driverOpts: await getInputList('driver-opts', true),
|
2021-07-02 13:02:22 +08:00
|
|
|
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
2022-10-18 17:06:34 +08:00
|
|
|
platforms: await getInputList('platforms', false, true),
|
2021-06-23 21:43:25 +08:00
|
|
|
install: core.getBooleanInput('install'),
|
|
|
|
use: core.getBooleanInput('use'),
|
2021-04-22 01:37:54 +08:00
|
|
|
endpoint: core.getInput('endpoint'),
|
2021-09-04 04:21:20 +08:00
|
|
|
config: core.getInput('config'),
|
2022-09-19 17:34:47 +08:00
|
|
|
configInline: core.getInput('config-inline'),
|
2023-01-29 05:21:40 +08:00
|
|
|
append: core.getInput('append')
|
2020-09-03 22:23:15 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:36:58 +08:00
|
|
|
export function getBuilderName(driver: string): string {
|
|
|
|
return driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getCreateArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
|
|
|
|
const args: Array<string> = ['create', '--name', inputs.name, '--driver', inputs.driver];
|
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
|
|
|
await asyncForEach(inputs.driverOpts, async driverOpt => {
|
|
|
|
args.push('--driver-opt', driverOpt);
|
|
|
|
});
|
|
|
|
if (inputs.driver != 'remote' && inputs.buildkitdFlags) {
|
|
|
|
args.push('--buildkitd-flags', inputs.buildkitdFlags);
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 17:54:00 +08:00
|
|
|
if (inputs.platforms.length > 0) {
|
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
|
|
|
}
|
2022-09-19 17:36:58 +08:00
|
|
|
if (inputs.use) {
|
|
|
|
args.push('--use');
|
|
|
|
}
|
|
|
|
if (inputs.driver != 'remote') {
|
|
|
|
if (inputs.config) {
|
|
|
|
args.push('--config', await buildx.getConfigFile(inputs.config));
|
|
|
|
} else if (inputs.configInline) {
|
|
|
|
args.push('--config', await buildx.getConfigInline(inputs.configInline));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inputs.endpoint) {
|
|
|
|
args.push(inputs.endpoint);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:34:47 +08:00
|
|
|
export async function getAppendArgs(inputs: Inputs, node: nodes.Node, buildxVersion: string): Promise<Array<string>> {
|
|
|
|
const args: Array<string> = ['create', '--name', inputs.name, '--append'];
|
|
|
|
if (node.name) {
|
|
|
|
args.push('--node', node.name);
|
|
|
|
}
|
|
|
|
if (node['driver-opts'] && buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
|
|
|
await asyncForEach(node['driver-opts'], async driverOpt => {
|
|
|
|
args.push('--driver-opt', driverOpt);
|
|
|
|
});
|
|
|
|
if (inputs.driver != 'remote' && node['buildkitd-flags']) {
|
|
|
|
args.push('--buildkitd-flags', node['buildkitd-flags']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.platforms) {
|
|
|
|
args.push('--platform', node.platforms);
|
|
|
|
}
|
|
|
|
if (node.endpoint) {
|
|
|
|
args.push(node.endpoint);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:36:58 +08:00
|
|
|
export async function getInspectArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
|
|
|
|
const args: Array<string> = ['inspect', '--bootstrap'];
|
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
|
|
|
|
args.push('--builder', inputs.name);
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2022-10-18 17:06:34 +08:00
|
|
|
export async function getInputList(name: string, ignoreComma?: boolean, escapeQuotes?: boolean): Promise<string[]> {
|
2022-09-19 17:34:47 +08:00
|
|
|
const res: Array<string> = [];
|
|
|
|
|
2020-09-03 22:23:15 +08:00
|
|
|
const items = core.getInput(name);
|
|
|
|
if (items == '') {
|
2022-09-19 17:34:47 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const records = parse(items, {
|
|
|
|
columns: false,
|
|
|
|
relaxQuotes: true,
|
|
|
|
comment: '#',
|
|
|
|
relaxColumnCount: true,
|
2022-10-18 02:44:03 +08:00
|
|
|
skipEmptyLines: true,
|
2022-10-18 17:06:34 +08:00
|
|
|
quote: escapeQuotes ? `"` : false
|
2022-09-19 17:34:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
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-09-03 22:23:15 +08:00
|
|
|
}
|
2022-09-19 17:34:47 +08:00
|
|
|
|
|
|
|
return res.filter(item => item).map(pat => pat.trim());
|
2020-09-03 22:23:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export const asyncForEach = async (array, callback) => {
|
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
};
|