Skip to content

Commit e51051a

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

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/main.ts

+55-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import * as stateHelper from './state-helper';
44
import * as core from '@actions/core';
55
import * as actionsToolkit from '@docker/actions-toolkit';
66

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

1316
import {ConfigFile} from '@docker/actions-toolkit/lib/types/docker/docker';
1417

@@ -17,6 +20,7 @@ import * as context from './context';
1720
actionsToolkit.run(
1821
// main
1922
async () => {
23+
const startedTime = new Date();
2024
const inputs: context.Inputs = await context.getInputs();
2125
core.debug(`inputs: ${JSON.stringify(inputs)}`);
2226

@@ -87,11 +91,12 @@ actionsToolkit.run(
8791
core.debug(`buildCmd.command: ${buildCmd.command}`);
8892
core.debug(`buildCmd.args: ${JSON.stringify(buildCmd.args)}`);
8993

94+
let err: Error | undefined;
9095
await Exec.getExecOutput(buildCmd.command, buildCmd.args, {
9196
ignoreReturnCode: true
9297
}).then(res => {
9398
if (res.stderr.length > 0 && res.exitCode != 0) {
94-
throw new Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
99+
err = Error(`buildx failed with: ${res.stderr.match(/(.*)\s*$/)?.[0]?.trim() ?? 'unknown error'}`);
95100
}
96101
});
97102

@@ -118,13 +123,62 @@ actionsToolkit.run(
118123
core.setOutput('metadata', metadatadt);
119124
});
120125
}
126+
await core.group(`Reference`, async () => {
127+
const ref = await buildRef(toolkit, startedTime, inputs.builder);
128+
if (ref) {
129+
core.info(ref);
130+
stateHelper.setBuildRef(ref);
131+
} else {
132+
core.warning('No build ref found');
133+
}
134+
});
135+
if (err) {
136+
throw err;
137+
}
121138
},
122139
// post
123140
async () => {
141+
if (stateHelper.buildRef.length > 0) {
142+
await core.group(`Exporting build record`, async () => {
143+
try {
144+
const buildxHistory = new BuildxHistory();
145+
const exportRes = await buildxHistory.export({
146+
refs: [stateHelper.buildRef]
147+
});
148+
core.info(`Build record exported to ${exportRes.dockerbuildFilename} (${Util.formatFileSize(exportRes.dockerbuildSize)})`);
149+
await GitHub.uploadArtifact({
150+
filename: exportRes.dockerbuildFilename,
151+
mimeType: 'application/gzip',
152+
retentionDays: 90
153+
});
154+
} catch (e) {
155+
core.warning(e.message);
156+
}
157+
});
158+
}
124159
if (stateHelper.tmpDir.length > 0) {
125160
await core.group(`Removing temp folder ${stateHelper.tmpDir}`, async () => {
126161
fs.rmSync(stateHelper.tmpDir, {recursive: true});
127162
});
128163
}
129164
}
130165
);
166+
167+
async function buildRef(toolkit: Toolkit, since: Date, builder?: string): Promise<string> {
168+
// get ref from metadata file
169+
const ref = toolkit.buildxBuild.resolveRef();
170+
if (ref) {
171+
return ref;
172+
}
173+
// otherwise, look for the very first build ref since the build has started
174+
if (!builder) {
175+
const currentBuilder = await toolkit.builder.inspect();
176+
builder = currentBuilder.name;
177+
}
178+
const refs = Buildx.refs({
179+
dir: Buildx.refsDir,
180+
builderName: builder,
181+
since: since
182+
});
183+
return Object.keys(refs).length > 0 ? Object.keys(refs)[0] : '';
184+
}

src/state-helper.ts

+5
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 buildRef = process.env['STATE_buildRef'] || '';
45

56
export function setTmpDir(tmpDir: string) {
67
core.saveState('tmpDir', tmpDir);
78
}
9+
10+
export function setBuildRef(buildRef: string) {
11+
core.saveState('buildRef', buildRef);
12+
}

0 commit comments

Comments
 (0)