@@ -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,11 @@ 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+
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+ }
0 commit comments