@@ -3,11 +3,14 @@ import * as path from 'path';
3
3
import * as core from '@actions/core' ;
4
4
import * as actionsToolkit from '@docker/actions-toolkit' ;
5
5
6
+ import { Buildx } from '@docker/actions-toolkit/lib/buildx/buildx' ;
7
+ import { History as BuildxHistory } from '@docker/actions-toolkit/lib/buildx/history' ;
6
8
import { Context } from '@docker/actions-toolkit/lib/context' ;
7
9
import { Docker } from '@docker/actions-toolkit/lib/docker/docker' ;
8
10
import { Exec } from '@docker/actions-toolkit/lib/exec' ;
9
11
import { GitHub } from '@docker/actions-toolkit/lib/github' ;
10
12
import { Toolkit } from '@docker/actions-toolkit/lib/toolkit' ;
13
+ import { Util } from '@docker/actions-toolkit/lib/util' ;
11
14
12
15
import { BakeDefinition } from '@docker/actions-toolkit/lib/types/buildx/bake' ;
13
16
import { ConfigFile } from '@docker/actions-toolkit/lib/types/docker/docker' ;
@@ -18,7 +21,11 @@ import * as stateHelper from './state-helper';
18
21
actionsToolkit . run (
19
22
// main
20
23
async ( ) => {
24
+ const startedTime = new Date ( ) ;
25
+
21
26
const inputs : context . Inputs = await context . getInputs ( ) ;
27
+ core . debug ( `inputs: ${ JSON . stringify ( inputs ) } ` ) ;
28
+
22
29
const toolkit = new Toolkit ( ) ;
23
30
const gitAuthToken = process . env . BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs [ 'github-token' ] ;
24
31
@@ -119,13 +126,14 @@ actionsToolkit.run(
119
126
} ) ;
120
127
} ) ;
121
128
129
+ let err : Error | undefined ;
122
130
await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
123
131
cwd : inputs . workdir ,
124
132
env : buildEnv ,
125
133
ignoreReturnCode : true
126
134
} ) . then ( res => {
127
135
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' } ` ) ;
129
137
}
130
138
} ) ;
131
139
@@ -137,13 +145,70 @@ actionsToolkit.run(
137
145
core . setOutput ( 'metadata' , metadatadt ) ;
138
146
} ) ;
139
147
}
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
+ }
140
162
} ,
141
163
// post
142
164
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
+ }
143
183
if ( stateHelper . tmpDir . length > 0 ) {
144
184
await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
145
185
fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
146
186
} ) ;
147
187
}
148
188
}
149
189
) ;
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