2020-09-02 10:07:11 +02:00
|
|
|
import * as fs from 'fs';
|
2023-09-08 15:28:08 +02:00
|
|
|
import * as path from 'path';
|
2020-09-02 10:07:11 +02:00
|
|
|
import * as stateHelper from './state-helper';
|
2020-08-16 00:36:41 +02:00
|
|
|
import * as core from '@actions/core';
|
2023-02-20 11:11:15 +01:00
|
|
|
import * as actionsToolkit from '@docker/actions-toolkit';
|
2024-04-26 11:20:49 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
import {Context} from '@docker/actions-toolkit/lib/context';
|
2023-03-12 23:34:24 +01:00
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
|
2023-02-20 11:11:15 +01:00
|
|
|
import {Exec} from '@docker/actions-toolkit/lib/exec';
|
|
|
|
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
2024-04-26 11:20:49 +02:00
|
|
|
|
2023-09-08 15:28:08 +02:00
|
|
|
import {ConfigFile} from '@docker/actions-toolkit/lib/types/docker';
|
2020-08-16 00:36:41 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
import * as context from './context';
|
2022-04-28 09:31:47 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
actionsToolkit.run(
|
|
|
|
// main
|
|
|
|
async () => {
|
2023-04-17 01:32:21 +02:00
|
|
|
const inputs: context.Inputs = await context.getInputs();
|
2024-03-06 14:20:33 +01:00
|
|
|
core.debug(`inputs: ${JSON.stringify(inputs)}`);
|
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
const toolkit = new Toolkit();
|
2022-04-28 09:31:47 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
await core.group(`GitHub Actions runtime token ACs`, async () => {
|
|
|
|
try {
|
|
|
|
await GitHub.printActionsRuntimeTokenACs();
|
|
|
|
} catch (e) {
|
|
|
|
core.warning(e.message);
|
2023-01-12 19:44:15 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
await core.group(`Docker info`, async () => {
|
|
|
|
try {
|
|
|
|
await Docker.printVersion();
|
|
|
|
await Docker.printInfo();
|
|
|
|
} catch (e) {
|
|
|
|
core.info(e.message);
|
|
|
|
}
|
|
|
|
});
|
2020-08-16 00:36:41 +02:00
|
|
|
|
2023-09-08 15:28:08 +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) {
|
2023-06-13 11:46:07 +02:00
|
|
|
for (const host in dockerConfig.proxies) {
|
|
|
|
let prefix = '';
|
2023-09-07 12:21:01 +02:00
|
|
|
if (Object.keys(dockerConfig.proxies).length > 1) {
|
2023-06-13 11:46:07 +02:00
|
|
|
prefix = ' ';
|
|
|
|
core.info(host);
|
|
|
|
}
|
|
|
|
for (const key in dockerConfig.proxies[host]) {
|
|
|
|
core.info(`${prefix}${key}: ${dockerConfig.proxies[host][key]}`);
|
|
|
|
}
|
|
|
|
}
|
2023-09-08 15:28:08 +02:00
|
|
|
} else if (!dockerConfigMalformed) {
|
|
|
|
core.info('No proxy configuration found');
|
|
|
|
}
|
|
|
|
});
|
2023-06-13 11:46:07 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
if (!(await toolkit.buildx.isAvailable())) {
|
2021-04-27 16:16:22 +02:00
|
|
|
core.setFailed(`Docker buildx is required. See https://github.com/docker/setup-buildx-action to set up buildx.`);
|
|
|
|
return;
|
2020-08-16 00:36:41 +02:00
|
|
|
}
|
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
stateHelper.setTmpDir(Context.tmpDir());
|
|
|
|
|
2022-04-28 09:31:47 +02:00
|
|
|
await core.group(`Buildx version`, async () => {
|
2023-02-20 11:11:15 +01:00
|
|
|
await toolkit.buildx.printVersion();
|
2022-04-28 09:31:47 +02:00
|
|
|
});
|
2020-08-16 22:31:37 +02:00
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
const args: string[] = await context.getArgs(inputs, toolkit);
|
2024-03-06 14:20:33 +01:00
|
|
|
core.debug(`context.getArgs: ${JSON.stringify(args)}`);
|
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
const buildCmd = await toolkit.buildx.getCommand(args);
|
2024-03-06 14:20:33 +01:00
|
|
|
core.debug(`buildCmd.command: ${buildCmd.command}`);
|
|
|
|
core.debug(`buildCmd.args: ${JSON.stringify(buildCmd.args)}`);
|
|
|
|
|
2023-02-20 11:11:15 +01:00
|
|
|
await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
|
|
|
|
ignoreReturnCode: true
|
|
|
|
}).then(res => {
|
|
|
|
if (res.stderr.length > 0 && res.exitCode != 0) {
|
|
|
|
throw new Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
|
|
|
|
}
|
|
|
|
});
|
2020-08-17 22:18:15 +02:00
|
|
|
|
2024-05-14 14:11:32 +02:00
|
|
|
const imageID = toolkit.buildxBuild.resolveImageID();
|
|
|
|
const metadata = toolkit.buildxBuild.resolveMetadata();
|
|
|
|
const digest = toolkit.buildxBuild.resolveDigest();
|
2022-03-14 19:30:50 +01:00
|
|
|
|
2022-02-09 11:32:35 +01:00
|
|
|
if (imageID) {
|
2022-03-14 19:30:50 +01:00
|
|
|
await core.group(`ImageID`, async () => {
|
2022-02-09 11:32:35 +01:00
|
|
|
core.info(imageID);
|
2022-10-12 06:56:31 +02:00
|
|
|
core.setOutput('imageid', imageID);
|
2022-03-14 19:30:50 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (digest) {
|
|
|
|
await core.group(`Digest`, async () => {
|
|
|
|
core.info(digest);
|
2022-10-12 06:56:31 +02:00
|
|
|
core.setOutput('digest', digest);
|
2022-02-09 11:32:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (metadata) {
|
2022-03-14 19:30:50 +01:00
|
|
|
await core.group(`Metadata`, async () => {
|
2024-04-26 11:20:49 +02:00
|
|
|
const metadatadt = JSON.stringify(metadata, null, 2);
|
|
|
|
core.info(metadatadt);
|
|
|
|
core.setOutput('metadata', metadatadt);
|
2022-02-09 11:32:35 +01:00
|
|
|
});
|
|
|
|
}
|
2023-02-20 11:11:15 +01:00
|
|
|
},
|
|
|
|
// post
|
|
|
|
async () => {
|
|
|
|
if (stateHelper.tmpDir.length > 0) {
|
|
|
|
await core.group(`Removing temp folder ${stateHelper.tmpDir}`, async () => {
|
|
|
|
fs.rmSync(stateHelper.tmpDir, {recursive: true});
|
|
|
|
});
|
|
|
|
}
|
2020-09-02 10:07:11 +02:00
|
|
|
}
|
2023-02-20 11:11:15 +01:00
|
|
|
);
|