mirror of
https://github.com/docker/setup-buildx-action.git
synced 2025-05-10 08:29:30 +02:00
support standalone mode and display version
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
2a6fbda6d8
commit
3472856dd9
9 changed files with 223 additions and 46 deletions
61
src/main.ts
61
src/main.ts
|
@ -3,6 +3,7 @@ import * as path from 'path';
|
|||
import * as uuid from 'uuid';
|
||||
import * as buildx from './buildx';
|
||||
import * as context from './context';
|
||||
import * as docker from './docker';
|
||||
import * as stateHelper from './state-helper';
|
||||
import * as util from './util';
|
||||
import * as core from '@actions/core';
|
||||
|
@ -10,32 +11,54 @@ import * as exec from '@actions/exec';
|
|||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
core.startGroup(`Docker info`);
|
||||
await exec.exec('docker', ['version']);
|
||||
await exec.exec('docker', ['info']);
|
||||
core.endGroup();
|
||||
|
||||
const inputs: context.Inputs = await context.getInputs();
|
||||
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
|
||||
|
||||
// standalone if docker cli not available
|
||||
const standalone = !(await docker.isAvailable());
|
||||
stateHelper.setStandalone(standalone);
|
||||
|
||||
core.startGroup(`Docker info`);
|
||||
if (standalone) {
|
||||
core.info(`Docker info skipped in standalone mode`);
|
||||
} else {
|
||||
await exec.exec('docker', ['version'], {
|
||||
failOnStdErr: false
|
||||
});
|
||||
await exec.exec('docker', ['info'], {
|
||||
failOnStdErr: false
|
||||
});
|
||||
}
|
||||
core.endGroup();
|
||||
|
||||
if (util.isValidUrl(inputs.version)) {
|
||||
if (standalone) {
|
||||
throw new Error(`Cannot build from source without the Docker CLI`);
|
||||
}
|
||||
core.startGroup(`Build and install buildx`);
|
||||
await buildx.build(inputs.version, dockerConfigHome);
|
||||
await buildx.build(inputs.version, dockerConfigHome, standalone);
|
||||
core.endGroup();
|
||||
} else if (!(await buildx.isAvailable()) || inputs.version) {
|
||||
} else if (!(await buildx.isAvailable(standalone)) || inputs.version) {
|
||||
core.startGroup(`Download and install buildx`);
|
||||
await buildx.install(inputs.version || 'latest', dockerConfigHome);
|
||||
await buildx.install(inputs.version || 'latest', standalone ? context.tmpDir() : dockerConfigHome, standalone);
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
const buildxVersion = await buildx.getVersion();
|
||||
const buildxVersion = await buildx.getVersion(standalone);
|
||||
await core.group(`Buildx version`, async () => {
|
||||
const versionCmd = buildx.getCommand(['version'], standalone);
|
||||
await exec.exec(versionCmd.commandLine, versionCmd.args, {
|
||||
failOnStdErr: false
|
||||
});
|
||||
});
|
||||
|
||||
const builderName: string = inputs.driver == 'docker' ? 'default' : `builder-${uuid.v4()}`;
|
||||
context.setOutput('name', builderName);
|
||||
stateHelper.setBuilderName(builderName);
|
||||
|
||||
if (inputs.driver !== 'docker') {
|
||||
core.startGroup(`Creating a new builder instance`);
|
||||
const createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', inputs.driver];
|
||||
const createArgs: Array<string> = ['create', '--name', builderName, '--driver', inputs.driver];
|
||||
if (buildx.satisfies(buildxVersion, '>=0.3.0')) {
|
||||
await context.asyncForEach(inputs.driverOpts, async driverOpt => {
|
||||
createArgs.push('--driver-opt', driverOpt);
|
||||
|
@ -55,26 +78,31 @@ async function run(): Promise<void> {
|
|||
} else if (inputs.configInline) {
|
||||
createArgs.push('--config', await buildx.getConfigInline(inputs.configInline));
|
||||
}
|
||||
await exec.exec('docker', createArgs);
|
||||
const createCmd = buildx.getCommand(createArgs, standalone);
|
||||
await exec.exec(createCmd.commandLine, createCmd.args);
|
||||
core.endGroup();
|
||||
|
||||
core.startGroup(`Booting builder`);
|
||||
const bootstrapArgs: Array<string> = ['buildx', 'inspect', '--bootstrap'];
|
||||
const bootstrapArgs: Array<string> = ['inspect', '--bootstrap'];
|
||||
if (buildx.satisfies(buildxVersion, '>=0.4.0')) {
|
||||
bootstrapArgs.push('--builder', builderName);
|
||||
}
|
||||
await exec.exec('docker', bootstrapArgs);
|
||||
const bootstrapCmd = buildx.getCommand(bootstrapArgs, standalone);
|
||||
await exec.exec(bootstrapCmd.commandLine, bootstrapCmd.args);
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
if (inputs.install) {
|
||||
if (standalone) {
|
||||
throw new Error(`Cannot set buildx as default builder without the Docker CLI`);
|
||||
}
|
||||
core.startGroup(`Setting buildx as default builder`);
|
||||
await exec.exec('docker', ['buildx', 'install']);
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
core.startGroup(`Inspect builder`);
|
||||
const builder = await buildx.inspect(builderName);
|
||||
const builder = await buildx.inspect(builderName, standalone);
|
||||
core.info(JSON.stringify(builder, undefined, 2));
|
||||
context.setOutput('driver', builder.driver);
|
||||
context.setOutput('endpoint', builder.node_endpoint);
|
||||
|
@ -83,7 +111,7 @@ async function run(): Promise<void> {
|
|||
context.setOutput('platforms', builder.node_platforms);
|
||||
core.endGroup();
|
||||
|
||||
if (inputs.driver == 'docker-container') {
|
||||
if (!standalone && inputs.driver == 'docker-container') {
|
||||
stateHelper.setContainerName(`buildx_buildkit_${builder.node_name}`);
|
||||
core.startGroup(`BuildKit version`);
|
||||
core.info(await buildx.getBuildKitVersion(`buildx_buildkit_${builder.node_name}`));
|
||||
|
@ -114,8 +142,9 @@ async function cleanup(): Promise<void> {
|
|||
|
||||
if (stateHelper.builderName.length > 0) {
|
||||
core.startGroup(`Removing builder`);
|
||||
const rmCmd = buildx.getCommand(['rm', stateHelper.builderName], /true/i.test(stateHelper.standalone));
|
||||
await exec
|
||||
.getExecOutput('docker', ['buildx', 'rm', `${stateHelper.builderName}`], {
|
||||
.getExecOutput(rmCmd.commandLine, rmCmd.args, {
|
||||
ignoreReturnCode: true
|
||||
})
|
||||
.then(res => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue