driver-opt as array of inputs (renamed driver-opts)

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-09-03 16:23:15 +02:00
parent f96a630d38
commit d479e0f33a
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
8 changed files with 239 additions and 57 deletions

View file

@ -1,15 +1,13 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as util from 'util';
import * as context from './context';
import * as exec from './exec';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
const osPlat: string = os.platform();
export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {
@ -65,7 +63,7 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
fs.mkdirSync(pluginsDir, {recursive: true});
}
const filename: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const pluginPath: string = path.join(pluginsDir, filename);
core.debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, filename), pluginPath);
@ -78,10 +76,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
async function download(version: string): Promise<string> {
version = semver.clean(version) || '';
const platform: string = osPlat == 'win32' ? 'windows' : osPlat;
const ext: string = osPlat == 'win32' ? '.exe' : '';
const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat;
const ext: string = context.osPlat == 'win32' ? '.exe' : '';
const filename: string = util.format('buildx-v%s.%s-amd64%s', version, platform, ext);
const targetFile: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename);
let downloadPath: string;

40
src/context.ts Normal file
View file

@ -0,0 +1,40 @@
import * as os from 'os';
import * as core from '@actions/core';
export const osPlat: string = os.platform();
export interface Inputs {
version: string;
driver: string;
driverOpts: string[];
buildkitdFlags: string;
install: boolean;
use: boolean;
}
export async function getInputs(): Promise<Inputs> {
return {
version: core.getInput('version'),
driver: core.getInput('driver') || 'docker-container',
driverOpts: await getInputList('driver-opts', true),
buildkitdFlags: core.getInput('buildkitd-flags'),
install: /true/i.test(core.getInput('install')),
use: /true/i.test(core.getInput('use'))
};
}
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.reduce<string[]>((acc, line) => acc.concat(!ignoreComma ? line.split(',') : line).map(pat => pat.trim()), []);
}
export const asyncForEach = async (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
};

View file

@ -3,6 +3,7 @@ import * as exec from '@actions/exec';
import * as os from 'os';
import * as path from 'path';
import * as buildx from './buildx';
import * as context from './context';
import * as mexec from './exec';
import * as stateHelper from './state-helper';
@ -13,48 +14,40 @@ async function run(): Promise<void> {
return;
}
const bxVersion: string = core.getInput('version');
const bxDriver: string = core.getInput('driver') || 'docker-container';
const bxDriverOpt: string = core.getInput('driver-opt');
const bxBuildkitdFlags: string = core.getInput('buildkitd-flags');
const bxInstall: boolean = /true/i.test(core.getInput('install'));
const bxUse: boolean = /true/i.test(core.getInput('use'));
const inputs: context.Inputs = await context.getInputs();
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
if (!(await buildx.isAvailable()) || bxVersion) {
await buildx.install(bxVersion || 'latest', dockerConfigHome);
if (!(await buildx.isAvailable()) || inputs.version) {
await buildx.install(inputs.version || 'latest', dockerConfigHome);
}
core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version']);
const builderName: string =
bxDriver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`;
inputs.driver == 'docker' ? 'default' : `builder-${process.env.GITHUB_JOB}-${(await buildx.countBuilders()) + 1}`;
core.setOutput('name', builderName);
stateHelper.setBuilderName(builderName);
if (bxDriver != 'docker') {
if (inputs.driver !== 'docker') {
core.info('🔨 Creating a new builder instance...');
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', bxDriver];
if (bxDriverOpt) {
createArgs.push('--driver-opt', bxDriverOpt);
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
createArgs.push('--driver-opt', driverOpt);
});
if (inputs.buildkitdFlags) {
createArgs.push('--buildkitd-flags', inputs.buildkitdFlags);
}
if (bxBuildkitdFlags) {
createArgs.push('--buildkitd-flags', bxBuildkitdFlags);
}
if (bxUse) {
if (inputs.use) {
createArgs.push('--use');
}
await exec.exec('docker', createArgs);
core.info('🏃 Booting builder...');
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
}
if (bxInstall) {
if (inputs.install) {
core.info('🤝 Setting buildx as default builder...');
await exec.exec('docker', ['buildx', 'install']);
}