@@ -4,11 +4,14 @@ import * as stateHelper from './state-helper';
4
4
import * as core from '@actions/core' ;
5
5
import * as actionsToolkit from '@docker/actions-toolkit' ;
6
6
7
+ import { Buildx } from '@docker/actions-toolkit/lib/buildx/buildx' ;
8
+ import { History as BuildxHistory } from '@docker/actions-toolkit/lib/buildx/history' ;
7
9
import { Context } from '@docker/actions-toolkit/lib/context' ;
8
10
import { Docker } from '@docker/actions-toolkit/lib/docker/docker' ;
9
11
import { Exec } from '@docker/actions-toolkit/lib/exec' ;
10
12
import { GitHub } from '@docker/actions-toolkit/lib/github' ;
11
13
import { Toolkit } from '@docker/actions-toolkit/lib/toolkit' ;
14
+ import { Util } from '@docker/actions-toolkit/lib/util' ;
12
15
13
16
import { ConfigFile } from '@docker/actions-toolkit/lib/types/docker/docker' ;
14
17
@@ -17,8 +20,10 @@ import * as context from './context';
17
20
actionsToolkit . run (
18
21
// main
19
22
async ( ) => {
23
+ const startedTime = new Date ( ) ;
20
24
const inputs : context . Inputs = await context . getInputs ( ) ;
21
25
core . debug ( `inputs: ${ JSON . stringify ( inputs ) } ` ) ;
26
+ stateHelper . setInputs ( inputs ) ;
22
27
23
28
const toolkit = new Toolkit ( ) ;
24
29
@@ -78,6 +83,7 @@ actionsToolkit.run(
78
83
await core . group ( `Builder info` , async ( ) => {
79
84
const builder = await toolkit . builder . inspect ( inputs . builder ) ;
80
85
core . info ( JSON . stringify ( builder , null , 2 ) ) ;
86
+ stateHelper . setBuilder ( builder ) ;
81
87
} ) ;
82
88
83
89
const args : string [ ] = await context . getArgs ( inputs , toolkit ) ;
@@ -87,11 +93,12 @@ actionsToolkit.run(
87
93
core . debug ( `buildCmd.command: ${ buildCmd . command } ` ) ;
88
94
core . debug ( `buildCmd.args: ${ JSON . stringify ( buildCmd . args ) } ` ) ;
89
95
96
+ let err : Error | undefined ;
90
97
await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
91
98
ignoreReturnCode : true
92
99
} ) . then ( res => {
93
100
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' } ` ) ;
95
102
}
96
103
} ) ;
97
104
@@ -118,13 +125,75 @@ actionsToolkit.run(
118
125
core . setOutput ( 'metadata' , metadatadt ) ;
119
126
} ) ;
120
127
}
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
+ }
121
140
} ,
122
141
// post
123
142
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
+ }
124
174
if ( stateHelper . tmpDir . length > 0 ) {
125
175
await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
126
176
fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
127
177
} ) ;
128
178
}
129
179
}
130
180
) ;
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