@@ -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,6 +20,7 @@ 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 ) } ` ) ;
22
26
@@ -87,11 +91,12 @@ actionsToolkit.run(
87
91
core . debug ( `buildCmd.command: ${ buildCmd . command } ` ) ;
88
92
core . debug ( `buildCmd.args: ${ JSON . stringify ( buildCmd . args ) } ` ) ;
89
93
94
+ let err : Error | undefined ;
90
95
await Exec . getExecOutput ( buildCmd . command , buildCmd . args , {
91
96
ignoreReturnCode : true
92
97
} ) . then ( res => {
93
98
if ( res . stderr . length > 0 && res . exitCode != 0 ) {
94
- throw new Error ( `buildx failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
99
+ err = Error ( `buildx failed with: ${ res . stderr . match ( / ( .* ) \s * $ / ) ?. [ 0 ] ?. trim ( ) ?? 'unknown error' } ` ) ;
95
100
}
96
101
} ) ;
97
102
@@ -118,13 +123,62 @@ actionsToolkit.run(
118
123
core . setOutput ( 'metadata' , metadatadt ) ;
119
124
} ) ;
120
125
}
126
+ await core . group ( `Reference` , async ( ) => {
127
+ const ref = await buildRef ( toolkit , startedTime , inputs . builder ) ;
128
+ if ( ref ) {
129
+ core . info ( ref ) ;
130
+ stateHelper . setBuildRef ( ref ) ;
131
+ } else {
132
+ core . warning ( 'No build ref found' ) ;
133
+ }
134
+ } ) ;
135
+ if ( err ) {
136
+ throw err ;
137
+ }
121
138
} ,
122
139
// post
123
140
async ( ) => {
141
+ if ( stateHelper . buildRef . length > 0 ) {
142
+ await core . group ( `Exporting build record` , async ( ) => {
143
+ try {
144
+ const buildxHistory = new BuildxHistory ( ) ;
145
+ const exportRes = await buildxHistory . export ( {
146
+ refs : [ stateHelper . buildRef ]
147
+ } ) ;
148
+ core . info ( `Build record exported to ${ exportRes . dockerbuildFilename } (${ Util . formatFileSize ( exportRes . dockerbuildSize ) } )` ) ;
149
+ await GitHub . uploadArtifact ( {
150
+ filename : exportRes . dockerbuildFilename ,
151
+ mimeType : 'application/gzip' ,
152
+ retentionDays : 90
153
+ } ) ;
154
+ } catch ( e ) {
155
+ core . warning ( e . message ) ;
156
+ }
157
+ } ) ;
158
+ }
124
159
if ( stateHelper . tmpDir . length > 0 ) {
125
160
await core . group ( `Removing temp folder ${ stateHelper . tmpDir } ` , async ( ) => {
126
161
fs . rmSync ( stateHelper . tmpDir , { recursive : true } ) ;
127
162
} ) ;
128
163
}
129
164
}
130
165
) ;
166
+
167
+ async function buildRef ( toolkit : Toolkit , since : Date , builder ?: string ) : Promise < string > {
168
+ // get ref from metadata file
169
+ const ref = toolkit . buildxBuild . resolveRef ( ) ;
170
+ if ( ref ) {
171
+ return ref ;
172
+ }
173
+ // otherwise, look for the very first build ref since the build has started
174
+ if ( ! builder ) {
175
+ const currentBuilder = await toolkit . builder . inspect ( ) ;
176
+ builder = currentBuilder . name ;
177
+ }
178
+ const refs = Buildx . refs ( {
179
+ dir : Buildx . refsDir ,
180
+ builderName : builder ,
181
+ since : since
182
+ } ) ;
183
+ return Object . keys ( refs ) . length > 0 ? Object . keys ( refs ) [ 0 ] : '' ;
184
+ }
0 commit comments