@@ -4,11 +4,14 @@ import * as stateHelper from './state-helper';
44import * as core from '@actions/core' ;
55import * 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' ;
79import { Context } from '@docker/actions-toolkit/lib/context' ;
810import { Docker } from '@docker/actions-toolkit/lib/docker/docker' ;
911import { Exec } from '@docker/actions-toolkit/lib/exec' ;
1012import { GitHub } from '@docker/actions-toolkit/lib/github' ;
1113import { Toolkit } from '@docker/actions-toolkit/lib/toolkit' ;
14+ import { Util } from '@docker/actions-toolkit/lib/util' ;
1215
1316import { ConfigFile } from '@docker/actions-toolkit/lib/types/docker/docker' ;
1417
@@ -17,8 +20,10 @@ import * as context from './context';
1720actionsToolkit . 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 ) } ` ) ;
26+ stateHelper . setInputs ( inputs ) ;
2227
2328 const toolkit = new Toolkit ( ) ;
2429
@@ -78,6 +83,7 @@ actionsToolkit.run(
7883 await core . group ( `Builder info` , async ( ) => {
7984 const builder = await toolkit . builder . inspect ( inputs . builder ) ;
8085 core . info ( JSON . stringify ( builder , null , 2 ) ) ;
86+ stateHelper . setBuilder ( builder ) ;
8187 } ) ;
8288
8389 const args : string [ ] = await context . getArgs ( inputs , toolkit ) ;
@@ -87,11 +93,12 @@ actionsToolkit.run(
8793 core . debug ( `buildCmd.command: ${ buildCmd . command } ` ) ;
8894 core . debug ( `buildCmd.args: ${ JSON . stringify ( buildCmd . args ) } ` ) ;
8995
96+ let err : Error | undefined ;
9097 await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
9198 ignoreReturnCode : true
9299 } ) . then ( res => {
93100 if ( res . stderr . length > 0 && res . exitCode != 0 ) {
94- throw new Error ( `buildx failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
101+ err = Error ( `buildx failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
95102 }
96103 } ) ;
97104
@@ -118,13 +125,75 @@ actionsToolkit.run(
118125 core . setOutput ( 'metadata' , metadatadt ) ;
119126 } ) ;
120127 }
128+ await core . group ( `Reference` , async ( ) => {
129+ const ref = await buildRef ( toolkit , startedTime , inputs . builder ) ;
130+ if ( ref ) {
131+ core . info ( ref ) ;
132+ stateHelper . setBuildRef ( ref ) ;
133+ } else {
134+ core . warning ( 'No build ref found' ) ;
135+ }
136+ } ) ;
137+ if ( err ) {
138+ throw err ;
139+ }
121140 } ,
122141 // post
123142 async ( ) => {
143+ if ( stateHelper . buildRef . length > 0 ) {
144+ await core . group ( `Generating build summary` , async ( ) => {
145+ if ( process . env . DOCKER_BUILD_NO_SUMMARY && Util . parseBool ( process . env . DOCKER_BUILD_NO_SUMMARY ) ) {
146+ core . info ( 'Summary disabled' ) ;
147+ return ;
148+ }
149+ if ( stateHelper . builder && stateHelper . builder . driver === 'cloud' ) {
150+ core . info ( 'Summary is not yet supported with Docker Build Cloud' ) ;
151+ return ;
152+ }
153+ try {
154+ const buildxHistory = new BuildxHistory ( ) ;
155+ const exportRes = await buildxHistory . export ( {
156+ refs : [ stateHelper . buildRef ]
157+ } ) ;
158+ core . info ( `Build record exported to ${ exportRes . dockerbuildFilename } (${ Util . formatFileSize ( exportRes . dockerbuildSize ) } )` ) ;
159+ const uploadRes = await GitHub . uploadArtifact ( {
160+ filename : exportRes . dockerbuildFilename ,
161+ mimeType : 'application/gzip' ,
162+ retentionDays : 90
163+ } ) ;
164+ await GitHub . writeBuildSummary ( {
165+ exportRes : exportRes ,
166+ uploadRes : uploadRes ,
167+ inputs : stateHelper . inputs
168+ } ) ;
169+ } catch ( e ) {
170+ core . warning ( e . message ) ;
171+ }
172+ } ) ;
173+ }
124174 if ( stateHelper . tmpDir . length > 0 ) {
125175 await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
126176 fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
127177 } ) ;
128178 }
129179 }
130180) ;
181+
182+ async function buildRef ( toolkit : Toolkit , since : Date , builder ?: string ) : Promise < string > {
183+ // get ref from metadata file
184+ const ref = toolkit . buildxBuild . resolveRef ( ) ;
185+ if ( ref ) {
186+ return ref ;
187+ }
188+ // otherwise, look for the very first build ref since the build has started
189+ if ( ! builder ) {
190+ const currentBuilder = await toolkit . builder . inspect ( ) ;
191+ builder = currentBuilder . name ;
192+ }
193+ const refs = Buildx . refs ( {
194+ dir : Buildx . refsDir ,
195+ builderName : builder ,
196+ since : since
197+ } ) ;
198+ return Object . keys ( refs ) . length > 0 ? Object . keys ( refs ) [ 0 ] : '' ;
199+ }
0 commit comments