@@ -3,11 +3,14 @@ import * as path from 'path';
33import * as core from '@actions/core' ;
44import * 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' ;
68import { Context } from '@docker/actions-toolkit/lib/context' ;
79import { Docker } from '@docker/actions-toolkit/lib/docker/docker' ;
810import { Exec } from '@docker/actions-toolkit/lib/exec' ;
911import { GitHub } from '@docker/actions-toolkit/lib/github' ;
1012import { Toolkit } from '@docker/actions-toolkit/lib/toolkit' ;
13+ import { Util } from '@docker/actions-toolkit/lib/util' ;
1114
1215import { BakeDefinition } from '@docker/actions-toolkit/lib/types/buildx/bake' ;
1316import { ConfigFile } from '@docker/actions-toolkit/lib/types/docker/docker' ;
@@ -18,7 +21,12 @@ import * as stateHelper from './state-helper';
1821actionsToolkit . 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+ stateHelper . setInputs ( inputs ) ;
29+
2230 const toolkit = new Toolkit ( ) ;
2331 const gitAuthToken = process . env . BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs [ 'github-token' ] ;
2432
@@ -78,6 +86,7 @@ actionsToolkit.run(
7886 await core . group ( `Builder info` , async ( ) => {
7987 const builder = await toolkit . builder . inspect ( inputs . builder ) ;
8088 core . info ( JSON . stringify ( builder , null , 2 ) ) ;
89+ stateHelper . setBuilder ( builder ) ;
8190 } ) ;
8291
8392 let definition : BakeDefinition | undefined ;
@@ -103,6 +112,7 @@ actionsToolkit.run(
103112 if ( ! definition ) {
104113 throw new Error ( 'Bake definition not set' ) ;
105114 }
115+ stateHelper . setBakeDefinition ( definition ) ;
106116
107117 const args : string [ ] = await context . getArgs ( inputs , definition , toolkit ) ;
108118 const buildCmd = await toolkit . buildx . getCommand ( args ) ;
@@ -119,13 +129,14 @@ actionsToolkit.run(
119129 } ) ;
120130 } ) ;
121131
132+ let err : Error | undefined ;
122133 await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
123134 cwd : inputs . workdir ,
124135 env : buildEnv ,
125136 ignoreReturnCode : true
126137 } ) . then ( res => {
127138 if ( res . stderr . length > 0 && res . exitCode != 0 ) {
128- throw new Error ( `buildx bake failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
139+ err = Error ( `buildx bake failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
129140 }
130141 } ) ;
131142
@@ -137,13 +148,84 @@ actionsToolkit.run(
137148 core . setOutput ( 'metadata' , metadatadt ) ;
138149 } ) ;
139150 }
151+ await core . group ( `Build references` , async ( ) => {
152+ const refs = await buildRefs ( toolkit , startedTime , inputs . builder ) ;
153+ if ( refs ) {
154+ for ( const ref of refs ) {
155+ core . info ( ref ) ;
156+ }
157+ stateHelper . setBuildRefs ( refs ) ;
158+ } else {
159+ core . warning ( 'No build refs found' ) ;
160+ }
161+ } ) ;
162+ if ( err ) {
163+ throw err ;
164+ }
140165 } ,
141166 // post
142167 async ( ) => {
168+ if ( stateHelper . buildRefs . length > 0 ) {
169+ await core . group ( `Generating build summary` , async ( ) => {
170+ if ( process . env . DOCKER_BUILD_NO_SUMMARY && Util . parseBool ( process . env . DOCKER_BUILD_NO_SUMMARY ) ) {
171+ core . info ( 'Summary disabled' ) ;
172+ return ;
173+ }
174+ if ( stateHelper . builder && stateHelper . builder . driver === 'cloud' ) {
175+ core . info ( 'Summary is not yet supported with Docker Build Cloud' ) ;
176+ return ;
177+ }
178+ try {
179+ const buildxHistory = new BuildxHistory ( ) ;
180+ const exportRes = await buildxHistory . export ( {
181+ refs : stateHelper . buildRefs
182+ } ) ;
183+ core . info ( `Build records exported to ${ exportRes . dockerbuildFilename } (${ Util . formatFileSize ( exportRes . dockerbuildSize ) } )` ) ;
184+ const uploadRes = await GitHub . uploadArtifact ( {
185+ filename : exportRes . dockerbuildFilename ,
186+ mimeType : 'application/gzip' ,
187+ retentionDays : 90
188+ } ) ;
189+ await GitHub . writeBuildSummary ( {
190+ exportRes : exportRes ,
191+ uploadRes : uploadRes ,
192+ inputs : stateHelper . inputs ,
193+ bakeDefinition : stateHelper . bakeDefinition
194+ } ) ;
195+ } catch ( e ) {
196+ core . warning ( e . message ) ;
197+ }
198+ } ) ;
199+ }
143200 if ( stateHelper . tmpDir . length > 0 ) {
144201 await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
145202 fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
146203 } ) ;
147204 }
148205 }
149206) ;
207+
208+ async function buildRefs ( toolkit : Toolkit , since : Date , builder ?: string ) : Promise < Array < string > > {
209+ // get refs from metadata file
210+ const metaRefs = toolkit . buildxBake . resolveRefs ( ) ;
211+ if ( metaRefs ) {
212+ return metaRefs ;
213+ }
214+ // otherwise, look for the very first build ref since the build has started
215+ if ( ! builder ) {
216+ const currentBuilder = await toolkit . builder . inspect ( ) ;
217+ builder = currentBuilder . name ;
218+ }
219+ const res = Buildx . refs ( {
220+ dir : Buildx . refsDir ,
221+ builderName : builder ,
222+ since : since
223+ } ) ;
224+ const refs : Array < string > = [ ] ;
225+ for ( const ref in res ) {
226+ if ( Object . prototype . hasOwnProperty . call ( res , ref ) ) {
227+ refs . push ( ref ) ;
228+ }
229+ }
230+ return refs ;
231+ }
0 commit comments