2020-08-16 11:18:08 -04:00
|
|
|
import * as core from '@actions/core';
|
2021-12-27 18:49:32 -05:00
|
|
|
import * as handlebars from 'handlebars';
|
2023-02-20 05:11:15 -05:00
|
|
|
import {Context} from '@docker/actions-toolkit/lib/context';
|
|
|
|
import {GitHub} from '@docker/actions-toolkit/lib/github';
|
|
|
|
import {Inputs as BuildxInputs} from '@docker/actions-toolkit/lib/buildx/inputs';
|
|
|
|
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
|
|
|
|
import {Util} from '@docker/actions-toolkit/lib/util';
|
2020-10-20 20:46:41 -04:00
|
|
|
|
2020-08-16 11:18:08 -04:00
|
|
|
export interface Inputs {
|
2022-01-31 05:47:45 -05:00
|
|
|
addHosts: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
allow: string[];
|
2023-10-24 01:23:44 -04:00
|
|
|
annotations: string[];
|
2023-01-11 06:12:09 -05:00
|
|
|
attests: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
buildArgs: string[];
|
2022-03-14 15:09:10 -04:00
|
|
|
buildContexts: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
builder: string;
|
|
|
|
cacheFrom: string[];
|
|
|
|
cacheTo: string[];
|
2021-11-16 01:19:27 -05:00
|
|
|
cgroupParent: string;
|
2020-08-16 11:18:08 -04:00
|
|
|
context: string;
|
|
|
|
file: string;
|
|
|
|
labels: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
load: boolean;
|
2021-04-06 08:49:15 -04:00
|
|
|
network: string;
|
2020-08-16 11:18:08 -04:00
|
|
|
noCache: boolean;
|
2022-07-18 11:24:11 -04:00
|
|
|
noCacheFilters: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
outputs: string[];
|
2020-08-16 20:32:27 -04:00
|
|
|
platforms: string[];
|
2023-01-11 06:12:09 -05:00
|
|
|
provenance: string;
|
2021-04-06 07:54:58 -04:00
|
|
|
pull: boolean;
|
2020-08-16 11:18:08 -04:00
|
|
|
push: boolean;
|
2023-01-11 06:12:09 -05:00
|
|
|
sbom: string;
|
2020-09-02 04:07:11 -04:00
|
|
|
secrets: string[];
|
2023-09-26 10:34:10 -04:00
|
|
|
secretEnvs: string[];
|
2021-02-15 04:08:19 -05:00
|
|
|
secretFiles: string[];
|
2021-11-16 01:19:27 -05:00
|
|
|
shmSize: string;
|
2020-09-10 19:23:49 -04:00
|
|
|
ssh: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
tags: string[];
|
|
|
|
target: string;
|
2021-11-16 01:19:27 -05:00
|
|
|
ulimit: string[];
|
2021-04-06 07:54:58 -04:00
|
|
|
githubToken: string;
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:32:21 -04:00
|
|
|
export async function getInputs(): Promise<Inputs> {
|
2020-08-16 11:18:08 -04:00
|
|
|
return {
|
2023-02-20 05:11:15 -05:00
|
|
|
addHosts: Util.getInputList('add-hosts'),
|
|
|
|
allow: Util.getInputList('allow'),
|
2023-10-24 01:23:44 -04:00
|
|
|
annotations: Util.getInputList('annotations', {ignoreComma: true}),
|
2023-02-20 05:11:15 -05:00
|
|
|
attests: Util.getInputList('attests', {ignoreComma: true}),
|
|
|
|
buildArgs: Util.getInputList('build-args', {ignoreComma: true}),
|
|
|
|
buildContexts: Util.getInputList('build-contexts', {ignoreComma: true}),
|
2021-04-06 07:54:58 -04:00
|
|
|
builder: core.getInput('builder'),
|
2023-02-20 05:11:15 -05:00
|
|
|
cacheFrom: Util.getInputList('cache-from', {ignoreComma: true}),
|
|
|
|
cacheTo: Util.getInputList('cache-to', {ignoreComma: true}),
|
2021-11-16 01:19:27 -05:00
|
|
|
cgroupParent: core.getInput('cgroup-parent'),
|
2023-02-20 05:11:15 -05:00
|
|
|
context: core.getInput('context') || Context.gitContext(),
|
2020-12-18 10:05:47 -05:00
|
|
|
file: core.getInput('file'),
|
2023-02-20 05:11:15 -05:00
|
|
|
labels: Util.getInputList('labels', {ignoreComma: true}),
|
2021-06-22 11:25:52 -04:00
|
|
|
load: core.getBooleanInput('load'),
|
2021-04-06 08:49:15 -04:00
|
|
|
network: core.getInput('network'),
|
2021-06-22 11:25:52 -04:00
|
|
|
noCache: core.getBooleanInput('no-cache'),
|
2023-02-20 05:11:15 -05:00
|
|
|
noCacheFilters: Util.getInputList('no-cache-filters'),
|
|
|
|
outputs: Util.getInputList('outputs', {ignoreComma: true}),
|
|
|
|
platforms: Util.getInputList('platforms'),
|
2023-04-16 19:32:21 -04:00
|
|
|
provenance: BuildxInputs.getProvenanceInput('provenance'),
|
2021-06-22 11:25:52 -04:00
|
|
|
pull: core.getBooleanInput('pull'),
|
|
|
|
push: core.getBooleanInput('push'),
|
2023-01-11 06:12:09 -05:00
|
|
|
sbom: core.getInput('sbom'),
|
2023-02-20 05:11:15 -05:00
|
|
|
secrets: Util.getInputList('secrets', {ignoreComma: true}),
|
2023-09-26 10:34:10 -04:00
|
|
|
secretEnvs: Util.getInputList('secret-envs'),
|
2023-02-20 05:11:15 -05:00
|
|
|
secretFiles: Util.getInputList('secret-files', {ignoreComma: true}),
|
2021-11-16 01:19:27 -05:00
|
|
|
shmSize: core.getInput('shm-size'),
|
2023-02-20 05:11:15 -05:00
|
|
|
ssh: Util.getInputList('ssh'),
|
|
|
|
tags: Util.getInputList('tags'),
|
2021-04-06 07:54:58 -04:00
|
|
|
target: core.getInput('target'),
|
2023-02-20 05:11:15 -05:00
|
|
|
ulimit: Util.getInputList('ulimit', {ignoreComma: true}),
|
2021-04-06 07:54:58 -04:00
|
|
|
githubToken: core.getInput('github-token')
|
2020-08-16 11:18:08 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-20 05:11:15 -05:00
|
|
|
export async function getArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
|
|
|
|
const context = handlebars.compile(inputs.context)({
|
|
|
|
defaultContext: Context.gitContext()
|
|
|
|
});
|
2022-03-15 16:59:52 -04:00
|
|
|
// prettier-ignore
|
|
|
|
return [
|
2023-02-20 05:11:15 -05:00
|
|
|
...await getBuildArgs(inputs, context, toolkit),
|
|
|
|
...await getCommonArgs(inputs, toolkit),
|
2022-08-03 08:38:56 -04:00
|
|
|
context
|
2022-03-15 16:59:52 -04:00
|
|
|
];
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
|
|
|
|
2023-02-20 05:11:15 -05:00
|
|
|
async function getBuildArgs(inputs: Inputs, context: string, toolkit: Toolkit): Promise<Array<string>> {
|
2022-03-15 16:59:52 -04:00
|
|
|
const args: Array<string> = ['build'];
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.addHosts, async addHost => {
|
2022-01-31 05:47:45 -05:00
|
|
|
args.push('--add-host', addHost);
|
|
|
|
});
|
2021-11-15 23:19:44 -05:00
|
|
|
if (inputs.allow.length > 0) {
|
|
|
|
args.push('--allow', inputs.allow.join(','));
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
if (await toolkit.buildx.versionSatisfies('>=0.10.0')) {
|
|
|
|
await Util.asyncForEach(inputs.attests, async attest => {
|
2023-01-11 06:12:09 -05:00
|
|
|
args.push('--attest', attest);
|
|
|
|
});
|
|
|
|
}
|
2023-10-24 01:23:44 -04:00
|
|
|
if (await toolkit.buildx.versionSatisfies('>=0.12.0')) {
|
|
|
|
await Util.asyncForEach(inputs.annotations, async annotation => {
|
|
|
|
args.push('--annotation', annotation);
|
|
|
|
});
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.buildArgs, async buildArg => {
|
2020-08-16 11:18:08 -04:00
|
|
|
args.push('--build-arg', buildArg);
|
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
if (await toolkit.buildx.versionSatisfies('>=0.8.0')) {
|
|
|
|
await Util.asyncForEach(inputs.buildContexts, async buildContext => {
|
2022-03-14 15:09:10 -04:00
|
|
|
args.push('--build-context', buildContext);
|
|
|
|
});
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
2021-11-15 23:19:44 -05:00
|
|
|
args.push('--cache-from', cacheFrom);
|
2020-08-16 11:18:08 -04:00
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.cacheTo, async cacheTo => {
|
2021-11-15 23:19:44 -05:00
|
|
|
args.push('--cache-to', cacheTo);
|
2020-08-16 11:18:08 -04:00
|
|
|
});
|
2021-11-16 01:19:27 -05:00
|
|
|
if (inputs.cgroupParent) {
|
|
|
|
args.push('--cgroup-parent', inputs.cgroupParent);
|
|
|
|
}
|
2023-09-26 10:34:10 -04:00
|
|
|
await Util.asyncForEach(inputs.secretEnvs, async secretEnv => {
|
|
|
|
try {
|
|
|
|
args.push('--secret', BuildxInputs.resolveBuildSecretEnv(secretEnv));
|
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
|
|
|
});
|
2021-11-15 23:19:44 -05:00
|
|
|
if (inputs.file) {
|
|
|
|
args.push('--file', inputs.file);
|
2020-08-22 21:31:38 -04:00
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
if (!BuildxInputs.hasLocalExporter(inputs.outputs) && !BuildxInputs.hasTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || (await toolkit.buildx.versionSatisfies('>=0.4.2')))) {
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--iidfile', BuildxInputs.getBuildImageIDFilePath());
|
2020-10-19 15:17:06 -04:00
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.labels, async label => {
|
2021-11-15 23:19:44 -05:00
|
|
|
args.push('--label', label);
|
2020-08-16 11:18:08 -04:00
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.noCacheFilters, async noCacheFilter => {
|
2022-07-18 11:24:11 -04:00
|
|
|
args.push('--no-cache-filter', noCacheFilter);
|
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.outputs, async output => {
|
2021-11-15 23:19:44 -05:00
|
|
|
args.push('--output', output);
|
2020-08-16 11:18:08 -04:00
|
|
|
});
|
2021-11-15 23:19:44 -05:00
|
|
|
if (inputs.platforms.length > 0) {
|
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
if (await toolkit.buildx.versionSatisfies('>=0.10.0')) {
|
2023-01-11 06:12:09 -05:00
|
|
|
if (inputs.provenance) {
|
2023-01-13 04:02:50 -05:00
|
|
|
args.push('--provenance', inputs.provenance);
|
2023-02-20 05:11:15 -05:00
|
|
|
} else if ((await toolkit.buildkit.versionSatisfies(inputs.builder, '>=0.11.0')) && !BuildxInputs.hasDockerExporter(inputs.outputs, inputs.load)) {
|
2023-01-30 13:21:25 -05:00
|
|
|
// if provenance not specified and BuildKit version compatible for
|
|
|
|
// attestation, set default provenance. Also needs to make sure user
|
2023-01-11 08:28:05 -05:00
|
|
|
// doesn't want to explicitly load the image to docker.
|
2023-02-20 05:11:15 -05:00
|
|
|
if (GitHub.context.payload.repository?.private ?? false) {
|
2023-01-30 13:21:25 -05:00
|
|
|
// if this is a private repository, we set the default provenance
|
|
|
|
// attributes being set in buildx: https://github.com/docker/buildx/blob/fb27e3f919dcbf614d7126b10c2bc2d0b1927eb6/build/build.go#L603
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--provenance', BuildxInputs.resolveProvenanceAttrs(`mode=min,inline-only=true`));
|
2023-01-30 13:21:25 -05:00
|
|
|
} else {
|
|
|
|
// for a public repository, we set max provenance mode.
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--provenance', BuildxInputs.resolveProvenanceAttrs(`mode=max`));
|
2023-01-30 13:21:25 -05:00
|
|
|
}
|
2023-01-11 06:12:09 -05:00
|
|
|
}
|
|
|
|
if (inputs.sbom) {
|
|
|
|
args.push('--sbom', inputs.sbom);
|
|
|
|
}
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.secrets, async secret => {
|
2020-11-17 15:38:45 -05:00
|
|
|
try {
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--secret', BuildxInputs.resolveBuildSecretString(secret));
|
2021-02-15 04:08:19 -05:00
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.secretFiles, async secretFile => {
|
2021-02-15 04:08:19 -05:00
|
|
|
try {
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--secret', BuildxInputs.resolveBuildSecretFile(secretFile));
|
2020-11-17 15:38:45 -05:00
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
2020-09-02 04:07:11 -04:00
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
if (inputs.githubToken && !BuildxInputs.hasGitAuthTokenSecret(inputs.secrets) && context.startsWith(Context.gitContext())) {
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--secret', BuildxInputs.resolveBuildSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
|
2020-09-22 14:49:18 -04:00
|
|
|
}
|
2021-11-16 01:19:27 -05:00
|
|
|
if (inputs.shmSize) {
|
|
|
|
args.push('--shm-size', inputs.shmSize);
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.ssh, async ssh => {
|
2020-09-10 19:23:49 -04:00
|
|
|
args.push('--ssh', ssh);
|
|
|
|
});
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.tags, async tag => {
|
2021-11-15 23:19:44 -05:00
|
|
|
args.push('--tag', tag);
|
|
|
|
});
|
|
|
|
if (inputs.target) {
|
|
|
|
args.push('--target', inputs.target);
|
2020-08-16 11:18:08 -04:00
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
await Util.asyncForEach(inputs.ulimit, async ulimit => {
|
2021-11-16 01:19:27 -05:00
|
|
|
args.push('--ulimit', ulimit);
|
|
|
|
});
|
2020-08-16 11:18:08 -04:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2023-02-20 05:11:15 -05:00
|
|
|
async function getCommonArgs(inputs: Inputs, toolkit: Toolkit): Promise<Array<string>> {
|
2022-03-15 16:59:52 -04:00
|
|
|
const args: Array<string> = [];
|
2020-09-03 05:49:39 -04:00
|
|
|
if (inputs.builder) {
|
|
|
|
args.push('--builder', inputs.builder);
|
|
|
|
}
|
2020-08-17 16:18:15 -04:00
|
|
|
if (inputs.load) {
|
|
|
|
args.push('--load');
|
|
|
|
}
|
2023-02-20 05:11:15 -05:00
|
|
|
if (await toolkit.buildx.versionSatisfies('>=0.6.0')) {
|
2023-04-16 19:32:21 -04:00
|
|
|
args.push('--metadata-file', BuildxInputs.getBuildMetadataFilePath());
|
2021-11-15 23:19:44 -05:00
|
|
|
}
|
2021-04-06 08:49:15 -04:00
|
|
|
if (inputs.network) {
|
|
|
|
args.push('--network', inputs.network);
|
|
|
|
}
|
2021-11-15 23:19:44 -05:00
|
|
|
if (inputs.noCache) {
|
|
|
|
args.push('--no-cache');
|
|
|
|
}
|
|
|
|
if (inputs.pull) {
|
|
|
|
args.push('--pull');
|
|
|
|
}
|
2020-08-17 16:18:15 -04:00
|
|
|
if (inputs.push) {
|
|
|
|
args.push('--push');
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|