Add digest output

Fix platforms and allow inputs

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-08-17 22:18:15 +02:00
parent 44d05b9191
commit 38c1f188ca
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
8 changed files with 244 additions and 68 deletions

View file

@ -1,5 +1,20 @@
import fs from 'fs';
import path from 'path';
import * as context from './context';
import * as exec from './exec';
export async function getImageIDFile(): Promise<string> {
return path.join(context.tmpDir, 'iidfile');
}
export async function getImageID(): Promise<string | undefined> {
const iidFile = await getImageIDFile();
if (!fs.existsSync(iidFile)) {
return undefined;
}
return fs.readFileSync(iidFile, {encoding: 'utf-8'});
}
export async function isAvailable(): Promise<Boolean> {
return await exec.exec(`docker`, ['buildx'], true).then(res => {
if (res.stderr != '' && !res.success) {

View file

@ -1,5 +1,11 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as buildx from './buildx';
import * as core from '@actions/core';
export const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'docker-build-push-'));
export interface Inputs {
context: string;
file: string;
@ -48,6 +54,43 @@ export async function getArgs(inputs: Inputs): Promise<Array<string>> {
return args;
}
async function getBuildArgs(inputs: Inputs): Promise<Array<string>> {
let args: Array<string> = ['build'];
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
});
await asyncForEach(inputs.labels, async label => {
args.push('--label', label);
});
await asyncForEach(inputs.tags, async tag => {
args.push('--tag', tag);
});
if (inputs.target) {
args.push('--target', inputs.target);
}
if (inputs.allow.length > 0) {
args.push('--allow', inputs.allow.join(','));
}
if (inputs.platforms.length > 0) {
args.push('--platform', inputs.platforms.join(','));
} else {
args.push('--iidfile', await buildx.getImageIDFile());
}
await asyncForEach(inputs.outputs, async output => {
args.push('--output', output);
});
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
await asyncForEach(inputs.cacheTo, async cacheTo => {
args.push('--cache-to', cacheTo);
});
if (inputs.file) {
args.push('--file', inputs.file);
}
return args;
}
async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
let args: Array<string> = [];
if (inputs.noCache) {
@ -65,41 +108,6 @@ async function getCommonArgs(inputs: Inputs): Promise<Array<string>> {
return args;
}
async function getBuildArgs(inputs: Inputs): Promise<Array<string>> {
let args: Array<string> = ['build'];
await asyncForEach(inputs.buildArgs, async buildArg => {
args.push('--build-arg', buildArg);
});
await asyncForEach(inputs.labels, async label => {
args.push('--label', label);
});
await asyncForEach(inputs.tags, async tag => {
args.push('--tag', tag);
});
if (inputs.target) {
args.push('--target', inputs.target);
}
if (inputs.allow) {
args.push('--allow', inputs.allow.join(','));
}
if (inputs.platforms) {
args.push('--platform', inputs.platforms.join(','));
}
await asyncForEach(inputs.outputs, async output => {
args.push('--output', output);
});
await asyncForEach(inputs.cacheFrom, async cacheFrom => {
args.push('--cache-from', cacheFrom);
});
await asyncForEach(inputs.cacheTo, async cacheTo => {
args.push('--cache-to', cacheTo);
});
if (inputs.file) {
args.push('--file', inputs.file);
}
return args;
}
export async function getInputList(name: string): Promise<string[]> {
const items = core.getInput(name);
if (items == '') {

View file

@ -25,6 +25,13 @@ async function run(): Promise<void> {
core.info(`🏃 Starting build...`);
const args: string[] = await getArgs(inputs);
await exec.exec('docker', args);
const imageID = await buildx.getImageID();
if (imageID) {
core.info('🛒 Extracting digest...');
core.info(`${imageID}`);
core.setOutput('digest', imageID);
}
} catch (error) {
core.setFailed(error.message);
}