Skip to content

Commit

Permalink
generate build summary
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Jun 14, 2024
1 parent f40a782 commit a29d7e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ export async function getInputs(): Promise<Inputs> {
};
}

export function sanitizeInputs(inputs: Inputs) {
const res = {};
for (const key of Object.keys(inputs)) {
if (key === 'github-token') {
continue;
}
const value: string | string[] | boolean = inputs[key];
if (typeof value === 'boolean' && value === false) {
continue;
} else if (Array.isArray(value) && value.length === 0) {
continue;
} else if (!value) {
continue;
}
if (key === 'workdir' && value === '.') {
continue;
}
res[key] = value;
}
return res;
}

export async function getArgs(inputs: Inputs, definition: BakeDefinition, toolkit: Toolkit): Promise<Array<string>> {
// prettier-ignore
return [
Expand Down
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ actionsToolkit.run(

const inputs: context.Inputs = await context.getInputs();
core.debug(`inputs: ${JSON.stringify(inputs)}`);
stateHelper.setInputs(inputs);

const toolkit = new Toolkit();
const gitAuthToken = process.env.BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs['github-token'];
Expand Down Expand Up @@ -110,6 +111,7 @@ actionsToolkit.run(
if (!definition) {
throw new Error('Bake definition not set');
}
stateHelper.setBakeDefinition(definition);

const args: string[] = await context.getArgs(inputs, definition, toolkit);
const buildCmd = await toolkit.buildx.getCommand(args);
Expand Down Expand Up @@ -170,11 +172,17 @@ actionsToolkit.run(
refs: stateHelper.buildRefs
});
core.info(`Build records exported to ${exportRes.dockerbuildFilename} (${Util.formatFileSize(exportRes.dockerbuildSize)})`);
await GitHub.uploadArtifact({
const uploadRes = await GitHub.uploadArtifact({
filename: exportRes.dockerbuildFilename,
mimeType: 'application/gzip',
retentionDays: 90
});
await GitHub.writeBuildSummary({
exportRes: exportRes,
uploadRes: uploadRes,
inputs: stateHelper.inputs,
bakeDefinition: stateHelper.bakeDefinition
});
} catch (e) {
core.warning(e.message);
}
Expand Down
14 changes: 14 additions & 0 deletions src/state-helper.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import * as core from '@actions/core';

import {BakeDefinition} from '@docker/actions-toolkit/lib/types/buildx/bake';

import {Inputs, sanitizeInputs} from './context';

export const tmpDir = process.env['STATE_tmpDir'] || '';
export const inputs = process.env['STATE_inputs'] ? JSON.parse(process.env['STATE_inputs']) : undefined;
export const bakeDefinition = process.env['STATE_bakeDefinition'] ? <BakeDefinition>JSON.parse(process.env['STATE_bakeDefinition']) : undefined;
export const buildRefs = process.env['STATE_buildRefs'] ? process.env['STATE_buildRefs'].split(',') : [];

export function setTmpDir(tmpDir: string) {
core.saveState('tmpDir', tmpDir);
}

export function setInputs(inputs: Inputs) {
core.saveState('inputs', JSON.stringify(sanitizeInputs(inputs)));
}

export function setBakeDefinition(bakeDefinition: BakeDefinition) {
core.saveState('bakeDefinition', JSON.stringify(bakeDefinition));
}

export function setBuildRefs(buildRefs: Array<string>) {
core.saveState('buildRefs', buildRefs.join(','));
}

0 comments on commit a29d7e9

Please sign in to comment.