Skip to content

Commit f40a782

Browse files
committed
export build record and upload artifact
Signed-off-by: CrazyMax <[email protected]>
1 parent bc96707 commit f40a782

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/main.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import * as path from 'path';
33
import * as core from '@actions/core';
44
import * as actionsToolkit from '@docker/actions-toolkit';
55

6+
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx';
7+
import {History as BuildxHistory} from '@docker/actions-toolkit/lib/buildx/history';
68
import {Context} from '@docker/actions-toolkit/lib/context';
79
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
810
import {Exec} from '@docker/actions-toolkit/lib/exec';
911
import {GitHub} from '@docker/actions-toolkit/lib/github';
1012
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';
13+
import {Util} from '@docker/actions-toolkit/lib/util';
1114

1215
import {BakeDefinition} from '@docker/actions-toolkit/lib/types/buildx/bake';
1316
import {ConfigFile} from '@docker/actions-toolkit/lib/types/docker/docker';
@@ -18,7 +21,11 @@ import * as stateHelper from './state-helper';
1821
actionsToolkit.run(
1922
// main
2023
async () => {
24+
const startedTime = new Date();
25+
2126
const inputs: context.Inputs = await context.getInputs();
27+
core.debug(`inputs: ${JSON.stringify(inputs)}`);
28+
2229
const toolkit = new Toolkit();
2330
const gitAuthToken = process.env.BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs['github-token'];
2431

@@ -119,13 +126,14 @@ actionsToolkit.run(
119126
});
120127
});
121128

129+
let err: Error | undefined;
122130
await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
123131
cwd: inputs.workdir,
124132
env: buildEnv,
125133
ignoreReturnCode: true
126134
}).then(res => {
127135
if (res.stderr.length > 0 && res.exitCode != 0) {
128-
throw new Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
136+
err = Error(`buildx bake failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
129137
}
130138
});
131139

@@ -137,13 +145,70 @@ actionsToolkit.run(
137145
core.setOutput('metadata', metadatadt);
138146
});
139147
}
148+
await core.group(`Build references`, async () => {
149+
const refs = await buildRefs(toolkit, startedTime, inputs.builder);
150+
if (refs) {
151+
for (const ref of refs) {
152+
core.info(ref);
153+
}
154+
stateHelper.setBuildRefs(refs);
155+
} else {
156+
core.warning('No build refs found');
157+
}
158+
});
159+
if (err) {
160+
throw err;
161+
}
140162
},
141163
// post
142164
async () => {
165+
if (stateHelper.buildRefs.length > 0) {
166+
await core.group(`Generating build summary`, async () => {
167+
try {
168+
const buildxHistory = new BuildxHistory();
169+
const exportRes = await buildxHistory.export({
170+
refs: stateHelper.buildRefs
171+
});
172+
core.info(`Build records exported to ${exportRes.dockerbuildFilename} (${Util.formatFileSize(exportRes.dockerbuildSize)})`);
173+
await GitHub.uploadArtifact({
174+
filename: exportRes.dockerbuildFilename,
175+
mimeType: 'application/gzip',
176+
retentionDays: 90
177+
});
178+
} catch (e) {
179+
core.warning(e.message);
180+
}
181+
});
182+
}
143183
if (stateHelper.tmpDir.length > 0) {
144184
await core.group(`Removing temp folder ${stateHelper.tmpDir}`, async () => {
145185
fs.rmSync(stateHelper.tmpDir, {recursive: true});
146186
});
147187
}
148188
}
149189
);
190+
191+
async function buildRefs(toolkit: Toolkit, since: Date, builder?: string): Promise<Array<string>> {
192+
// get refs from metadata file
193+
const metaRefs = toolkit.buildxBake.resolveRefs();
194+
if (metaRefs) {
195+
return metaRefs;
196+
}
197+
// otherwise, look for the very first build ref since the build has started
198+
if (!builder) {
199+
const currentBuilder = await toolkit.builder.inspect();
200+
builder = currentBuilder.name;
201+
}
202+
const res = Buildx.refs({
203+
dir: Buildx.refsDir,
204+
builderName: builder,
205+
since: since
206+
});
207+
const refs: Array<string> = [];
208+
for (const ref in res) {
209+
if (Object.prototype.hasOwnProperty.call(res, ref)) {
210+
refs.push(ref);
211+
}
212+
}
213+
return refs;
214+
}

src/state-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as core from '@actions/core';
22

33
export const tmpDir = process.env['STATE_tmpDir'] || '';
4+
export const buildRefs = process.env['STATE_buildRefs'] ? process.env['STATE_buildRefs'].split(',') : [];
45

56
export function setTmpDir(tmpDir: string) {
67
core.saveState('tmpDir', tmpDir);
78
}
9+
10+
export function setBuildRefs(buildRefs: Array<string>) {
11+
core.saveState('buildRefs', buildRefs.join(','));
12+
}

0 commit comments

Comments
 (0)