Skip to content

Commit acd2771

Browse files
leodidoona-agent
andcommitted
refactor: call getDeterministicMtime() once per build function
Move getDeterministicMtime() calls to the beginning of each build function to avoid duplicate calls and ensure the same timestamp is used throughout. - buildYarn(): call once at start - buildGo(): call once at start - buildDocker(): call once at start (was called 3 times) - buildGeneric(): call once at start (was called 2 times) This is more efficient and guarantees the same timestamp is used for all tar archives created within a single build function. Co-authored-by: Ona <no-reply@ona.com>
1 parent 8ae60c3 commit acd2771

File tree

1 file changed

+12
-30
lines changed

1 file changed

+12
-30
lines changed

pkg/leeway/build.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,12 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (res *p
19341934
return nil, err
19351935
}
19361936

1937+
// Get deterministic mtime for tar archives (call once, reuse throughout)
1938+
mtime, err := p.getDeterministicMtime()
1939+
if err != nil {
1940+
return nil, err
1941+
}
1942+
19371943
// Determine final exportToCache value with proper precedence
19381944
determineDockerExportMode(p, &cfg, buildctx)
19391945

@@ -2075,12 +2081,6 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (res *p
20752081
return subjects, containerDir, nil
20762082
}
20772083

2078-
// Get deterministic mtime for tar archives
2079-
mtime, err := p.getDeterministicMtime()
2080-
if err != nil {
2081-
return nil, err
2082-
}
2083-
20842084
// Create package with improved diagnostic logging
20852085
var pkgcmds [][]string
20862086

@@ -2134,12 +2134,6 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (res *p
21342134
encodedMetadata := base64.StdEncoding.EncodeToString(metadataContent)
21352135
pkgCommands = append(pkgCommands, []string{"sh", "-c", fmt.Sprintf("echo %s | base64 -d > %s", encodedMetadata, dockerMetadataFile)})
21362136

2137-
// Get deterministic mtime for tar archives
2138-
mtime, err := p.getDeterministicMtime()
2139-
if err != nil {
2140-
return nil, err
2141-
}
2142-
21432137
// Prepare for packaging
21442138
sourcePaths := []string{fmt.Sprintf("./%s", dockerImageNamesFiles), fmt.Sprintf("./%s", dockerMetadataFile)}
21452139
if p.C.W.Provenance.Enabled {
@@ -2197,12 +2191,6 @@ func (p *Package) buildDocker(buildctx *buildContext, wd, result string) (res *p
21972191
)
21982192
}
21992193

2200-
// Get deterministic mtime for tar archives
2201-
mtime, err := p.getDeterministicMtime()
2202-
if err != nil {
2203-
return nil, err
2204-
}
2205-
22062194
// Package everything into final tar.gz
22072195
sourcePaths := []string{"./image.tar", fmt.Sprintf("./%s", dockerImageNamesFiles), "./docker-export-metadata.json"}
22082196
if len(cfg.Metadata) > 0 {
@@ -2430,6 +2418,12 @@ func (p *Package) buildGeneric(buildctx *buildContext, wd, result string) (res *
24302418
return nil, xerrors.Errorf("package should have generic config")
24312419
}
24322420

2421+
// Get deterministic mtime for tar archives (call once, reuse throughout)
2422+
mtime, err := p.getDeterministicMtime()
2423+
if err != nil {
2424+
return nil, err
2425+
}
2426+
24332427
// shortcut: no command == empty package
24342428
if len(cfg.Commands) == 0 && len(cfg.Test) == 0 {
24352429
log.WithField("package", p.FullName()).Debug("package has no commands nor test - creating empty tar")
@@ -2458,12 +2452,6 @@ func (p *Package) buildGeneric(buildctx *buildContext, wd, result string) (res *
24582452
}...)
24592453
}
24602454

2461-
// Get deterministic mtime for tar archives
2462-
mtime, err := p.getDeterministicMtime()
2463-
if err != nil {
2464-
return nil, err
2465-
}
2466-
24672455
// Use buildTarCommand directly which will handle compression internally
24682456
var tarCmd []string
24692457
if p.C.W.Provenance.Enabled || p.C.W.SBOM.Enabled {
@@ -2546,12 +2534,6 @@ func (p *Package) buildGeneric(buildctx *buildContext, wd, result string) (res *
25462534
commands = append(commands, cfg.Test...)
25472535
}
25482536

2549-
// Get deterministic mtime for tar archives
2550-
mtime, err := p.getDeterministicMtime()
2551-
if err != nil {
2552-
return nil, err
2553-
}
2554-
25552537
return &packageBuild{
25562538
Commands: map[PackageBuildPhase][][]string{
25572539
PackageBuildPhaseBuild: commands,

0 commit comments

Comments
 (0)