Skip to content

Commit

Permalink
simplified some flows
Browse files Browse the repository at this point in the history
TODO, change PublishImage to support multi arch images
  • Loading branch information
Vad1mo committed Sep 24, 2024
1 parent 06807d7 commit 62d7055
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 333 deletions.
177 changes: 177 additions & 0 deletions comments.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
Subject: [PATCH] comments
---
Index: main.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/main.go b/main.go
--- a/main.go (revision 96fa3fa98fb6ceb760e89eb10f1704e753b888ed)
+++ b/main.go (date 1724857880146)
@@ -3,17 +3,16 @@
import (
"context"
"fmt"
- "log"
- "strings"
"github.com/goharbor/harbor-cli/internal/dagger"
+ "log"
)

const (
- GO_VERSION = "1.22.5"
- SYFT_VERSION = "v1.9.0"
+ GO_VERSION = "1.22.5"
+ SYFT_VERSION = "v1.9.0"
GORELEASER_VERSION = "v2.1.0"
- APP_NAME = "dagger-harbor-cli"
- PUBLISH_ADDRESS = "demo.goharbor.io/library/harbor-cli:0.0.3"
+ APP_NAME = "dagger-harbor-cli"
+ PUBLISH_ADDRESS = "demo.goharbor.io/library/harbor-cli:0.0.3"
)

type HarborCli struct{}
@@ -46,25 +45,31 @@
WithMountedDirectory("/src", directoryArg).
WithWorkdir("/src").
WithExec([]string{"golangci-lint", "run", "--timeout", "5m"})
-
}

-func (m *HarborCli) BuildHarbor(ctx context.Context, directoryArg *dagger.Directory) *dagger.Directory {
+// Builds the Harbor CLI for multiple OS and architectures
+func (m *HarborCli) Build(ctx context.Context, source *dagger.Directory) *dagger.Directory {
+
fmt.Println("🛠️ Building with Dagger...")
oses := []string{"linux", "darwin", "windows"}
arches := []string{"amd64", "arm64"}
outputs := dag.Directory()
- golangcont, main_go_path := fetchMainGoPath(ctx, directoryArg)
-
for _, goos := range oses {
for _, goarch := range arches {
- path := fmt.Sprintf("build/%s/%s/", goos, goarch)
- build := golangcont.WithEnvVariable("GOOS", goos).
+ bin_path := fmt.Sprintf("build/%s/%s/", goos, goarch)
+ builder := dag.Container().
+ From("golang:1.22-alpine").
+ WithMountedDirectory("/src", source).
+ WithWorkdir("/src").
+ WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-122-5")).
+ WithEnvVariable("GOMODCACHE", "/go/pkg/mod").
+ WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-122-5")).
+ WithEnvVariable("GOCACHE", "/go/build-cache").
+ WithEnvVariable("GOOS", goos).
WithEnvVariable("GOARCH", goarch).
- WithExec([]string{"go", "build", "-o", path + "harbor", main_go_path})
-
+ WithExec([]string{"go", "build", "-o", bin_path + "harbor", "/src/cmd/harbor/main.go"})
// Get reference to build output directory in container
- outputs = outputs.WithDirectory(path, build.Directory(path))
+ outputs = outputs.WithDirectory(bin_path, builder.Directory(bin_path))
}
}
return outputs
@@ -90,44 +95,26 @@
log.Println("Release tasks completed successfully 🎉")
}

-func (m *HarborCli) DockerPublish(ctx context.Context, directoryArg *dagger.Directory, regUsername string, regPassword *dagger.Secret, privateKey *dagger.Secret, cosignPassword *dagger.Secret) string {
+func (m *HarborCli) DockerPublish(ctx context.Context, source *dagger.Directory) string {

-builder, main_go_path := fetchMainGoPath(ctx, directoryArg)
-builder = builder.WithWorkdir("/src").WithExec([]string{"go", "build", "-o", "harbor", main_go_path})
+ builder := m.Build(ctx, source)

-// Create a minimal runtime container
-runtime := dag.Container().
-From("alpine:latest").
-WithWorkdir("/root/").
-WithFile("/root/harbor", builder.File("/src/harbor")).
-WithEntrypoint([]string{"./harbor"})
+ // Create a minimal runtime container
+ runtime := dag.Container().
+ From("alpine:latest").
+ WithWorkdir("/root/").
+ WithFile("/root/harbor", builder.File("/src/harbor")).
+ WithEntrypoint([]string{"./harbor"})

-addr, _ := runtime.Publish(ctx,PUBLISH_ADDRESS)
-_ , err := dag.Cosign().Sign(ctx,privateKey,cosignPassword,[]string{addr},dagger.CosignSignOpts{RegistryUsername: regUsername, RegistryPassword: regPassword})
-if err != nil {
- panic(err)
-}
-fmt.Printf("Published to %s 🎉\n", addr)
-return addr
+ addr, _ := runtime.Publish(ctx, PUBLISH_ADDRESS)
+ //_, err := dag.Cosign().Sign(ctx, privateKey, cosignPassword, []string{addr}, dagger.CosignSignOpts{RegistryUsername: regUsername, RegistryPassword: regPassword})
+ //if err != nil {
+ // panic(err)
+ //}
+ fmt.Printf("Published to %s 🎉\n", addr)
+ return addr
}

-func fetchMainGoPath(ctx context.Context, directoryArg *dagger.Directory) (*dagger.Container, string) {
-
- container := dag.Container().
- From("golang:1.22-alpine").
- WithMountedDirectory("/src", directoryArg).
- WithWorkdir("/src").
- WithExec([]string{"sh", "-c", "export MAIN_GO_PATH=$(find ./cmd -type f -name 'main.go' -print -quit) && echo $MAIN_GO_PATH > main_go_path.txt"})
-
- // Reading the content of main_go_path.txt file and fetching the actual path of main.go
- main_go_txt_file, _ := container.File("main_go_path.txt").Contents(ctx)
- trimmedPath := strings.TrimPrefix(main_go_txt_file, "./")
- result := "/src/" + trimmedPath
- main_go_path := strings.TrimRight(result, "\n")
-
- return container, main_go_path
-}
-
func goreleaserContainer(directoryArg *dagger.Directory, githubToken string) *dagger.Container {
token := dag.SetSecret("github_token", githubToken)

Index: dagger.json
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/dagger.json b/dagger.json
--- a/dagger.json (revision 96fa3fa98fb6ceb760e89eb10f1704e753b888ed)
+++ b/dagger.json (date 1724762958963)
@@ -8,5 +8,5 @@
}
],
"source": ".",
- "engineVersion": "v0.12.3"
+ "engineVersion": "v0.12.5"
}
Index: README.md
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/README.md b/README.md
--- a/README.md (revision 96fa3fa98fb6ceb760e89eb10f1704e753b888ed)
+++ b/README.md (date 1724851803391)
@@ -102,11 +102,16 @@

## Build From Source

+For building the Harbor CLI locally, we recommend using [Dagger](https://docs.dagger.io/).
+Make sure you have also a container runtime like Docker or Podman installed on your system.
+
```bash
git clone https://github.com/goharbor/harbor-cli.git
-cd harbor-cli/cmd/harbor
-go build .
-sudo mv harbor /usr/local/bin/
+dagger functions # to see all existing operations without looking into code
+dagger call build --source=. # to create a harbor cli binary
+ls -la build/ # to see the binary for your platform
+
+
```

## Linux and MacOS
Loading

0 comments on commit 62d7055

Please sign in to comment.