Check Buildx version

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-08-23 03:31:38 +02:00
parent f11192a27b
commit 3e57a3300a
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
7 changed files with 2241 additions and 13 deletions

View file

@ -1,5 +1,6 @@
import fs from 'fs';
import path from 'path';
import * as semver from 'semver';
import * as context from './context';
import * as exec from './exec';
@ -24,6 +25,23 @@ export async function isAvailable(): Promise<Boolean> {
});
}
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.]+)\s/.exec(stdout);
if (!matches) {
throw new Error(`Cannot parse Buildx version`);
}
return semver.clean(matches[1]);
}
export async function use(builder: string): Promise<void> {
return await exec.exec(`docker`, ['buildx', 'use', '--builder', builder], false).then(res => {
if (res.stderr != '' && !res.success) {

View file

@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as buildx from './buildx';
import * as core from '@actions/core';
@ -46,15 +47,15 @@ export async function getInputs(): Promise<Inputs> {
};
}
export async function getArgs(inputs: Inputs): Promise<Array<string>> {
export async function getArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['buildx'];
args.push.apply(args, await getBuildArgs(inputs));
args.push.apply(args, await getBuildArgs(inputs, buildxVersion));
args.push.apply(args, await getCommonArgs(inputs));
args.push(inputs.context);
return args;
}
async function getBuildArgs(inputs: Inputs): Promise<Array<string>> {
async function getBuildArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
let args: Array<string> = ['build'];
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
@ -73,7 +74,8 @@ async function getBuildArgs(inputs: Inputs): Promise<Array<string>> {
}
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
} else {
}
if (inputs.platforms.length == 0 || semver.satisfies(buildxVersion, '>=0.4.2')) {
args.push('--iidfile', await buildx.getImageIDFile());
}
await asyncForEach(inputs.outputs, async output => {

View file

@ -16,6 +16,9 @@ async function run(): Promise<void> {
return;
}
const buildxVersion = await buildx.getVersion();
core.info(`📣 Buildx version: ${buildxVersion}`);
let inputs: Inputs = await getInputs();
if (inputs.builder) {
core.info(`📌 Using builder instance ${inputs.builder}`);
@ -23,7 +26,7 @@ async function run(): Promise<void> {
}
core.info(`🏃 Starting build...`);
const args: string[] = await getArgs(inputs);
const args: string[] = await getArgs(inputs, buildxVersion);
await exec.exec('docker', args);
const imageID = await buildx.getImageID();