2021-09-01 22:22:37 +02:00
|
|
|
import * as fs from 'fs';
|
2023-02-20 23:45:13 +01:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
|
import * as actionsToolkit from '@docker/actions-toolkit';
|
2023-03-13 11:32:49 +01:00
|
|
|
import {Inputs as BuildxInputs} from '@docker/actions-toolkit/lib/buildx/inputs';
|
2023-02-20 23:45:13 +01:00
|
|
|
import {Context} from '@docker/actions-toolkit/lib/context';
|
2023-03-13 11:32:49 +01:00
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
|
2023-02-20 23:45:13 +01:00
|
|
|
import {Exec} from '@docker/actions-toolkit/lib/exec';
|
|
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
|
|
|
|
|
2020-10-08 00:52:52 +02:00
|
|
|
import * as context from './context';
|
2021-09-01 22:22:37 +02:00
|
|
|
import * as stateHelper from './state-helper';
|
2020-10-08 00:52:52 +02:00
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
actionsToolkit.run(
|
|
|
|
|
// main
|
|
|
|
|
async () => {
|
2022-05-04 14:53:33 +02:00
|
|
|
const inputs: context.Inputs = await context.getInputs();
|
2023-02-20 23:45:13 +01:00
|
|
|
const toolkit = new Toolkit();
|
2022-05-04 14:53:33 +02:00
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
await core.group(`Docker info`, async () => {
|
|
|
|
|
try {
|
|
|
|
|
await Docker.printVersion();
|
|
|
|
|
await Docker.printInfo();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
core.info(e.message);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-10-08 00:52:52 +02:00
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
if (!(await toolkit.buildx.isAvailable())) {
|
2021-04-27 15:47:00 +02:00
|
|
|
core.setFailed(`Docker buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
2020-10-08 00:52:52 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
stateHelper.setTmpDir(Context.tmpDir());
|
|
|
|
|
|
2022-05-04 14:53:33 +02:00
|
|
|
await core.group(`Buildx version`, async () => {
|
2023-02-20 23:45:13 +01:00
|
|
|
await toolkit.buildx.printVersion();
|
2022-05-04 14:53:33 +02:00
|
|
|
});
|
|
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
const args: string[] = await context.getArgs(inputs, toolkit);
|
|
|
|
|
const buildCmd = await toolkit.buildx.getCommand(args);
|
2020-12-24 18:03:30 +01:00
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
await core.group(`Bake definition`, async () => {
|
|
|
|
|
await Exec.exec(buildCmd.command, [...buildCmd.args, '--print'], {
|
|
|
|
|
cwd: inputs.workdir
|
|
|
|
|
});
|
2022-07-08 08:53:34 -04:00
|
|
|
});
|
2020-12-24 18:03:30 +01:00
|
|
|
|
2023-02-20 23:45:13 +01:00
|
|
|
await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
|
|
|
|
|
cwd: inputs.workdir,
|
|
|
|
|
ignoreReturnCode: true
|
|
|
|
|
}).then(res => {
|
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
|
throw new Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
|
|
|
|
|
}
|
|
|
|
|
});
|
2021-09-01 22:22:37 +02:00
|
|
|
|
2023-03-13 11:32:49 +01:00
|
|
|
const metadata = await BuildxInputs.resolveBuildMetadata();
|
2022-02-09 11:36:19 +01:00
|
|
|
if (metadata) {
|
2023-02-20 23:45:13 +01:00
|
|
|
await core.group(`Metadata`, async () => {
|
2022-02-09 11:36:19 +01:00
|
|
|
core.info(metadata);
|
2022-10-12 07:02:18 +02:00
|
|
|
core.setOutput('metadata', metadata);
|
2022-02-09 11:36:19 +01:00
|
|
|
});
|
|
|
|
|
}
|
2023-02-20 23:45:13 +01:00
|
|
|
},
|
|
|
|
|
// post
|
|
|
|
|
async () => {
|
|
|
|
|
if (stateHelper.tmpDir.length > 0) {
|
|
|
|
|
await core.group(`Removing temp folder ${stateHelper.tmpDir}`, async () => {
|
|
|
|
|
fs.rmSync(stateHelper.tmpDir, {recursive: true});
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-09-01 22:22:37 +02:00
|
|
|
}
|
2023-02-20 23:45:13 +01:00
|
|
|
);
|