Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
marccampbell committed Oct 17, 2024
1 parent 6e84467 commit 69e283c
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ docs:

.PHONE: release
release:
dagger call release --version $(version) --one-password-service-account env:$(OP_SERVICE_ACCOUNT)
dagger call release --one-password-service-account env:OP_SERVICE_ACCOUNT --version $(version)
108 changes: 95 additions & 13 deletions dagger/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"context"
"dagger/replicated/internal/dagger"
"encoding/json"
"fmt"
"net/http"

"github.com/Masterminds/semver"
)
Expand All @@ -24,12 +26,23 @@ func (r *Replicated) Release(
// +default=false
clean bool,

onePasswordSeriveAccount *dagger.Secret,
onePasswordServiceAccount *dagger.Secret,
) error {
// parse the requested version
v, err := semver.NewVersion(version)
gitTreeOK, err := checkGitTree(ctx, source)
if err != nil {
panic(err)
return err
}
if !gitTreeOK {
return fmt.Errorf("git tree is not clean")
}

if gitTreeOK {
return fmt.Errorf("git tree is clean")
}

major, minor, patch, err := parseVersion(ctx, version)
if err != nil {
return err
}

replicatedBinary := dag.Container().
Expand All @@ -50,27 +63,33 @@ func (r *Replicated) Release(
WithEntrypoint([]string{"/replicated"}).
WithFile("/replicated", replicatedBinary)

username, err := dag.Onepassword().FindSecret(onePasswordSeriveAccount, "Developer Automation", "Docker Hub Release Account", "username").Plaintext(ctx)
if err != nil {
panic(err)
}
password, err := dag.Onepassword().FindSecret(onePasswordSeriveAccount, "Developer Automation", "Docker Hub Release Account", "password").Plaintext(ctx)
username, err := dag.Onepassword().FindSecret(
onePasswordServiceAccount,
"Developer Automation",
"Docker Hub Release Account",
"username",
).Plaintext(ctx)
if err != nil {
panic(err)
}
password := dag.Onepassword().FindSecret(
onePasswordServiceAccount,
"Developer Automation",
"Docker Hub Release Account",
"password",
)

dockerContainer = dockerContainer.WithRegistryAuth("", username, password)
// push the container
if _, err := dockerContainer.Publish(ctx, "replicated/vendor-cli:latest"); err != nil {
panic(err)
}
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%d", v.Major())); err != nil {
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%d", major)); err != nil {
panic(err)
}
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%d.%d", v.Major(), v.Minor())); err != nil {
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%d.%d", major, minor)); err != nil {
panic(err)
}
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%s", version)); err != nil {
if _, err := dockerContainer.Publish(ctx, fmt.Sprintf("replicated/vendor-cli:%d.%d.%d", major, minor, patch)); err != nil {
panic(err)
}

Expand Down Expand Up @@ -102,3 +121,66 @@ func (r *Replicated) Release(

return nil
}

func parseVersion(ctx context.Context, version string) (int64, int64, int64, error) {
latestVersion, err := getLatestVersion(ctx)
if err != nil {
return 0, 0, 0, err
}
parsedLatestVersion, err := semver.NewVersion(latestVersion)
if err != nil {
return 0, 0, 0, err
}

switch version {
case "major":
return parsedLatestVersion.Major() + 1, 0, 0, nil
case "minor":
return parsedLatestVersion.Major(), parsedLatestVersion.Minor() + 1, 0, nil
case "patch":
return parsedLatestVersion.Major(), parsedLatestVersion.Minor(), parsedLatestVersion.Patch() + 1, nil
default:
v, err := semver.NewVersion(version)
if err != nil {
return 0, 0, 0, err
}
return v.Major(), v.Minor(), v.Patch(), nil
}
}

func getLatestVersion(ctx context.Context) (string, error) {
resp, err := http.DefaultClient.Get("https://api.github.com/repos/replicatedhq/replicated/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()

var release struct {
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", err
}

return release.TagName, nil
}

// checkGitTree will return true if the local git tree is clean
func checkGitTree(ctx context.Context, source *dagger.Directory) (bool, error) {
container := dag.Container().
From("alpine/git:latest").
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source).
WithWorkdir("/go/src/github.com/replicatedhq/replicated").
WithExec([]string{"git", "status", "--porcelain"})

output, err := container.Stdout(ctx)
if err != nil {
return false, err
}

if len(output) == 0 {
return true, nil
}

return false, nil
}

0 comments on commit 69e283c

Please sign in to comment.