@@ -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,12 @@ 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
+ stateHelper . setInputs ( inputs ) ;
29
+
22
30
const toolkit = new Toolkit ( ) ;
23
31
const gitAuthToken = process . env . BUILDX_BAKE_GIT_AUTH_TOKEN ?? inputs [ 'github-token' ] ;
24
32
@@ -78,6 +86,7 @@ actionsToolkit.run(
78
86
await core . group ( `Builder info` , async ( ) => {
79
87
const builder = await toolkit . builder . inspect ( inputs . builder ) ;
80
88
core . info ( JSON . stringify ( builder , null , 2 ) ) ;
89
+ stateHelper . setBuilder ( builder ) ;
81
90
} ) ;
82
91
83
92
let definition : BakeDefinition | undefined ;
@@ -103,6 +112,7 @@ actionsToolkit.run(
103
112
if ( ! definition ) {
104
113
throw new Error ( 'Bake definition not set' ) ;
105
114
}
115
+ stateHelper . setBakeDefinition ( definition ) ;
106
116
107
117
const args : string [ ] = await context . getArgs ( inputs , definition , toolkit ) ;
108
118
const buildCmd = await toolkit . buildx . getCommand ( args ) ;
@@ -119,13 +129,14 @@ actionsToolkit.run(
119
129
} ) ;
120
130
} ) ;
121
131
132
+ let err : Error | undefined ;
122
133
await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
123
134
cwd : inputs . workdir ,
124
135
env : buildEnv ,
125
136
ignoreReturnCode : true
126
137
} ) . then ( res => {
127
138
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' } ` ) ;
129
140
}
130
141
} ) ;
131
142
@@ -137,13 +148,84 @@ actionsToolkit.run(
137
148
core . setOutput ( 'metadata' , metadatadt ) ;
138
149
} ) ;
139
150
}
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
+ }
140
165
} ,
141
166
// post
142
167
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
+ }
143
200
if ( stateHelper . tmpDir . length > 0 ) {
144
201
await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
145
202
fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
146
203
} ) ;
147
204
}
148
205
}
149
206
) ;
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