Skip to content

Commit

Permalink
imporvment
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Eschenbach <[email protected]>
  • Loading branch information
Vad1mo authored and qcserestipy committed Dec 22, 2024
1 parent 2859283 commit 848ad7c
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -43,27 +44,24 @@ func (m *HarborCli) BuildDev(
log.Fatalf("Error parsing platform: %v", err)
}
builder := dag.Container().
From("golang:"+GO_VERSION+"-alpine").
From("golang:"+GO_VERSION).
WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-"+GO_VERSION)).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+GO_VERSION)).
WithEnvVariable("GOCACHE", "/go/build-cache").
WithMountedDirectory("/src", m.Source). // Ensure the source directory with go.mod is mounted
WithWorkdir("/src").
WithEnvVariable("GOOS", os).
WithEnvVariable("GOARCH", arch).
WithExec([]string{"apk", "add", "--no-cache", "git"}).
WithExec([]string{
"sh",
"-c",
fmt.Sprintf(`
go build -ldflags "-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=dev \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GoVersion=$(go version | awk '{print $3}') \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=$(git rev-parse --short HEAD) \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=$(date -u +"%%Y-%%m-%%dT%%H:%%M:%%SZ")" \
-o %s %s
`, "bin/harbor-cli", "cmd/harbor/main.go"),
})
WithEnvVariable("GOARCH", arch)

gitCommit, _ := builder.WithExec([]string{"git", "rev-parse", "--short", "HEAD"}).Stdout(ctx)
buildTime := time.Now().UTC().Format(time.RFC3339)
ldflagsArgs := fmt.Sprintf(`-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=dev
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GoVersion=%s
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=%s
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=%s
`, GO_VERSION, buildTime, gitCommit)
builder = builder.WithExec([]string{"go", "build", "-ldflags", ldflagsArgs, "-o", "bin/harbor-cli", "cmd/harbor/main.go"})
return builder.File("bin/harbor-cli")
}

Expand All @@ -85,7 +83,7 @@ func (m *HarborCli) build(
for _, goarch := range arches {
bin_path := fmt.Sprintf("build/%s/%s/", goos, goarch)
builder := dag.Container().
From("golang:"+GO_VERSION+"-alpine").
From("golang:"+GO_VERSION).
WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-"+GO_VERSION)).
WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+GO_VERSION)).
Expand All @@ -94,7 +92,8 @@ func (m *HarborCli) build(
WithWorkdir("/src").
WithEnvVariable("GOOS", goos).
WithEnvVariable("GOARCH", goarch).
WithExec([]string{"apk", "add", "--no-cache", "git"}).
WithExec([]string{"git", "describe", "--tags", "--abbrev=0", ">>", "git-version.txt"}).
Terminal().
WithExec([]string{
"sh",
"-c",
Expand Down

0 comments on commit 848ad7c

Please sign in to comment.