@@ -3,11 +3,13 @@ package pipeline
33import (
44 "context"
55 "fmt"
6+ "time"
67
78 "dagger/harbor-cli/internal/dagger"
9+ "dagger/harbor-cli/utils"
810)
911
10- func (s * Pipeline ) Build (ctx context.Context , dist * dagger.Directory , GO_VERSION string ) (* dagger.Directory , error ) {
12+ func (s * Pipeline ) Build (ctx context.Context , dist * dagger.Directory ) (* dagger.Directory , error ) {
1113 goos := []string {"linux" , "darwin" , "windows" }
1214 goarch := []string {"amd64" , "arm64" }
1315
@@ -20,19 +22,25 @@ func (s *Pipeline) Build(ctx context.Context, dist *dagger.Directory, GO_VERSION
2022 }
2123
2224 builder := s .dag .Container ().
23- From ("golang:" + GO_VERSION ).
24- WithMountedCache ("/go/pkg/mod" , s .dag .CacheVolume ("go-mod-" + GO_VERSION )).
25+ From ("golang:" + s . goVersion ).
26+ WithMountedCache ("/go/pkg/mod" , s .dag .CacheVolume ("go-mod-" + s . goVersion )).
2527 WithEnvVariable ("GOMODCACHE" , "/go/pkg/mod" ).
26- WithMountedCache ("/go/build-cache" , s .dag .CacheVolume ("go-build-" + GO_VERSION )).
28+ WithMountedCache ("/go/build-cache" , s .dag .CacheVolume ("go-build-" + s . goVersion )).
2729 WithEnvVariable ("GOCACHE" , "/go/build-cache" ).
2830 WithMountedDirectory ("/src" , s .source ).
2931 WithWorkdir ("/src" ).
3032 WithEnvVariable ("GOOS" , os ).
31- WithEnvVariable ("GOARCH" , arch ).
32- WithExec ([]string {
33- "go" , "build" , "-ldflags" , "github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=" + s .appVersion ,
34- "-o" , "/bin/" + binName , "./cmd/harbor" ,
35- })
33+ WithEnvVariable ("GOARCH" , arch )
34+
35+ gitCommit , _ := builder .WithExec ([]string {"git" , "rev-parse" , "--short" , "HEAD" , "--always" }).Stdout (ctx )
36+ buildTime := time .Now ().UTC ().Format (time .RFC3339 )
37+
38+ ldflagsArgs := utils .LDFlags (ctx , s .appVersion , s .goVersion , buildTime , gitCommit )
39+
40+ builder = builder .WithExec ([]string {
41+ "bash" , "-c" ,
42+ fmt .Sprintf (`set -ex && go env && go build -v -ldflags "%s" -o /bin/%s /src/cmd/harbor/main.go` , ldflagsArgs , binName ),
43+ })
3644
3745 file := builder .File ("/bin/" + binName ) // Taking file from container
3846 dist = dist .WithFile (fmt .Sprintf ("%s/%s" , os , binName ), file ) // Adding file(bin) to dist directory
0 commit comments