2020-08-17 20:18:15 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
2020-10-19 19:17:06 +00:00
|
|
|
import * as tmp from 'tmp';
|
2022-10-12 04:56:31 +00:00
|
|
|
import * as buildx from './buildx';
|
2020-08-16 15:18:08 +00:00
|
|
|
import * as core from '@actions/core';
|
2020-09-02 08:07:11 +00:00
|
|
|
import * as github from '@actions/github';
|
2022-10-12 04:56:31 +00:00
|
|
|
import {parse} from 'csv-parse/sync';
|
2021-12-27 23:49:32 +00:00
|
|
|
import * as handlebars from 'handlebars';
|
2020-09-10 23:23:49 +00:00
|
|
|
|
2020-10-21 00:46:41 +00:00
|
|
|
let _defaultContext, _tmpDir: string;
|
|
|
|
|
2020-08-16 15:18:08 +00:00
|
|
|
export interface Inputs {
|
2022-01-31 10:47:45 +00:00
|
|
|
addHosts: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
allow: string[];
|
2023-01-11 11:12:09 +00:00
|
|
|
attests: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
buildArgs: string[];
|
2022-03-14 19:09:10 +00:00
|
|
|
buildContexts: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
builder: string;
|
|
|
|
cacheFrom: string[];
|
|
|
|
cacheTo: string[];
|
2021-11-16 06:19:27 +00:00
|
|
|
cgroupParent: string;
|
2020-08-16 15:18:08 +00:00
|
|
|
context: string;
|
|
|
|
file: string;
|
|
|
|
labels: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
load: boolean;
|
2021-04-06 12:49:15 +00:00
|
|
|
network: string;
|
2020-08-16 15:18:08 +00:00
|
|
|
noCache: boolean;
|
2022-07-18 15:24:11 +00:00
|
|
|
noCacheFilters: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
outputs: string[];
|
2020-08-17 00:32:27 +00:00
|
|
|
platforms: string[];
|
2023-01-11 11:12:09 +00:00
|
|
|
provenance: string;
|
2021-04-06 11:54:58 +00:00
|
|
|
pull: boolean;
|
2020-08-16 15:18:08 +00:00
|
|
|
push: boolean;
|
2023-01-11 11:12:09 +00:00
|
|
|
sbom: string;
|
2020-09-02 08:07:11 +00:00
|
|
|
secrets: string[];
|
2021-02-15 09:08:19 +00:00
|
|
|
secretFiles: string[];
|
2021-11-16 06:19:27 +00:00
|
|
|
shmSize: string;
|
2020-09-10 23:23:49 +00:00
|
|
|
ssh: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
tags: string[];
|
|
|
|
target: string;
|
2021-11-16 06:19:27 +00:00
|
|
|
ulimit: string[];
|
2021-04-06 11:54:58 +00:00
|
|
|
githubToken: string;
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
export function defaultContext(): string {
|
2020-10-21 00:46:41 +00:00
|
|
|
if (!_defaultContext) {
|
2021-04-26 09:02:09 +00:00
|
|
|
let ref = github.context.ref;
|
|
|
|
if (github.context.sha && ref && !ref.startsWith('refs/')) {
|
|
|
|
ref = `refs/heads/${github.context.ref}`;
|
|
|
|
}
|
|
|
|
if (github.context.sha && !ref.startsWith(`refs/pull/`)) {
|
|
|
|
ref = github.context.sha;
|
|
|
|
}
|
2021-07-01 13:29:36 +00:00
|
|
|
_defaultContext = `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}.git#${ref}`;
|
2020-10-21 00:46:41 +00:00
|
|
|
}
|
|
|
|
return _defaultContext;
|
2020-10-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpDir(): string {
|
2020-10-21 00:46:41 +00:00
|
|
|
if (!_tmpDir) {
|
|
|
|
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-')).split(path.sep).join(path.posix.sep);
|
|
|
|
}
|
|
|
|
return _tmpDir;
|
2020-10-19 19:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function tmpNameSync(options?: tmp.TmpNameOptions): string {
|
|
|
|
return tmp.tmpNameSync(options);
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:02:50 +00:00
|
|
|
export function provenanceBuilderID(): string {
|
|
|
|
return `${process.env.GITHUB_SERVER_URL || 'https://github.com'}/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`;
|
|
|
|
}
|
|
|
|
|
2020-10-19 19:17:06 +00:00
|
|
|
export async function getInputs(defaultContext: string): Promise<Inputs> {
|
2020-08-16 15:18:08 +00:00
|
|
|
return {
|
2022-01-31 10:47:45 +00:00
|
|
|
addHosts: await getInputList('add-hosts'),
|
2021-04-06 11:54:58 +00:00
|
|
|
allow: await getInputList('allow'),
|
2023-01-11 11:12:09 +00:00
|
|
|
attests: await getInputList('attests', true),
|
2023-01-13 10:24:17 +00:00
|
|
|
buildArgs: await getInputList('build-args', true),
|
2022-03-14 19:09:10 +00:00
|
|
|
buildContexts: await getInputList('build-contexts', true),
|
2021-04-06 11:54:58 +00:00
|
|
|
builder: core.getInput('builder'),
|
|
|
|
cacheFrom: await getInputList('cache-from', true),
|
|
|
|
cacheTo: await getInputList('cache-to', true),
|
2021-11-16 06:19:27 +00:00
|
|
|
cgroupParent: core.getInput('cgroup-parent'),
|
2020-09-22 18:49:18 +00:00
|
|
|
context: core.getInput('context') || defaultContext,
|
2020-12-18 15:05:47 +00:00
|
|
|
file: core.getInput('file'),
|
2020-10-20 17:04:54 +00:00
|
|
|
labels: await getInputList('labels', true),
|
2021-06-22 15:25:52 +00:00
|
|
|
load: core.getBooleanInput('load'),
|
2021-04-06 12:49:15 +00:00
|
|
|
network: core.getInput('network'),
|
2021-06-22 15:25:52 +00:00
|
|
|
noCache: core.getBooleanInput('no-cache'),
|
2022-07-18 15:24:11 +00:00
|
|
|
noCacheFilters: await getInputList('no-cache-filters'),
|
2021-04-06 11:54:58 +00:00
|
|
|
outputs: await getInputList('outputs', true),
|
2020-08-17 00:32:27 +00:00
|
|
|
platforms: await getInputList('platforms'),
|
2023-01-13 09:02:50 +00:00
|
|
|
provenance: getProvenanceInput('provenance'),
|
2021-06-22 15:25:52 +00:00
|
|
|
pull: core.getBooleanInput('pull'),
|
|
|
|
push: core.getBooleanInput('push'),
|
2023-01-11 11:12:09 +00:00
|
|
|
sbom: core.getInput('sbom'),
|
2020-09-22 18:49:18 +00:00
|
|
|
secrets: await getInputList('secrets', true),
|
2021-02-15 09:08:19 +00:00
|
|
|
secretFiles: await getInputList('secret-files', true),
|
2021-11-16 06:19:27 +00:00
|
|
|
shmSize: core.getInput('shm-size'),
|
2021-04-06 11:54:58 +00:00
|
|
|
ssh: await getInputList('ssh'),
|
|
|
|
tags: await getInputList('tags'),
|
|
|
|
target: core.getInput('target'),
|
2021-11-16 06:19:27 +00:00
|
|
|
ulimit: await getInputList('ulimit', true),
|
2021-04-06 11:54:58 +00:00
|
|
|
githubToken: core.getInput('github-token')
|
2020-08-16 15:18:08 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:10:34 +00:00
|
|
|
export async function getArgs(inputs: Inputs, defaultContext: string, buildxVersion: string, standalone?: boolean): Promise<Array<string>> {
|
2022-08-03 12:38:56 +00:00
|
|
|
const context = handlebars.compile(inputs.context)({defaultContext});
|
2022-03-15 20:59:52 +00:00
|
|
|
// prettier-ignore
|
|
|
|
return [
|
2023-01-11 11:10:34 +00:00
|
|
|
...await getBuildArgs(inputs, defaultContext, context, buildxVersion, standalone),
|
2022-03-15 20:59:52 +00:00
|
|
|
...await getCommonArgs(inputs, buildxVersion),
|
2022-08-03 12:38:56 +00:00
|
|
|
context
|
2022-03-15 20:59:52 +00:00
|
|
|
];
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 11:10:34 +00:00
|
|
|
async function getBuildArgs(inputs: Inputs, defaultContext: string, context: string, buildxVersion: string, standalone?: boolean): Promise<Array<string>> {
|
2022-03-15 20:59:52 +00:00
|
|
|
const args: Array<string> = ['build'];
|
2022-01-31 10:47:45 +00:00
|
|
|
await asyncForEach(inputs.addHosts, async addHost => {
|
|
|
|
args.push('--add-host', addHost);
|
|
|
|
});
|
2021-11-16 04:19:44 +00:00
|
|
|
if (inputs.allow.length > 0) {
|
|
|
|
args.push('--allow', inputs.allow.join(','));
|
|
|
|
}
|
2023-01-11 11:12:09 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.10.0')) {
|
|
|
|
await asyncForEach(inputs.attests, async attest => {
|
|
|
|
args.push('--attest', attest);
|
|
|
|
});
|
|
|
|
}
|
2020-08-16 15:18:08 +00:00
|
|
|
await asyncForEach(inputs.buildArgs, async buildArg => {
|
|
|
|
args.push('--build-arg', buildArg);
|
|
|
|
});
|
2022-03-14 19:09:10 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.8.0')) {
|
|
|
|
await asyncForEach(inputs.buildContexts, async buildContext => {
|
|
|
|
args.push('--build-context', buildContext);
|
|
|
|
});
|
|
|
|
}
|
2021-11-16 04:19:44 +00:00
|
|
|
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
|
|
|
args.push('--cache-from', cacheFrom);
|
2020-08-16 15:18:08 +00:00
|
|
|
});
|
2021-11-16 04:19:44 +00:00
|
|
|
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
|
|
|
args.push('--cache-to', cacheTo);
|
2020-08-16 15:18:08 +00:00
|
|
|
});
|
2021-11-16 06:19:27 +00:00
|
|
|
if (inputs.cgroupParent) {
|
|
|
|
args.push('--cgroup-parent', inputs.cgroupParent);
|
|
|
|
}
|
2021-11-16 04:19:44 +00:00
|
|
|
if (inputs.file) {
|
|
|
|
args.push('--file', inputs.file);
|
2020-08-23 01:31:38 +00:00
|
|
|
}
|
2021-07-01 13:29:36 +00:00
|
|
|
if (!buildx.isLocalOrTarExporter(inputs.outputs) && (inputs.platforms.length == 0 || buildx.satisfies(buildxVersion, '>=0.4.2'))) {
|
2020-10-19 19:17:06 +00:00
|
|
|
args.push('--iidfile', await buildx.getImageIDFile());
|
|
|
|
}
|
2021-11-16 04:19:44 +00:00
|
|
|
await asyncForEach(inputs.labels, async label => {
|
|
|
|
args.push('--label', label);
|
2020-08-16 15:18:08 +00:00
|
|
|
});
|
2022-07-18 15:24:11 +00:00
|
|
|
await asyncForEach(inputs.noCacheFilters, async noCacheFilter => {
|
|
|
|
args.push('--no-cache-filter', noCacheFilter);
|
|
|
|
});
|
2021-11-16 04:19:44 +00:00
|
|
|
await asyncForEach(inputs.outputs, async output => {
|
|
|
|
args.push('--output', output);
|
2020-08-16 15:18:08 +00:00
|
|
|
});
|
2021-11-16 04:19:44 +00:00
|
|
|
if (inputs.platforms.length > 0) {
|
|
|
|
args.push('--platform', inputs.platforms.join(','));
|
|
|
|
}
|
2023-01-11 11:12:09 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.10.0')) {
|
|
|
|
if (inputs.provenance) {
|
2023-01-13 09:02:50 +00:00
|
|
|
args.push('--provenance', inputs.provenance);
|
2023-01-11 13:28:05 +00:00
|
|
|
} else if ((await buildx.satisfiesBuildKitVersion(inputs.builder, '>=0.11.0', standalone)) && !hasDockerExport(inputs)) {
|
2023-01-30 18:21:25 +00:00
|
|
|
// if provenance not specified and BuildKit version compatible for
|
|
|
|
// attestation, set default provenance. Also needs to make sure user
|
2023-01-11 13:28:05 +00:00
|
|
|
// doesn't want to explicitly load the image to docker.
|
2023-01-30 18:21:25 +00:00
|
|
|
if (fromPayload('repository.private') !== false) {
|
|
|
|
// 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
|
|
|
|
args.push('--provenance', getProvenanceAttrs(`mode=min,inline-only=true`));
|
|
|
|
} else {
|
|
|
|
// for a public repository, we set max provenance mode.
|
|
|
|
args.push('--provenance', getProvenanceAttrs(`mode=max`));
|
|
|
|
}
|
2023-01-11 11:12:09 +00:00
|
|
|
}
|
|
|
|
if (inputs.sbom) {
|
|
|
|
args.push('--sbom', inputs.sbom);
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 08:07:11 +00:00
|
|
|
await asyncForEach(inputs.secrets, async secret => {
|
2020-11-17 20:38:45 +00:00
|
|
|
try {
|
2021-02-15 09:08:19 +00:00
|
|
|
args.push('--secret', await buildx.getSecretString(secret));
|
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
await asyncForEach(inputs.secretFiles, async secretFile => {
|
|
|
|
try {
|
|
|
|
args.push('--secret', await buildx.getSecretFile(secretFile));
|
2020-11-17 20:38:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
core.warning(err.message);
|
|
|
|
}
|
2020-09-02 08:07:11 +00:00
|
|
|
});
|
2022-08-03 12:38:56 +00:00
|
|
|
if (inputs.githubToken && !buildx.hasGitAuthToken(inputs.secrets) && context.startsWith(defaultContext)) {
|
2021-02-15 09:08:19 +00:00
|
|
|
args.push('--secret', await buildx.getSecretString(`GIT_AUTH_TOKEN=${inputs.githubToken}`));
|
2020-09-22 18:49:18 +00:00
|
|
|
}
|
2021-11-16 06:19:27 +00:00
|
|
|
if (inputs.shmSize) {
|
|
|
|
args.push('--shm-size', inputs.shmSize);
|
|
|
|
}
|
2020-09-10 23:23:49 +00:00
|
|
|
await asyncForEach(inputs.ssh, async ssh => {
|
|
|
|
args.push('--ssh', ssh);
|
|
|
|
});
|
2021-11-16 04:19:44 +00:00
|
|
|
await asyncForEach(inputs.tags, async tag => {
|
|
|
|
args.push('--tag', tag);
|
|
|
|
});
|
|
|
|
if (inputs.target) {
|
|
|
|
args.push('--target', inputs.target);
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
2021-11-16 06:19:27 +00:00
|
|
|
await asyncForEach(inputs.ulimit, async ulimit => {
|
|
|
|
args.push('--ulimit', ulimit);
|
|
|
|
});
|
2020-08-16 15:18:08 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2021-11-16 04:19:44 +00:00
|
|
|
async function getCommonArgs(inputs: Inputs, buildxVersion: string): Promise<Array<string>> {
|
2022-03-15 20:59:52 +00:00
|
|
|
const args: Array<string> = [];
|
2020-09-03 09:49:39 +00:00
|
|
|
if (inputs.builder) {
|
|
|
|
args.push('--builder', inputs.builder);
|
|
|
|
}
|
2020-08-17 20:18:15 +00:00
|
|
|
if (inputs.load) {
|
|
|
|
args.push('--load');
|
|
|
|
}
|
2021-11-16 04:19:44 +00:00
|
|
|
if (buildx.satisfies(buildxVersion, '>=0.6.0')) {
|
|
|
|
args.push('--metadata-file', await buildx.getMetadataFile());
|
|
|
|
}
|
2021-04-06 12:49:15 +00:00
|
|
|
if (inputs.network) {
|
|
|
|
args.push('--network', inputs.network);
|
|
|
|
}
|
2021-11-16 04:19:44 +00:00
|
|
|
if (inputs.noCache) {
|
|
|
|
args.push('--no-cache');
|
|
|
|
}
|
|
|
|
if (inputs.pull) {
|
|
|
|
args.push('--pull');
|
|
|
|
}
|
2020-08-17 20:18:15 +00:00
|
|
|
if (inputs.push) {
|
|
|
|
args.push('--push');
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2020-08-29 15:15:26 +00:00
|
|
|
export async function getInputList(name: string, ignoreComma?: boolean): Promise<string[]> {
|
2022-03-15 20:59:52 +00:00
|
|
|
const res: Array<string> = [];
|
2020-11-17 20:38:45 +00:00
|
|
|
|
2020-08-16 15:18:08 +00:00
|
|
|
const items = core.getInput(name);
|
|
|
|
if (items == '') {
|
2020-11-17 20:38:45 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-04-25 04:47:48 +00:00
|
|
|
const records = await parse(items, {
|
2020-11-17 20:38:45 +00:00
|
|
|
columns: false,
|
2022-04-25 04:47:48 +00:00
|
|
|
relaxQuotes: true,
|
2020-11-17 20:38:45 +00:00
|
|
|
relaxColumnCount: true,
|
2022-04-25 04:47:48 +00:00
|
|
|
skipEmptyLines: true
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const record of records as Array<string[]>) {
|
|
|
|
if (record.length == 1) {
|
|
|
|
res.push(record[0]);
|
2020-11-17 20:38:45 +00:00
|
|
|
continue;
|
|
|
|
} else if (!ignoreComma) {
|
2022-04-25 04:47:48 +00:00
|
|
|
res.push(...record);
|
2020-11-17 20:38:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-04-25 04:47:48 +00:00
|
|
|
res.push(record.join(','));
|
2020-11-17 20:38:45 +00:00
|
|
|
}
|
|
|
|
|
2020-12-05 02:40:39 +00:00
|
|
|
return res.filter(item => item).map(pat => pat.trim());
|
2020-08-16 15:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 00:32:27 +00:00
|
|
|
export const asyncForEach = async (array, callback) => {
|
2020-08-16 15:18:08 +00:00
|
|
|
for (let index = 0; index < array.length; index++) {
|
|
|
|
await callback(array[index], index, array);
|
|
|
|
}
|
|
|
|
};
|
2022-12-20 05:17:29 +00:00
|
|
|
|
2023-01-30 18:21:25 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
function fromPayload(path: string): any {
|
|
|
|
return select(github.context.payload, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
function select(obj: any, path: string): any {
|
|
|
|
if (!obj) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const i = path.indexOf('.');
|
|
|
|
if (i < 0) {
|
|
|
|
return obj[path];
|
|
|
|
}
|
|
|
|
const key = path.slice(0, i);
|
|
|
|
return select(obj[key], path.slice(i + 1));
|
|
|
|
}
|
|
|
|
|
2023-01-13 09:02:50 +00:00
|
|
|
function getProvenanceInput(name: string): string {
|
|
|
|
const input = core.getInput(name);
|
|
|
|
if (!input) {
|
|
|
|
// if input is not set, default values will be set later.
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
const builderID = provenanceBuilderID();
|
|
|
|
try {
|
|
|
|
return core.getBooleanInput(name) ? `builder-id=${builderID}` : 'false';
|
|
|
|
} catch (err) {
|
|
|
|
// not a valid boolean, so we assume it's a string
|
|
|
|
return getProvenanceAttrs(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getProvenanceAttrs(input: string): string {
|
|
|
|
const builderID = provenanceBuilderID();
|
|
|
|
// parse attributes from input
|
2023-01-11 12:20:47 +00:00
|
|
|
const fields = parse(input, {
|
|
|
|
relaxColumnCount: true,
|
|
|
|
skipEmptyLines: true
|
|
|
|
})[0];
|
|
|
|
// check if builder-id attribute exists in the input
|
|
|
|
for (const field of fields) {
|
|
|
|
const parts = field
|
|
|
|
.toString()
|
|
|
|
.split(/(?<=^[^=]+?)=/)
|
|
|
|
.map(item => item.trim());
|
|
|
|
if (parts[0] == 'builder-id') {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if not add builder-id attribute
|
|
|
|
return `${input},builder-id=${builderID}`;
|
|
|
|
}
|
2023-01-11 13:28:05 +00:00
|
|
|
|
|
|
|
function hasDockerExport(inputs: Inputs): boolean {
|
|
|
|
if (inputs.load) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const output of inputs.outputs) {
|
|
|
|
const fields = parse(output, {
|
|
|
|
relaxColumnCount: true,
|
|
|
|
skipEmptyLines: true
|
|
|
|
})[0];
|
|
|
|
for (const field of fields) {
|
|
|
|
const parts = field
|
|
|
|
.toString()
|
|
|
|
.split(/(?<=^[^=]+?)=/)
|
|
|
|
.map(item => item.trim());
|
|
|
|
if (parts.length != 2) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (parts[0] == 'type' && parts[1] == 'docker') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|