2020-08-18 23:40:31 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as semver from 'semver';
|
|
|
|
import * as util from 'util';
|
2020-09-03 22:23:15 +08:00
|
|
|
import * as context from './context';
|
2021-07-02 13:02:22 +08:00
|
|
|
import * as git from './git';
|
2020-08-18 23:40:31 +08:00
|
|
|
import * as github from './github';
|
|
|
|
import * as core from '@actions/core';
|
2021-06-23 22:11:52 +08:00
|
|
|
import * as exec from '@actions/exec';
|
2020-08-18 23:40:31 +08:00
|
|
|
import * as tc from '@actions/tool-cache';
|
|
|
|
|
2021-04-24 00:14:38 +08:00
|
|
|
export type Builder = {
|
|
|
|
name?: string;
|
|
|
|
driver?: string;
|
|
|
|
node_name?: string;
|
|
|
|
node_endpoint?: string;
|
|
|
|
node_status?: string;
|
|
|
|
node_flags?: string;
|
|
|
|
node_platforms?: string;
|
|
|
|
};
|
|
|
|
|
2021-09-04 04:21:20 +08:00
|
|
|
export async function getConfigInline(s: string): Promise<string> {
|
|
|
|
return getConfig(s, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getConfigFile(s: string): Promise<string> {
|
|
|
|
return getConfig(s, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getConfig(s: string, file: boolean): Promise<string> {
|
|
|
|
if (file) {
|
|
|
|
if (!fs.existsSync(s)) {
|
|
|
|
throw new Error(`config file ${s} not found`);
|
|
|
|
}
|
|
|
|
s = fs.readFileSync(s, {encoding: 'utf-8'});
|
|
|
|
}
|
|
|
|
const configFile = context.tmpNameSync({
|
|
|
|
tmpdir: context.tmpDir()
|
|
|
|
});
|
|
|
|
fs.writeFileSync(configFile, s);
|
|
|
|
return configFile;
|
|
|
|
}
|
|
|
|
|
2022-03-21 20:43:41 +08:00
|
|
|
export async function isAvailable(): Promise<boolean> {
|
2021-06-23 22:11:52 +08:00
|
|
|
return await exec
|
|
|
|
.getExecOutput('docker', ['buildx'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return res.exitCode == 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-04 03:02:36 +08:00
|
|
|
export async function getVersion(): Promise<string> {
|
2021-06-23 22:11:52 +08:00
|
|
|
return await exec
|
|
|
|
.getExecOutput('docker', ['buildx', 'version'], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(res.stderr.trim());
|
|
|
|
}
|
2021-06-29 20:02:42 +08:00
|
|
|
return parseVersion(res.stdout.trim());
|
2021-06-23 22:11:52 +08:00
|
|
|
});
|
2020-09-04 03:02:36 +08:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
export function parseVersion(stdout: string): string {
|
|
|
|
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
|
2020-09-04 03:02:36 +08:00
|
|
|
if (!matches) {
|
2021-06-23 22:11:52 +08:00
|
|
|
throw new Error(`Cannot parse buildx version`);
|
2020-09-04 03:02:36 +08:00
|
|
|
}
|
2021-07-02 13:02:22 +08:00
|
|
|
return matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function satisfies(version: string, range: string): boolean {
|
|
|
|
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
|
2020-09-04 03:02:36 +08:00
|
|
|
}
|
|
|
|
|
2021-04-24 00:14:38 +08:00
|
|
|
export async function inspect(name: string): Promise<Builder> {
|
2021-06-23 22:11:52 +08:00
|
|
|
return await exec
|
|
|
|
.getExecOutput(`docker`, ['buildx', 'inspect', name], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(res.stderr.trim());
|
2021-04-24 00:14:38 +08:00
|
|
|
}
|
2021-06-23 22:11:52 +08: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;
|
2021-04-24 00:14:38 +08:00
|
|
|
}
|
2021-06-23 22:11:52 +08:00
|
|
|
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;
|
|
|
|
}
|
2021-04-24 00:14:38 +08:00
|
|
|
}
|
2020-08-18 23:40:31 +08:00
|
|
|
}
|
2021-06-23 22:11:52 +08:00
|
|
|
return builder;
|
|
|
|
});
|
2020-08-18 23:40:31 +08:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
export async function build(inputBuildRef: string, dockerConfigHome: string): Promise<string> {
|
2022-03-21 20:43:41 +08:00
|
|
|
// eslint-disable-next-line prefer-const
|
2021-07-02 13:02:22 +08:00
|
|
|
let [repo, ref] = inputBuildRef.split('#');
|
|
|
|
if (ref.length == 0) {
|
|
|
|
ref = 'master';
|
|
|
|
}
|
|
|
|
|
2021-07-12 04:42:19 +08:00
|
|
|
let vspec: string;
|
|
|
|
if (ref.match(/^[0-9a-fA-F]{40}$/)) {
|
|
|
|
vspec = ref;
|
|
|
|
} else {
|
|
|
|
vspec = await git.getRemoteSha(repo, ref);
|
|
|
|
}
|
|
|
|
core.debug(`Tool version spec ${vspec}`);
|
2021-07-02 13:02:22 +08:00
|
|
|
|
|
|
|
let toolPath: string;
|
2021-07-12 04:42:19 +08:00
|
|
|
toolPath = tc.find('buildx', vspec);
|
2021-07-02 13:02:22 +08:00
|
|
|
if (!toolPath) {
|
|
|
|
const outFolder = path.join(context.tmpDir(), 'out').split(path.sep).join(path.posix.sep);
|
|
|
|
toolPath = await exec
|
|
|
|
.getExecOutput('docker', ['buildx', 'build', '--target', 'binaries', '--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1', '--output', `type=local,dest=${outFolder}`, inputBuildRef], {
|
|
|
|
ignoreReturnCode: true
|
|
|
|
})
|
|
|
|
.then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
core.warning(res.stderr.trim());
|
|
|
|
}
|
2021-07-12 04:42:19 +08:00
|
|
|
return tc.cacheFile(`${outFolder}/buildx`, context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx', 'buildx', vspec);
|
2021-07-02 13:02:22 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return setPlugin(toolPath, dockerConfigHome);
|
|
|
|
}
|
|
|
|
|
2020-08-18 23:40:31 +08: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 07:19:14 +08:00
|
|
|
core.debug(`Release ${release.tag_name} found`);
|
2020-08-18 23:40:31 +08: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);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:02:22 +08:00
|
|
|
return setPlugin(toolPath, dockerConfigHome);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setPlugin(toolPath: string, dockerConfigHome: string): Promise<string> {
|
2020-08-18 23:40:31 +08:00
|
|
|
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 22:23:15 +08:00
|
|
|
const filename: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
2020-08-18 23:40:31 +08:00
|
|
|
const pluginPath: string = path.join(pluginsDir, filename);
|
|
|
|
core.debug(`Plugin path is ${pluginPath}`);
|
|
|
|
fs.copyFileSync(path.join(toolPath, filename), pluginPath);
|
|
|
|
|
2021-04-02 07:19:14 +08:00
|
|
|
core.info('Fixing perms');
|
2020-08-18 23:40:31 +08:00
|
|
|
fs.chmodSync(pluginPath, '0755');
|
|
|
|
|
|
|
|
return pluginPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function download(version: string): Promise<string> {
|
2020-09-03 22:23:15 +08:00
|
|
|
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
2021-07-02 13:02:22 +08:00
|
|
|
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, await filename(version));
|
2022-03-21 20:43:41 +08:00
|
|
|
core.info(`Downloading ${downloadUrl}`);
|
|
|
|
const downloadPath = await tc.downloadTool(downloadUrl);
|
|
|
|
core.debug(`Downloaded to ${downloadPath}`);
|
2020-08-18 23:40:31 +08:00
|
|
|
return await tc.cacheFile(downloadPath, targetFile, 'buildx', version);
|
|
|
|
}
|
2020-09-05 22:30:41 +08: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': {
|
2022-03-21 20:43:41 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2020-09-05 22:30:41 +08:00
|
|
|
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);
|
|
|
|
}
|
2021-04-29 00:08:43 +08:00
|
|
|
|
|
|
|
export async function getBuildKitVersion(containerID: string): Promise<string> {
|
2021-06-23 22:11:52 +08:00
|
|
|
return exec
|
|
|
|
.getExecOutput(`docker`, ['inspect', '--format', '{{.Config.Image}}', containerID], {
|
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(bkitimage => {
|
|
|
|
if (bkitimage.exitCode == 0 && bkitimage.stdout.length > 0) {
|
|
|
|
return exec
|
2021-06-29 20:02:42 +08:00
|
|
|
.getExecOutput(`docker`, ['run', '--rm', bkitimage.stdout.trim(), '--version'], {
|
2021-06-23 22:11:52 +08:00
|
|
|
ignoreReturnCode: true,
|
|
|
|
silent: true
|
|
|
|
})
|
|
|
|
.then(bkitversion => {
|
|
|
|
if (bkitversion.exitCode == 0 && bkitversion.stdout.length > 0) {
|
2021-06-29 20:02:42 +08:00
|
|
|
return `${bkitimage.stdout.trim()} => ${bkitversion.stdout.trim()}`;
|
2021-06-23 22:11:52 +08:00
|
|
|
} else if (bkitversion.stderr.length > 0) {
|
|
|
|
core.warning(bkitversion.stderr.trim());
|
|
|
|
}
|
2021-06-29 20:02:42 +08:00
|
|
|
return bkitversion.stdout.trim();
|
2021-06-23 22:11:52 +08:00
|
|
|
});
|
|
|
|
} else if (bkitimage.stderr.length > 0) {
|
|
|
|
core.warning(bkitimage.stderr.trim());
|
|
|
|
}
|
2021-06-29 20:02:42 +08:00
|
|
|
return bkitimage.stdout.trim();
|
2021-06-23 22:11:52 +08:00
|
|
|
});
|
2021-04-29 00:08:43 +08:00
|
|
|
}
|