mirror of
https://github.com/docker/setup-buildx-action.git
synced 2025-04-23 08:26:38 +02:00
Allow building buildx from source
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
a1c666d855
commit
f40e8894f1
14 changed files with 342 additions and 44 deletions
|
@ -3,6 +3,7 @@ import * as path from 'path';
|
|||
import * as semver from 'semver';
|
||||
import * as util from 'util';
|
||||
import * as context from './context';
|
||||
import * as git from './git';
|
||||
import * as github from './github';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
@ -46,12 +47,16 @@ export async function getVersion(): Promise<string> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function parseVersion(stdout: string): Promise<string> {
|
||||
const matches = /\sv?([0-9.]+)/.exec(stdout);
|
||||
export function parseVersion(stdout: string): string {
|
||||
const matches = /\sv?([0-9a-f]{7}|[0-9.]+)/.exec(stdout);
|
||||
if (!matches) {
|
||||
throw new Error(`Cannot parse buildx version`);
|
||||
}
|
||||
return semver.clean(matches[1]);
|
||||
return matches[1];
|
||||
}
|
||||
|
||||
export function satisfies(version: string, range: string): boolean {
|
||||
return semver.satisfies(version, range) || /^[0-9a-f]{7}$/.exec(version) !== null;
|
||||
}
|
||||
|
||||
export async function inspect(name: string): Promise<Builder> {
|
||||
|
@ -106,6 +111,34 @@ export async function inspect(name: string): Promise<Builder> {
|
|||
});
|
||||
}
|
||||
|
||||
export async function build(inputBuildRef: string, dockerConfigHome: string): Promise<string> {
|
||||
let [repo, ref] = inputBuildRef.split('#');
|
||||
if (ref.length == 0) {
|
||||
ref = 'master';
|
||||
}
|
||||
|
||||
const sha = await git.getRemoteSha(repo, ref);
|
||||
core.debug(`Remote ref ${sha} found`);
|
||||
|
||||
let toolPath: string;
|
||||
toolPath = tc.find('buildx', sha);
|
||||
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());
|
||||
}
|
||||
return tc.cacheFile(`${outFolder}/buildx`, context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx', 'buildx', sha);
|
||||
});
|
||||
}
|
||||
|
||||
return setPlugin(toolPath, dockerConfigHome);
|
||||
}
|
||||
|
||||
export async function install(inputVersion: string, dockerConfigHome: string): Promise<string> {
|
||||
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
|
||||
if (!release) {
|
||||
|
@ -124,6 +157,10 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
|
|||
toolPath = await download(version);
|
||||
}
|
||||
|
||||
return setPlugin(toolPath, dockerConfigHome);
|
||||
}
|
||||
|
||||
async function setPlugin(toolPath: string, dockerConfigHome: string): Promise<string> {
|
||||
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
|
||||
core.debug(`Plugins dir is ${pluginsDir}`);
|
||||
if (!fs.existsSync(pluginsDir)) {
|
||||
|
@ -143,11 +180,7 @@ export async function install(inputVersion: string, dockerConfigHome: string): P
|
|||
|
||||
async function download(version: string): Promise<string> {
|
||||
const targetFile: string = context.osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
|
||||
const downloadUrl = util.format(
|
||||
'https://github.com/docker/buildx/releases/download/v%s/%s',
|
||||
version,
|
||||
await filename(version)
|
||||
);
|
||||
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, await filename(version));
|
||||
let downloadPath: string;
|
||||
|
||||
try {
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
import fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
import {issueCommand} from '@actions/core/lib/command';
|
||||
|
||||
let _tmpDir: string;
|
||||
export const osPlat: string = os.platform();
|
||||
export const osArch: string = os.arch();
|
||||
|
||||
export function tmpDir(): string {
|
||||
if (!_tmpDir) {
|
||||
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-setup-buildx-')).split(path.sep).join(path.posix.sep);
|
||||
}
|
||||
return _tmpDir;
|
||||
}
|
||||
|
||||
export interface Inputs {
|
||||
version: string;
|
||||
driver: string;
|
||||
|
@ -21,9 +31,7 @@ export async function getInputs(): Promise<Inputs> {
|
|||
version: core.getInput('version'),
|
||||
driver: core.getInput('driver') || 'docker-container',
|
||||
driverOpts: await getInputList('driver-opts', true),
|
||||
buildkitdFlags:
|
||||
core.getInput('buildkitd-flags') ||
|
||||
'--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
||||
buildkitdFlags: core.getInput('buildkitd-flags') || '--allow-insecure-entitlement security.insecure --allow-insecure-entitlement network.host',
|
||||
install: core.getBooleanInput('install'),
|
||||
use: core.getBooleanInput('use'),
|
||||
endpoint: core.getInput('endpoint'),
|
||||
|
@ -39,10 +47,7 @@ export async function getInputList(name: string, ignoreComma?: boolean): Promise
|
|||
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()),
|
||||
[]
|
||||
);
|
||||
.reduce<string[]>((acc, line) => acc.concat(!ignoreComma ? line.split(',').filter(x => x) : line).map(pat => pat.trim()), []);
|
||||
}
|
||||
|
||||
export const asyncForEach = async (array, callback) => {
|
||||
|
|
19
src/git.ts
Normal file
19
src/git.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import * as exec from '@actions/exec';
|
||||
|
||||
export async function getRemoteSha(repo: string, ref: string): Promise<string> {
|
||||
return await exec
|
||||
.getExecOutput(`git`, ['ls-remote', repo, ref], {
|
||||
ignoreReturnCode: true,
|
||||
silent: true
|
||||
})
|
||||
.then(res => {
|
||||
if (res.stderr.length > 0 && res.exitCode != 0) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
const [rsha, rref] = res.stdout.trim().split(/[\s\t]/);
|
||||
if (rsha.length == 0) {
|
||||
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
|
||||
}
|
||||
return rsha;
|
||||
});
|
||||
}
|
14
src/main.ts
14
src/main.ts
|
@ -1,9 +1,9 @@
|
|||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as semver from 'semver';
|
||||
import * as buildx from './buildx';
|
||||
import * as context from './context';
|
||||
import * as stateHelper from './state-helper';
|
||||
import * as util from './util';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
|
@ -17,8 +17,12 @@ async function run(): Promise<void> {
|
|||
const inputs: context.Inputs = await context.getInputs();
|
||||
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
||||
|
||||
if (!(await buildx.isAvailable()) || inputs.version) {
|
||||
core.startGroup(`Installing buildx`);
|
||||
if (util.isValidUrl(inputs.version)) {
|
||||
core.startGroup(`Build and install buildx`);
|
||||
await buildx.build(inputs.version, dockerConfigHome);
|
||||
core.endGroup();
|
||||
} else if (!(await buildx.isAvailable()) || inputs.version) {
|
||||
core.startGroup(`Download and install buildx`);
|
||||
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
||||
core.endGroup();
|
||||
}
|
||||
|
@ -31,7 +35,7 @@ async function run(): Promise<void> {
|
|||
if (inputs.driver !== 'docker') {
|
||||
core.startGroup(`Creating a new builder instance`);
|
||||
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
||||
if (semver.satisfies(buildxVersion, '>=0.3.0')) {
|
||||
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
||||
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
||||
createArgs.push('--driver-opt', driverOpt);
|
||||
});
|
||||
|
@ -53,7 +57,7 @@ async function run(): Promise<void> {
|
|||
|
||||
core.startGroup(`Booting builder`);
|
||||
let bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
|
||||
if (semver.satisfies(buildxVersion, '>=0.4.0')) {
|
||||
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
|
||||
bootstrapArgs.push('--builder', builderName);
|
||||
}
|
||||
await exec.exec('docker', bootstrapArgs);
|
||||
|
|
8
src/util.ts
Normal file
8
src/util.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
export function isValidUrl(url: string): boolean {
|
||||
try {
|
||||
new URL(url);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue