|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "dagger/harbor-cli/internal/dagger" |
| 10 | +) |
| 11 | + |
| 12 | +func (m *HarborCli) PublishImageAndSign( |
| 13 | + ctx context.Context, |
| 14 | + source *dagger.Directory, |
| 15 | + registry string, |
| 16 | + registryUsername string, |
| 17 | + registryPassword *dagger.Secret, |
| 18 | + imageTags []string, |
| 19 | + // +optional |
| 20 | + githubToken *dagger.Secret, |
| 21 | + // +optional |
| 22 | + actionsIdTokenRequestToken *dagger.Secret, |
| 23 | + // +optional |
| 24 | + actionsIdTokenRequestUrl string, |
| 25 | +) (string, error) { |
| 26 | + m.init(ctx, source) |
| 27 | + |
| 28 | + imageAddrs := m.PublishImage(ctx, registry, registryUsername, imageTags, registryPassword) |
| 29 | + |
| 30 | + _, err := m.Sign( |
| 31 | + ctx, |
| 32 | + githubToken, |
| 33 | + actionsIdTokenRequestUrl, |
| 34 | + actionsIdTokenRequestToken, |
| 35 | + registryUsername, |
| 36 | + registryPassword, |
| 37 | + imageAddrs[0], |
| 38 | + ) |
| 39 | + if err != nil { |
| 40 | + return "", fmt.Errorf("failed to sign image: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + fmt.Printf("Signed image: %s\n", imageAddrs) |
| 44 | + return imageAddrs[0], nil |
| 45 | +} |
| 46 | + |
| 47 | +func (m *HarborCli) PublishImage( |
| 48 | + ctx context.Context, |
| 49 | + registry, registryUsername string, |
| 50 | + // +optional |
| 51 | + // +default=["latest"] |
| 52 | + imageTags []string, |
| 53 | + registryPassword *dagger.Secret, |
| 54 | +) []string { |
| 55 | + version := getVersion(imageTags) |
| 56 | + builders := m.build(ctx, version) |
| 57 | + releaseImages := []*dagger.Container{} |
| 58 | + |
| 59 | + for i, tag := range imageTags { |
| 60 | + imageTags[i] = strings.TrimSpace(tag) |
| 61 | + if strings.HasPrefix(imageTags[i], "v") { |
| 62 | + imageTags[i] = strings.TrimPrefix(imageTags[i], "v") |
| 63 | + } |
| 64 | + } |
| 65 | + fmt.Printf("provided tags: %s\n", imageTags) |
| 66 | + |
| 67 | + // Get current time for image creation timestamp |
| 68 | + creationTime := time.Now().UTC().Format(time.RFC3339) |
| 69 | + |
| 70 | + for _, builder := range builders { |
| 71 | + os, _ := builder.EnvVariable(ctx, "GOOS") |
| 72 | + arch, _ := builder.EnvVariable(ctx, "GOARCH") |
| 73 | + |
| 74 | + if os != "linux" { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + ctr := dag.Container(dagger.ContainerOpts{Platform: dagger.Platform(os + "/" + arch)}). |
| 79 | + From("alpine:latest"). |
| 80 | + WithWorkdir("/"). |
| 81 | + WithFile("/harbor", builder.File("./harbor")). |
| 82 | + WithExec([]string{"ls", "-al"}). |
| 83 | + WithExec([]string{"./harbor", "version"}). |
| 84 | + // Add required metadata labels for ArtifactHub |
| 85 | + WithLabel("org.opencontainers.image.created", creationTime). |
| 86 | + WithLabel("org.opencontainers.image.description", "Harbor CLI - A command-line interface for CNCF Harbor, the cloud native registry!"). |
| 87 | + WithLabel("io.artifacthub.package.readme-url", "https://raw.githubusercontent.com/goharbor/harbor-cli/main/README.md"). |
| 88 | + WithLabel("org.opencontainers.image.source", "https://github.com/goharbor/harbor-cli"). |
| 89 | + WithLabel("org.opencontainers.image.version", version). |
| 90 | + WithLabel("io.artifacthub.package.license", "Apache-2.0"). |
| 91 | + WithEntrypoint([]string{"/harbor"}) |
| 92 | + releaseImages = append(releaseImages, ctr) |
| 93 | + } |
| 94 | + |
| 95 | + imageAddrs := []string{} |
| 96 | + for _, imageTag := range imageTags { |
| 97 | + addr, err := dag.Container().WithRegistryAuth(registry, registryUsername, registryPassword). |
| 98 | + Publish(ctx, |
| 99 | + fmt.Sprintf("%s/%s/harbor-cli:%s", registry, registryUsername, imageTag), |
| 100 | + dagger.ContainerPublishOpts{PlatformVariants: releaseImages}, |
| 101 | + ) |
| 102 | + if err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | + fmt.Printf("Published image address: %s\n", addr) |
| 106 | + imageAddrs = append(imageAddrs, addr) |
| 107 | + } |
| 108 | + return imageAddrs |
| 109 | +} |
| 110 | + |
| 111 | +func (m *HarborCli) build( |
| 112 | + ctx context.Context, |
| 113 | + version string, |
| 114 | +) []*dagger.Container { |
| 115 | + var builds []*dagger.Container |
| 116 | + |
| 117 | + fmt.Println("🛠️ Building with Dagger...") |
| 118 | + oses := []string{"linux", "darwin", "windows"} |
| 119 | + arches := []string{"amd64", "arm64"} |
| 120 | + |
| 121 | + // temp container with git installed |
| 122 | + temp := dag.Container(). |
| 123 | + From("alpine:latest"). |
| 124 | + WithMountedDirectory("/src", m.Source). |
| 125 | + // --no-cache option is to avoid caching the apk package index |
| 126 | + WithExec([]string{"apk", "add", "--no-cache", "git"}). |
| 127 | + WithWorkdir("/src") |
| 128 | + |
| 129 | + gitCommit, _ := temp.WithExec([]string{"git", "rev-parse", "--short", "HEAD", "--always"}).Stdout(ctx) |
| 130 | + buildTime := time.Now().UTC().Format(time.RFC3339) |
| 131 | + ldflagsArgs := fmt.Sprintf(`-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=%s |
| 132 | + -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GoVersion=%s |
| 133 | + -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=%s |
| 134 | + -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=%s |
| 135 | + `, version, m.GoVersion, buildTime, gitCommit) |
| 136 | + |
| 137 | + for _, goos := range oses { |
| 138 | + for _, goarch := range arches { |
| 139 | + bin_path := fmt.Sprintf("build/%s/%s/", goos, goarch) |
| 140 | + builder := dag.Container(). |
| 141 | + From("golang:"+m.GoVersion+"-alpine"). |
| 142 | + WithMountedCache("/go/pkg/mod", dag.CacheVolume("go-mod-"+m.GoVersion)). |
| 143 | + WithEnvVariable("GOMODCACHE", "/go/pkg/mod"). |
| 144 | + WithMountedCache("/go/build-cache", dag.CacheVolume("go-build-"+m.GoVersion)). |
| 145 | + WithEnvVariable("GOCACHE", "/go/build-cache"). |
| 146 | + WithMountedDirectory("/src", m.Source). |
| 147 | + WithWorkdir("/src"). |
| 148 | + WithEnvVariable("GOOS", goos). |
| 149 | + WithEnvVariable("GOARCH", goarch). |
| 150 | + WithExec([]string{"go", "build", "-ldflags", ldflagsArgs, "-o", bin_path + "harbor", "/src/cmd/harbor/main.go"}). |
| 151 | + WithWorkdir(bin_path). |
| 152 | + WithExec([]string{"ls"}). |
| 153 | + WithEntrypoint([]string{"./harbor"}) |
| 154 | + |
| 155 | + builds = append(builds, builder) |
| 156 | + } |
| 157 | + } |
| 158 | + return builds |
| 159 | +} |
| 160 | + |
| 161 | +// Sign signs a container image using Cosign, works also with GitHub Actions |
| 162 | +func (m *HarborCli) Sign(ctx context.Context, |
| 163 | + // +optional |
| 164 | + githubToken *dagger.Secret, |
| 165 | + // +optional |
| 166 | + actionsIdTokenRequestUrl string, |
| 167 | + // +optional |
| 168 | + actionsIdTokenRequestToken *dagger.Secret, |
| 169 | + registryUsername string, |
| 170 | + registryPassword *dagger.Secret, |
| 171 | + imageAddr string, |
| 172 | +) (string, error) { |
| 173 | + registryPasswordPlain, _ := registryPassword.Plaintext(ctx) |
| 174 | + |
| 175 | + cosing_ctr := dag.Container().From("cgr.dev/chainguard/cosign") |
| 176 | + |
| 177 | + // If githubToken is provided, use it to sign the image |
| 178 | + if githubToken != nil { |
| 179 | + if actionsIdTokenRequestUrl == "" || actionsIdTokenRequestToken == nil { |
| 180 | + return "", fmt.Errorf("actionsIdTokenRequestUrl (exist=%s) and actionsIdTokenRequestToken (exist=%t) must be provided when githubToken is provided", actionsIdTokenRequestUrl, actionsIdTokenRequestToken != nil) |
| 181 | + } |
| 182 | + fmt.Printf("Setting the ENV Vars GITHUB_TOKEN, ACTIONS_ID_TOKEN_REQUEST_URL, ACTIONS_ID_TOKEN_REQUEST_TOKEN to sign with GitHub Token") |
| 183 | + cosing_ctr = cosing_ctr.WithSecretVariable("GITHUB_TOKEN", githubToken). |
| 184 | + WithEnvVariable("ACTIONS_ID_TOKEN_REQUEST_URL", actionsIdTokenRequestUrl). |
| 185 | + WithSecretVariable("ACTIONS_ID_TOKEN_REQUEST_TOKEN", actionsIdTokenRequestToken) |
| 186 | + } |
| 187 | + |
| 188 | + return cosing_ctr.WithSecretVariable("REGISTRY_PASSWORD", registryPassword). |
| 189 | + WithExec([]string{"cosign", "env"}). |
| 190 | + WithExec([]string{ |
| 191 | + "cosign", "sign", "--yes", "--recursive", |
| 192 | + "--registry-username", registryUsername, |
| 193 | + "--registry-password", registryPasswordPlain, |
| 194 | + imageAddr, |
| 195 | + "--timeout", "1m", |
| 196 | + }).Stdout(ctx) |
| 197 | +} |
| 198 | + |
| 199 | +func getVersion(tags []string) string { |
| 200 | + for _, tag := range tags { |
| 201 | + if strings.HasPrefix(tag, "v") { |
| 202 | + return tag |
| 203 | + } |
| 204 | + } |
| 205 | + return "latest" |
| 206 | +} |
0 commit comments