1414 * limitations under the License.
1515 */
1616
17- import { beforeEach , describe , expect , it , jest } from '@jest/globals' ;
17+ import { beforeEach , describe , expect , it , jest , test } from '@jest/globals' ;
18+ import fs from 'fs' ;
1819import * as path from 'path' ;
1920
21+ import { Buildx } from '../src/buildx/buildx' ;
22+ import { Bake } from '../src/buildx/bake' ;
23+ import { Build } from '../src/buildx/build' ;
24+ import { Exec } from '../src/exec' ;
2025import { GitHub } from '../src/github' ;
26+ import { History } from '../src/buildx/history' ;
2127
2228const fixturesDir = path . join ( __dirname , 'fixtures' ) ;
2329
30+ // prettier-ignore
31+ const tmpDir = path . join ( process . env . TEMP || '/tmp' , 'github-jest' ) ;
32+
2433const maybe = ! process . env . GITHUB_ACTIONS || ( process . env . GITHUB_ACTIONS === 'true' && process . env . ImageOS && process . env . ImageOS . startsWith ( 'ubuntu' ) ) ? describe : describe . skip ;
2534
2635beforeEach ( ( ) => {
@@ -39,3 +48,149 @@ maybe('uploadArtifact', () => {
3948 expect ( res ?. url ) . toBeDefined ( ) ;
4049 } ) ;
4150} ) ;
51+
52+ maybe ( 'writeBuildSummary' , ( ) => {
53+ // prettier-ignore
54+ test . each ( [
55+ [
56+ "single" ,
57+ [
58+ 'build' ,
59+ '-f' , path . join ( fixturesDir , 'hello.Dockerfile' ) ,
60+ fixturesDir
61+ ] ,
62+ ] ,
63+ [
64+ "multiplatform" ,
65+ [
66+ 'build' ,
67+ '-f' , path . join ( fixturesDir , 'hello.Dockerfile' ) ,
68+ '--platform' , 'linux/amd64,linux/arm64' ,
69+ fixturesDir
70+ ] ,
71+ ]
72+ ] ) ( 'write build summary %p' , async ( _ , bargs ) => {
73+ const buildx = new Buildx ( ) ;
74+ const build = new Build ( { buildx : buildx } ) ;
75+
76+ fs . mkdirSync ( tmpDir , { recursive : true } ) ;
77+ await expect (
78+ ( async ( ) => {
79+ // prettier-ignore
80+ const buildCmd = await buildx . getCommand ( [
81+ '--builder' , process . env . CTN_BUILDER_NAME ?? 'default' ,
82+ ...bargs ,
83+ '--metadata-file' , build . getMetadataFilePath ( )
84+ ] ) ;
85+ await Exec . exec ( buildCmd . command , buildCmd . args ) ;
86+ } ) ( )
87+ ) . resolves . not . toThrow ( ) ;
88+
89+ const metadata = build . resolveMetadata ( ) ;
90+ expect ( metadata ) . toBeDefined ( ) ;
91+ const buildRef = build . resolveRef ( metadata ) ;
92+ expect ( buildRef ) . toBeDefined ( ) ;
93+
94+ const history = new History ( { buildx : buildx } ) ;
95+ const exportRes = await history . export ( {
96+ refs : [ buildRef ?? '' ]
97+ } ) ;
98+ expect ( exportRes ) . toBeDefined ( ) ;
99+ expect ( exportRes ?. dockerbuildFilename ) . toBeDefined ( ) ;
100+ expect ( exportRes ?. dockerbuildSize ) . toBeDefined ( ) ;
101+ expect ( exportRes ?. summaries ) . toBeDefined ( ) ;
102+
103+ const uploadRes = await GitHub . uploadArtifact ( {
104+ filename : exportRes ?. dockerbuildFilename ,
105+ mimeType : 'application/gzip' ,
106+ retentionDays : 1
107+ } ) ;
108+ expect ( uploadRes ) . toBeDefined ( ) ;
109+ expect ( uploadRes ?. url ) . toBeDefined ( ) ;
110+
111+ await GitHub . writeBuildSummary ( {
112+ exportRes : exportRes ,
113+ uploadRes : uploadRes ,
114+ inputs : {
115+ context : fixturesDir ,
116+ file : path . join ( fixturesDir , 'hello.Dockerfile' )
117+ }
118+ } ) ;
119+ } ) ;
120+
121+ // prettier-ignore
122+ test . each ( [
123+ [
124+ 'single' ,
125+ [
126+ 'bake' ,
127+ '-f' , path . join ( fixturesDir , 'hello-bake.hcl' ) ,
128+ 'hello'
129+ ] ,
130+ ] ,
131+ [
132+ 'group' ,
133+ [
134+ 'bake' ,
135+ '-f' , path . join ( fixturesDir , 'hello-bake.hcl' ) ,
136+ 'hello-all'
137+ ] ,
138+ ] ,
139+ [
140+ 'matrix' ,
141+ [
142+ 'bake' ,
143+ '-f' , path . join ( fixturesDir , 'hello-bake.hcl' ) ,
144+ 'hello-matrix'
145+ ] ,
146+ ]
147+ ] ) ( 'write bake summary %p' , async ( _ , bargs ) => {
148+ const buildx = new Buildx ( ) ;
149+ const bake = new Bake ( { buildx : buildx } ) ;
150+
151+ fs . mkdirSync ( tmpDir , { recursive : true } ) ;
152+ await expect (
153+ ( async ( ) => {
154+ // prettier-ignore
155+ const buildCmd = await buildx . getCommand ( [
156+ '--builder' , process . env . CTN_BUILDER_NAME ?? 'default' ,
157+ ...bargs ,
158+ '--metadata-file' , bake . getMetadataFilePath ( )
159+ ] ) ;
160+ await Exec . exec ( buildCmd . command , buildCmd . args , {
161+ cwd : fixturesDir
162+ } ) ;
163+ } ) ( )
164+ ) . resolves . not . toThrow ( ) ;
165+
166+ const metadata = bake . resolveMetadata ( ) ;
167+ expect ( metadata ) . toBeDefined ( ) ;
168+ const buildRefs = bake . resolveRefs ( metadata ) ;
169+ expect ( buildRefs ) . toBeDefined ( ) ;
170+
171+ const history = new History ( { buildx : buildx } ) ;
172+ const exportRes = await history . export ( {
173+ refs : buildRefs ?? [ ]
174+ } ) ;
175+ expect ( exportRes ) . toBeDefined ( ) ;
176+ expect ( exportRes ?. dockerbuildFilename ) . toBeDefined ( ) ;
177+ expect ( exportRes ?. dockerbuildSize ) . toBeDefined ( ) ;
178+ expect ( exportRes ?. summaries ) . toBeDefined ( ) ;
179+
180+ const uploadRes = await GitHub . uploadArtifact ( {
181+ filename : exportRes ?. dockerbuildFilename ,
182+ mimeType : 'application/gzip' ,
183+ retentionDays : 1
184+ } ) ;
185+ expect ( uploadRes ) . toBeDefined ( ) ;
186+ expect ( uploadRes ?. url ) . toBeDefined ( ) ;
187+
188+ await GitHub . writeBuildSummary ( {
189+ exportRes : exportRes ,
190+ uploadRes : uploadRes ,
191+ inputs : {
192+ files : path . join ( fixturesDir , 'hello-bake.hcl' )
193+ }
194+ } ) ;
195+ } ) ;
196+ } ) ;
0 commit comments