@@ -178,8 +178,10 @@ func (m *Ci) Attest(
178178 digest string ,
179179 // SBOM file
180180 predicate * dagger.File ,
181+ // SBOM type
182+ sbomType string ,
181183) (string , error ) {
182- return dag .Cosign ().AttestKeyless (ctx , digest , predicate , dagger.CosignAttestKeylessOpts {RegistryUsername : registryUsername , RegistryPassword : registryPassword })
184+ return dag .Cosign ().AttestKeyless (ctx , digest , predicate , dagger.CosignAttestKeylessOpts {RegistryUsername : registryUsername , RegistryPassword : registryPassword , SbomType : sbomType })
183185
184186}
185187
@@ -209,17 +211,26 @@ func (m *Ci) BaseTestContainer(_ context.Context, dir *dagger.Directory) *dagger
209211 WithExec ([]string {"bundle" , "install" , "--jobs" , "4" , "--retry" , "3" })
210212}
211213
212- // Publish the Container built from the Dockerfile in the provided registry
213- func (m * Ci ) Publish (ctx context.Context , dir * dagger.Directory , destImage string ) (string , error ) {
214- return m .Build (ctx , dir ).Publish (ctx , destImage )
214+ // Build and Publish the Container from the Dockerfile in the provided registry
215+ func (m * Ci ) BuildAndPublish (ctx context.Context , dir * dagger.Directory , registryAddress string ) (string , error ) {
216+ return m .Build (ctx , dir ).Publish (ctx , registryAddress )
217+ }
218+
219+ // Publish the provided Container to the provided registry
220+ func (m * Ci ) Publish (ctx context.Context , container * dagger.Container , registryAddress string ) (string , error ) {
221+ return container .Publish (ctx , registryAddress )
215222}
216223
217224// Executes all the steps and returns a Results object
218225func (m * Ci ) Ci (
219226 ctx context.Context ,
227+ // source directory
220228 dir * dagger.Directory ,
229+ // registry username for publishing the contaner image
221230 registryUsername string ,
231+ // registry password for publishing the container image
222232 registryPassword * dagger.Secret ,
233+ // registry address registry/repository/image:tag
223234 registryAddress string ,
224235 // ignore linter failures
225236 // +optional
@@ -232,11 +243,11 @@ func (m *Ci) Ci(
232243 sbom := m .Sbom (image )
233244 vulnerabilityScan := m .Vulnscan (sbom )
234245 testReports := m .Test (ctx , dir )
246+ digest , err := m .Publish (ctx , image , registryAddress )
235247
236- digest , err := image .Publish (ctx , registryAddress )
237248 if err == nil {
238249 m .Sign (ctx , registryUsername , registryPassword , digest )
239- m .Attest (ctx , registryUsername , registryPassword , digest , sbom )
250+ m .Attest (ctx , registryUsername , registryPassword , digest , sbom , "cyclonedx" )
240251 }
241252
242253 return & Results {
@@ -252,14 +263,21 @@ func (m *Ci) Ci(
252263// Executes all the steps and returns a directory with the results
253264func (m * Ci ) CiIntegration (
254265 ctx context.Context ,
266+ // source directory
255267 dir * dagger.Directory ,
268+ // registry username for publishing the contaner image
269+ registryUsername string ,
270+ // registry password for publishing the container image
271+ registryPassword * dagger.Secret ,
272+ // registry address registry/repository/image:tag
273+ registryAddress string ,
256274 // ignore linter failures
257275 // +optional
258276 // +default=false
259277 pass bool ,
260278) * dagger.Directory {
261279 var wg sync.WaitGroup
262- wg .Add (3 )
280+ wg .Add (5 )
263281
264282 var lintOutput = func () * dagger.File {
265283 defer wg .Done ()
@@ -271,16 +289,15 @@ func (m *Ci) CiIntegration(
271289 return m .Sast (dir )
272290 }()
273291
274- /*
275- var vulnerabilityScan = func() *dagger.File {
276- defer wg.Done()
277- return m.Vulnscan(m.Sbom(m.Build(ctx, dir)))
278- }()
279- var image = func() *dagger.Container {
280- defer wg.Done()
281- return m.Build(ctx, dir)
282- }()
283- */
292+ var vulnerabilityScan = func () * dagger.File {
293+ defer wg .Done ()
294+ return m .Vulnscan (m .Sbom (m .Build (ctx , dir )))
295+ }()
296+
297+ var image = func () * dagger.Container {
298+ defer wg .Done ()
299+ return m .Build (ctx , dir )
300+ }()
284301
285302 var testReports = func () * dagger.Directory {
286303 defer wg .Done ()
@@ -292,15 +309,39 @@ func (m *Ci) CiIntegration(
292309
293310 // TODO: fail on errors of the functions!
294311
312+ // After linting, scanning and testing is done, we can create the sbom and publish the image
313+
314+ wg .Add (2 )
315+
316+ var sbom = func () * dagger.File {
317+ defer wg .Done ()
318+ return m .Sbom (image )
319+ }()
320+
321+ digest , err := func () (string , error ) {
322+ defer wg .Done ()
323+ return m .Publish (ctx , image , registryAddress )
324+ }()
325+
326+ // This Blocks the execution until its counter become 0
327+ wg .Wait ()
328+
329+ // After publishing the image, we can sign and attest
330+
331+ if err == nil {
332+ m .Sign (ctx , registryUsername , registryPassword , digest )
333+ m .Attest (ctx , registryUsername , registryPassword , digest , sbom , "cyclonedx" )
334+ }
335+
295336 lintOutputName , _ := lintOutput .Name (ctx )
296337 securityScanName , _ := securityScan .Name (ctx )
297- // vulnerabilityScanName, _ := vulnerabilityScan.Name(ctx)
338+ vulnerabilityScanName , _ := vulnerabilityScan .Name (ctx )
298339 result_container := dag .Container ().
299340 WithWorkdir ("/tmp/out" ).
300341 WithFile (fmt .Sprintf ("/tmp/out/lint/%s" , lintOutputName ), lintOutput ).
301342 WithFile (fmt .Sprintf ("/tmp/out/scan/%s" , securityScanName ), securityScan ).
302- WithDirectory ("/tmp/out/unit-tests/" , testReports )
303- // WithFile(fmt.Sprintf("/tmp/out/vuln/%s", vulnerabilityScanName), vulnerabilityScan)
343+ WithDirectory ("/tmp/out/unit-tests/" , testReports ).
344+ WithFile (fmt .Sprintf ("/tmp/out/vuln/%s" , vulnerabilityScanName ), vulnerabilityScan )
304345 return result_container .
305346 Directory ("." )
306347}
0 commit comments