Check if buildx available on runner

Use default buildx on runner

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-08-12 15:58:09 +02:00
parent 15c6a86f2d
commit 5bb3f5433b
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
9 changed files with 299 additions and 171 deletions

View file

@ -3,13 +3,23 @@ import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as util from 'util';
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 buildx(inputVersion: string, dockerConfigHome: string): Promise<string> {
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 install(inputVersion: string, dockerConfigHome: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
if (!release) {
throw new Error(`Cannot find buildx ${inputVersion} release`);
@ -24,7 +34,7 @@ export async function buildx(inputVersion: string, dockerConfigHome: string): Pr
if (!semver.valid(c)) {
throw new Error(`Invalid Buildx version "${version}".`);
}
toolPath = await installBuildx(version);
toolPath = await download(version);
}
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
@ -44,7 +54,7 @@ export async function buildx(inputVersion: string, dockerConfigHome: string): Pr
return pluginPath;
}
async function installBuildx(version: string): Promise<string> {
async function download(version: string): Promise<string> {
version = semver.clean(version) || '';
const platform: string = osPlat == 'win32' ? 'windows' : osPlat;
const ext: string = osPlat == 'win32' ? '.exe' : '';

34
setup-buildx/src/exec.ts Normal file
View file

@ -0,0 +1,34 @@
import * as actionsExec 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 actionsExec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};

View file

@ -7,6 +7,6 @@ export interface GitHubRelease {
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/docker/buildx/releases/${version}`;
const http: httpm.HttpClient = new httpm.HttpClient('ghaction-docker-buildx');
const http: httpm.HttpClient = new httpm.HttpClient('setup-buildx');
return (await http.getJson<GitHubRelease>(url)).result;
};

View file

@ -1,10 +1,9 @@
import * as child_process from 'child_process';
import * as os from 'os';
import * as path from 'path';
import * as installer from './installer';
import * as buildx from './buildx';
import * as exec from './exec';
import * as stateHelper from './state-helper';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
@ -13,17 +12,20 @@ async function run(): Promise<void> {
return;
}
const buildxVer: string = core.getInput('buildx-version') || 'latest';
const buildxVer: string = core.getInput('buildx-version');
const driver: string = core.getInput('driver') || 'docker-container';
const driverOpt: string = core.getInput('driver-opt');
const install: boolean = /true/i.test(core.getInput('install'));
const use: boolean = /true/i.test(core.getInput('use'));
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
await installer.buildx(buildxVer, dockerConfigHome);
if (!(await buildx.isAvailable()) || buildxVer) {
await buildx.install(buildxVer || 'latest', dockerConfigHome);
}
core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version']);
await exec.exec('docker', ['buildx', 'version'], false);
core.info('🔨 Creating a new builder instance...');
let createArgs: Array<string> = [
@ -41,29 +43,31 @@ async function run(): Promise<void> {
createArgs.push('--use');
}
await exec.exec('docker', createArgs);
await exec.exec('docker', createArgs, false);
core.info('🏃 Booting builder...');
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap'], false);
if (install) {
core.info('🤝 Setting buildx as default builder...');
await exec.exec('docker', ['buildx', 'install']);
await exec.exec('docker', ['buildx', 'install'], false);
}
core.info('🐳 Docker info');
await exec.exec('docker', ['info']);
await exec.exec('docker', ['info'], false);
core.info('🛒 Extracting available platforms...');
const inspect = child_process.execSync('docker buildx inspect', {
encoding: 'utf8'
});
for (const line of inspect.split(os.EOL)) {
if (line.startsWith('Platforms')) {
core.setOutput('platforms', line.replace('Platforms: ', '').replace(/\s/g, '').trim());
break;
await exec.exec(`docker`, ['buildx', 'inspect'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
}
for (const line of res.stdout.trim().split(os.EOL)) {
if (line.startsWith('Platforms')) {
core.setOutput('platforms', line.replace('Platforms: ', '').replace(/\s/g, '').trim());
break;
}
}
});
} catch (error) {
core.setFailed(error.message);
}
@ -72,7 +76,7 @@ async function run(): Promise<void> {
async function cleanup(): Promise<void> {
try {
core.info('🚿 Removing builder instance...');
await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`]);
await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`], false);
} catch (error) {
core.warning(error.message);
}