Initial commit

This commit is contained in:
CrazyMax
2020-10-08 00:52:52 +02:00
commit 2f8da7e49a
27 changed files with 9135 additions and 0 deletions

28
src/buildx.ts Normal file
View File

@@ -0,0 +1,28 @@
import * as semver from 'semver';
import * as exec from './exec';
export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {
return false;
}
return res.success;
});
}
export async function getVersion(): Promise<string> {
return await exec.exec(`docker`, ['buildx', 'version'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return parseVersion(res.stdout);
});
}
export async function parseVersion(stdout: string): Promise<string> {
const matches = /\sv?([0-9.]+)/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
}
return semver.clean(matches[1]);
}

85
src/context.ts Normal file
View File

@@ -0,0 +1,85 @@
import * as buildx from './buildx';
import * as core from '@actions/core';
export interface Inputs {
builder: string;
files: string[];
targets: string[];
noCache: boolean;
pull: boolean;
load: boolean;
push: boolean;
set: string[];
}
export async function getInputs(): Promise<Inputs> {
return {
builder: core.getInput('builder'),
files: await getInputList('files'),
targets: await getInputList('targets'),
noCache: /true/i.test(core.getInput('no-cache')),
pull: /true/i.test(core.getInput('pull')),
load: /true/i.test(core.getInput('load')),
push: /true/i.test(core.getInput('push')),
set: await getInputList('set', true)
};
}
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx'];
args.push.apply(args, await getBakeArgs(inputs, buildxVersion));
args.push.apply(args, await getCommonArgs(inputs));
args.push.apply(args, inputs.targets);
return args;
}
async function getBakeArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['bake'];
await asyncForEach(inputs.files, async file => {
args.push('--file', file);
});
await asyncForEach(inputs.set, async set => {
args.push('--set', set);
});
return args;
}
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
let args: Array<string> = [];
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 async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {
return [];
}
return items
.split(/\r?\n/)
.filter(x => x)
.reduce<string[]>(
(acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : 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);
}
};

34
src/exec.ts Normal file
View File

@@ -0,0 +1,34 @@
import * as aexec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (command: string, args: string[] = [], silent: boolean): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await aexec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};

32
src/main.ts Normal file
View File

@@ -0,0 +1,32 @@
import * as os from 'os';
import * as buildx from './buildx';
import * as context from './context';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
if (os.platform() !== 'linux') {
core.setFailed('Only supported on linux platform');
return;
}
if (!(await buildx.isAvailable())) {
core.setFailed(`Buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
return;
}
const buildxVersion = await buildx.getVersion();
core.info(`📣 Buildx version: ${buildxVersion}`);
let inputs: context.Inputs = await context.getInputs();
core.info(`🏃 Starting bake...`);
const args: string[] = await context.getArgs(inputs, buildxVersion);
await exec.exec('docker', args);
} catch (error) {
core.setFailed(error.message);
}
}
run();