2020-08-18 17:40:31 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as util from 'util';
|
2020-09-03 16:23:15 +02:00
|
|
|
import * as context from './context';
|
2020-08-18 17:40:31 +02:00
|
|
|
import * as exec from './exec';
|
|
|
|
import * as github from './github';
|
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
|
2021-04-23 18:14:38 +02:00
|
|
|
export type Builder = {
|
|
|
|
name?: string;
|
|
|
|
driver?: string;
|
|
|
|
node_name?: string;
|
|
|
|
node_endpoint?: string;
|
|
|
|
node_status?: string;
|
|
|
|
node_flags?: string;
|
|
|
|
node_platforms?: string;
|
|
|
|
};
|
|
|
|
|
2020-09-03 21:02:36 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:40:31 +02:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-23 18:14:38 +02:00
|
|
|
export async function inspect(name: string): Promise<Builder> {
|
|
|
|
return await exec.exec(`docker`, ['buildx', 'inspect', name], true).then(res => {
|
2020-08-18 17:40:31 +02:00
|
|
|
if (res.stderr != '' && !res.success) {
|
|
|
|
throw new Error(res.stderr);
|
|
|
|
}
|
2021-04-23 18:14:38 +02:00
|
|
|
const builder: Builder = {};
|
|
|
|
itlines: for (const line of res.stdout.trim().split(`\n`)) {
|
|
|
|
const [key, ...rest] = line.split(':');
|
|
|
|
const value = rest.map(v => v.trim()).join(':');
|
|
|
|
if (key.length == 0 || value.length == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (key) {
|
|
|
|
case 'Name': {
|
|
|
|
if (builder.name == undefined) {
|
|
|
|
builder.name = value;
|
|
|
|
} else {
|
|
|
|
builder.node_name = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Driver': {
|
|
|
|
builder.driver = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Endpoint': {
|
|
|
|
builder.node_endpoint = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Status': {
|
|
|
|
builder.node_status = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Flags': {
|
|
|
|
builder.node_flags = value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Platforms': {
|
|
|
|
builder.node_platforms = value.replace(/\s/g, '');
|
|
|
|
break itlines;
|
|
|
|
}
|
2020-08-18 17:40:31 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-23 18:14:38 +02:00
|
|
|
return builder;
|
2020-08-18 17:40:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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`);
|
|
|
|
}
|
2021-04-02 01:19:14 +02:00
|
|
|
core.debug(`Release ${release.tag_name} found`);
|
2020-08-18 17:40:31 +02:00
|
|
|
const version = release.tag_name.replace(/^v+|v+$/g, '');
|
|
|
|
|
|
|
|
let toolPath: string;
|
|
|
|
toolPath = tc.find('buildx', version);
|
|
|
|
if (!toolPath) {
|
|
|
|
const c = semver.clean(version) || '';
|
|
|
|
if (!semver.valid(c)) {
|
|
|
|
throw new Error(`Invalid Buildx version "${version}".`);
|
|
|
|
}
|
|
|
|
toolPath = await download(version);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
|
|
|
|
core.debug(`Plugins dir is ${pluginsDir}`);
|
|
|
|
if (!fs.existsSync(pluginsDir)) {
|
|
|
|
fs.mkdirSync(pluginsDir, {recursive: true});
|
|
|
|
}
|
|
|
|
|
2020-09-03 16:23:15 +02:00
|
|
|
const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
2020-08-18 17:40:31 +02:00
|
|
|
const pluginPath: string = path.join(pluginsDir, filename);
|
|
|
|
core.debug(`Plugin path is ${pluginPath}`);
|
|
|
|
fs.copyFileSync(path.join(toolPath, filename), pluginPath);
|
|
|
|
|
2021-04-02 01:19:14 +02:00
|
|
|
core.info('Fixing perms');
|
2020-08-18 17:40:31 +02:00
|
|
|
fs.chmodSync(pluginPath, '0755');
|
|
|
|
|
|
|
|
return pluginPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function download(version: string): Promise<string> {
|
2020-09-03 16:23:15 +02:00
|
|
|
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
2020-09-05 16:30:41 +02:00
|
|
|
const downloadUrl = util.format(
|
|
|
|
'https://github.com/docker/buildx/releases/download/v%s/%s',
|
|
|
|
version,
|
|
|
|
await filename(version)
|
|
|
|
);
|
2020-08-18 17:40:31 +02:00
|
|
|
let downloadPath: string;
|
|
|
|
|
|
|
|
try {
|
2021-04-02 01:19:14 +02:00
|
|
|
core.info(`Downloading ${downloadUrl}`);
|
2020-08-18 17:40:31 +02:00
|
|
|
downloadPath = await tc.downloadTool(downloadUrl);
|
|
|
|
core.debug(`Downloaded to ${downloadPath}`);
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await tc.cacheFile(downloadPath, targetFile, 'buildx', version);
|
|
|
|
}
|
2020-09-05 16:30:41 +02:00
|
|
|
|
|
|
|
async function filename(version: string): Promise<string> {
|
|
|
|
let arch: string;
|
|
|
|
switch (context.osArch) {
|
|
|
|
case 'x64': {
|
|
|
|
arch = 'amd64';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ppc64': {
|
|
|
|
arch = 'ppc64le';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'arm': {
|
|
|
|
const arm_version = (process.config.variables as any).arm_version;
|
|
|
|
arch = arm_version ? 'arm-v' + arm_version : 'arm';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
arch = context.osArch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const platform: string = context.osPlat == 'win32' ? 'windows' : context.osPlat;
|
|
|
|
const ext: string = context.osPlat == 'win32' ? '.exe' : '';
|
|
|
|
return util.format('buildx-v%s.%s-%s%s', version, platform, arch, ext);
|
|
|
|
}
|