2021-09-01 22:22:37 +02:00
|
|
|
import * as fs from 'fs';
|
2023-09-08 10:04:41 +02:00
|
|
|
import * as path from 'path';
|
2023-02-20 23:45:13 +01:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
|
import * as actionsToolkit from '@docker/actions-toolkit';
|
2024-04-26 13:12:47 +02:00
|
|
|
|
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';
|
2023-06-09 12:35:42 +02:00
|
|
|
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
2023-02-20 23:45:13 +01:00
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
2024-04-26 13:12:47 +02:00
|
|
|
|
2024-05-27 12:50:16 +02:00
|
|
|
import {BakeDefinition} from '@docker/actions-toolkit/lib/types/buildx/bake';
|
|
|
|
|
import {ConfigFile} from '@docker/actions-toolkit/lib/types/docker/docker';
|
2023-02-20 23:45:13 +01:00
|
|
|
|
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();
|
2024-05-02 15:15:36 +02:00
|
|
|
const gitAuthToken = process.env.BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs['github-token'];
|
2022-05-04 14:53:33 +02:00
|
|
|
|
2023-06-09 12:35:42 +02:00
|
|
|
await core.group(`GitHub Actions runtime token ACs`, async () => {
|
|
|
|
|
try {
|
|
|
|
|
await GitHub.printActionsRuntimeTokenACs();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
core.warning(e.message);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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-09-08 10:04:41 +02:00
|
|
|
await core.group(`Proxy configuration`, async () => {
|
|
|
|
|
let dockerConfig: ConfigFile | undefined;
|
|
|
|
|
let dockerConfigMalformed = false;
|
|
|
|
|
try {
|
|
|
|
|
dockerConfig = await Docker.configFile();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
dockerConfigMalformed = true;
|
|
|
|
|
core.warning(`Unable to parse config file ${path.join(Docker.configDir, 'config.json')}: ${e}`);
|
|
|
|
|
}
|
|
|
|
|
if (dockerConfig && dockerConfig.proxies) {
|
|
|
|
|
for (const host in dockerConfig.proxies) {
|
|
|
|
|
let prefix = '';
|
|
|
|
|
if (Object.keys(dockerConfig.proxies).length > 1) {
|
|
|
|
|
prefix = ' ';
|
|
|
|
|
core.info(host);
|
|
|
|
|
}
|
|
|
|
|
for (const key in dockerConfig.proxies[host]) {
|
|
|
|
|
core.info(`${prefix}${key}: ${dockerConfig.proxies[host][key]}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (!dockerConfigMalformed) {
|
|
|
|
|
core.info('No proxy configuration found');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
2024-05-29 13:14:09 +02:00
|
|
|
await core.group(`Builder info`, async () => {
|
|
|
|
|
const builder = await toolkit.builder.inspect(inputs.builder);
|
|
|
|
|
core.info(JSON.stringify(builder, null, 2));
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-05 14:25:34 +02:00
|
|
|
let definition: BakeDefinition | undefined;
|
|
|
|
|
await core.group(`Parsing raw definition`, async () => {
|
2024-05-14 14:02:37 +02:00
|
|
|
definition = await toolkit.buildxBake.getDefinition(
|
2024-04-05 14:25:34 +02:00
|
|
|
{
|
|
|
|
|
files: inputs.files,
|
|
|
|
|
load: inputs.load,
|
2024-05-02 15:15:36 +02:00
|
|
|
noCache: inputs['no-cache'],
|
2024-04-05 14:25:34 +02:00
|
|
|
overrides: inputs.set,
|
|
|
|
|
provenance: inputs.provenance,
|
|
|
|
|
push: inputs.push,
|
|
|
|
|
sbom: inputs.sbom,
|
|
|
|
|
source: inputs.source,
|
2024-04-13 15:20:19 +02:00
|
|
|
targets: inputs.targets,
|
|
|
|
|
githubToken: gitAuthToken
|
2024-04-05 14:25:34 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
cwd: inputs.workdir
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
if (!definition) {
|
|
|
|
|
throw new Error('Bake definition not set');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const args: string[] = await context.getArgs(inputs, definition, toolkit);
|
2023-02-20 23:45:13 +01:00
|
|
|
const buildCmd = await toolkit.buildx.getCommand(args);
|
2024-04-13 15:20:19 +02:00
|
|
|
const buildEnv = Object.assign({}, process.env, {
|
|
|
|
|
BUILDX_BAKE_GIT_AUTH_TOKEN: gitAuthToken
|
|
|
|
|
}) as {
|
|
|
|
|
[key: string]: string;
|
|
|
|
|
};
|
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'], {
|
2024-04-13 15:20:19 +02:00
|
|
|
cwd: inputs.workdir,
|
|
|
|
|
env: buildEnv
|
2023-02-20 23:45:13 +01:00
|
|
|
});
|
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,
|
2024-04-13 15:20:19 +02:00
|
|
|
env: buildEnv,
|
2023-02-20 23:45:13 +01:00
|
|
|
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
|
|
|
|
2024-05-14 14:02:37 +02:00
|
|
|
const metadata = toolkit.buildxBake.resolveMetadata();
|
2022-02-09 11:36:19 +01:00
|
|
|
if (metadata) {
|
2023-02-20 23:45:13 +01:00
|
|
|
await core.group(`Metadata`, async () => {
|
2024-04-26 13:12:47 +02:00
|
|
|
const metadatadt = JSON.stringify(metadata, null, 2);
|
|
|
|
|
core.info(metadatadt);
|
|
|
|
|
core.setOutput('metadata', metadatadt);
|
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
|
|
|
);
|