Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

270 add ldflags to dagger #273

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 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,16 +44,25 @@ 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{"go", "build", "-o", "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 @@ -74,20 +84,31 @@ 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)).
WithEnvVariable("GOCACHE", "/go/build-cache").
WithMountedDirectory("/src", m.Source).
WithWorkdir("/src").
WithEnvVariable("GOOS", goos).
WithEnvVariable("GOARCH", goarch).
WithExec([]string{"go", "build", "-o", bin_path + "harbor", "/src/cmd/harbor/main.go"}).
WithEnvVariable("GOARCH", goarch)

version, _ := builder.WithExec([]string{"git", "describe", "--tags", "--abbrev=0"}).Stdout(ctx)
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=%s
-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
`, version, GO_VERSION, buildTime, gitCommit)

builder = builder.WithExec([]string{
"go", "build", "-ldflags", ldflagsArgs, "-o", "bin/harbor-cli", "cmd/harbor/main.go"}).
WithWorkdir(bin_path).
WithExec([]string{"ls"}).
WithEntrypoint([]string{"./harbor"})

builds = append(builds, builder)
}
}
Expand Down
Loading