mirror of
https://github.com/docker/build-push-action.git
synced 2025-04-19 01:46:45 +00:00
Build push action v2
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
3f08c86128
commit
99bea387ee
22 changed files with 2778 additions and 314 deletions
18
src/buildx.ts
Normal file
18
src/buildx.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import * as exec from './exec';
|
||||
|
||||
export async function isAvailable(): Promise<Boolean> {
|
||||
return await exec.exec(`docker`, ['buildx'], true).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
return false;
|
||||
}
|
||||
return res.success;
|
||||
});
|
||||
}
|
||||
|
||||
export async function use(builder: string): Promise<void> {
|
||||
return await exec.exec(`docker`, ['buildx', 'use', '--builder', builder], false).then(res => {
|
||||
if (res.stderr != '' && !res.success) {
|
||||
throw new Error(res.stderr);
|
||||
}
|
||||
});
|
||||
}
|
58
src/context-helper.ts
Normal file
58
src/context-helper.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import * as core from '@actions/core';
|
||||
|
||||
export interface Inputs {
|
||||
context: string;
|
||||
file: string;
|
||||
buildArgs: string[];
|
||||
labels: string[];
|
||||
tags: string[];
|
||||
pull: boolean;
|
||||
target: string;
|
||||
noCache: boolean;
|
||||
builder: string;
|
||||
platforms: string;
|
||||
load: boolean;
|
||||
push: boolean;
|
||||
outputs: string[];
|
||||
cacheFrom: string[];
|
||||
cacheTo: string[];
|
||||
}
|
||||
|
||||
export async function loadInputs(): Promise<Inputs> {
|
||||
return {
|
||||
context: core.getInput('context') || '.',
|
||||
file: core.getInput('file') || './Dockerfile',
|
||||
buildArgs: await getInputList('build-args'),
|
||||
labels: await getInputList('labels'),
|
||||
tags: await getInputList('tags'),
|
||||
pull: /true/i.test(core.getInput('pull')),
|
||||
target: core.getInput('target'),
|
||||
noCache: /true/i.test(core.getInput('no-cache')),
|
||||
builder: core.getInput('builder'),
|
||||
platforms: core.getInput('platforms'),
|
||||
load: /true/i.test(core.getInput('load')),
|
||||
push: /true/i.test(core.getInput('push')),
|
||||
outputs: await getInputList('outputs'),
|
||||
cacheFrom: await getInputList('cache-from'),
|
||||
cacheTo: await getInputList('cache-to')
|
||||
};
|
||||
}
|
||||
|
||||
export async function mustBuildx(inputs: Inputs): Promise<boolean> {
|
||||
return (
|
||||
inputs.builder.length > 0 ||
|
||||
inputs.platforms.length > 0 ||
|
||||
inputs.push ||
|
||||
inputs.outputs.length > 0 ||
|
||||
inputs.cacheFrom.length > 0 ||
|
||||
inputs.cacheTo.length > 0
|
||||
);
|
||||
}
|
||||
|
||||
async function getInputList(name: string): Promise<string[]> {
|
||||
const items = core.getInput(name);
|
||||
if (items == '') {
|
||||
return [];
|
||||
}
|
||||
return items.split(/\r?\n/).reduce<string[]>((acc, line) => acc.concat(line.split(',')).map(pat => pat.trim()), []);
|
||||
}
|
34
src/exec.ts
Normal file
34
src/exec.ts
Normal file
|
@ -0,0 +1,34 @@
|
|||
import * as aexec from '@actions/exec';
|
||||
import {ExecOptions} from '@actions/exec';
|
||||
|
||||
export interface ExecResult {
|
||||
success: boolean;
|
||||
stdout: string;
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
export const exec = async (command: string, args: string[] = [], silent: boolean): Promise<ExecResult> => {
|
||||
let stdout: string = '';
|
||||
let stderr: string = '';
|
||||
|
||||
const options: ExecOptions = {
|
||||
silent: silent,
|
||||
ignoreReturnCode: true
|
||||
};
|
||||
options.listeners = {
|
||||
stdout: (data: Buffer) => {
|
||||
stdout += data.toString();
|
||||
},
|
||||
stderr: (data: Buffer) => {
|
||||
stderr += data.toString();
|
||||
}
|
||||
};
|
||||
|
||||
const returnCode: number = await aexec.exec(command, args, options);
|
||||
|
||||
return {
|
||||
success: returnCode === 0,
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim()
|
||||
};
|
||||
};
|
85
src/main.ts
Normal file
85
src/main.ts
Normal file
|
@ -0,0 +1,85 @@
|
|||
import * as os from 'os';
|
||||
import * as buildx from './buildx';
|
||||
import {Inputs, loadInputs, mustBuildx} from './context-helper';
|
||||
import * as core from '@actions/core';
|
||||
import * as exec from '@actions/exec';
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
if (os.platform() !== 'linux') {
|
||||
core.setFailed('Only supported on linux platform');
|
||||
return;
|
||||
}
|
||||
|
||||
const inputs: Inputs = await loadInputs();
|
||||
let buildArgs: Array<string> = [];
|
||||
|
||||
// Check buildx
|
||||
if (await mustBuildx(inputs)) {
|
||||
if (await !buildx.isAvailable()) {
|
||||
throw new Error(`Buildx is required but not available`);
|
||||
}
|
||||
core.info(`🚀 Buildx will be used to build your image`);
|
||||
buildArgs.push('buildx', 'build');
|
||||
} else {
|
||||
buildArgs.push('build');
|
||||
}
|
||||
|
||||
if (inputs.file) {
|
||||
buildArgs.push('--file', inputs.file);
|
||||
}
|
||||
await asyncForEach(inputs.buildArgs, async buildArg => {
|
||||
buildArgs.push('--build-arg', buildArg);
|
||||
});
|
||||
await asyncForEach(inputs.labels, async label => {
|
||||
buildArgs.push('--label', label);
|
||||
});
|
||||
await asyncForEach(inputs.tags, async tag => {
|
||||
buildArgs.push('--tag', tag);
|
||||
});
|
||||
if (inputs.pull) {
|
||||
buildArgs.push('--pull');
|
||||
}
|
||||
if (inputs.target) {
|
||||
buildArgs.push('--target', inputs.target);
|
||||
}
|
||||
if (inputs.noCache) {
|
||||
buildArgs.push('--no-cache');
|
||||
}
|
||||
if (inputs.builder) {
|
||||
core.info(`📌 Using build instance ${inputs.builder}`);
|
||||
await buildx.use(inputs.builder);
|
||||
}
|
||||
if (inputs.platforms) {
|
||||
buildArgs.push('--platform', inputs.platforms);
|
||||
}
|
||||
if (inputs.load) {
|
||||
buildArgs.push('--load');
|
||||
}
|
||||
if (inputs.push) {
|
||||
buildArgs.push('--push');
|
||||
}
|
||||
await asyncForEach(inputs.outputs, async output => {
|
||||
buildArgs.push('--output', output);
|
||||
});
|
||||
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
|
||||
buildArgs.push('--cache-from', cacheFrom);
|
||||
});
|
||||
await asyncForEach(inputs.cacheTo, async cacheTo => {
|
||||
buildArgs.push('--cache-from', cacheTo);
|
||||
});
|
||||
|
||||
core.info(`🏃 Starting build...`);
|
||||
await exec.exec('docker', buildArgs);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
const asyncForEach = async (array, callback) => {
|
||||
for (let index = 0; index < array.length; index++) {
|
||||
await callback(array[index], index, array);
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
Loading…
Add table
Add a link
Reference in a new issue