-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup from first release with dagger
- Loading branch information
1 parent
24ddfa7
commit 9eb0cc1
Showing
6 changed files
with
138 additions
and
152 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ import ( | |
"context" | ||
"dagger/replicated/internal/dagger" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/Masterminds/semver" | ||
) | ||
|
@@ -27,32 +29,68 @@ func (r *Replicated) Release( | |
clean bool, | ||
|
||
onePasswordServiceAccountProduction *dagger.Secret, | ||
|
||
githubToken *dagger.Secret, | ||
) error { | ||
gitTreeOK, err := checkGitTree(ctx, source) | ||
gitTreeOK, err := checkGitTree(ctx, source, githubToken) | ||
if err != nil { | ||
return err | ||
} | ||
if !gitTreeOK { | ||
return fmt.Errorf("git tree is not clean") | ||
return fmt.Errorf("Your git tree is not clean. You cannot release what's not commited.") | ||
} | ||
|
||
major, minor, patch, err := parseVersion(ctx, version) | ||
latestVersion, err := getLatestVersion(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_ = dag.Container(). | ||
major, minor, patch, err := parseVersion(ctx, latestVersion, version) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Releasing as version %d.%d.%d\n", major, minor, patch) | ||
|
||
githubTokenPlaintext, err := githubToken.Plaintext(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
tagContainer := dag.Container(). | ||
From("alpine/git:latest"). | ||
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source). | ||
WithWorkdir("/go/src/github.com/replicatedhq/replicated"). | ||
WithExec([]string{"git", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)}). | ||
WithExec([]string{"git", "push", "origin", fmt.Sprintf("v%d.%d.%d", major, minor, patch)}) | ||
WithExec([]string{"git", "remote", "add", "tag", fmt.Sprintf("https://%[email protected]/replicatedhq/replicated.git", githubTokenPlaintext)}). | ||
With(CacheBustingExec([]string{"git", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)})). | ||
With(CacheBustingExec([]string{"git", "push", "tag", fmt.Sprintf("v%d.%d.%d", major, minor, patch)})) | ||
_, err = tagContainer.Stdout(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// copy the source that has the tag included in it | ||
updatedSource := tagContainer.Directory("/go/src/github.com/replicatedhq/replicated") | ||
|
||
// replace the version in the Makefile | ||
buildFileContent, err := updatedSource.File("./pkg/version/build.go").Contents(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
buildFileContent = strings.ReplaceAll(buildFileContent, "const version = \"unknown\"", fmt.Sprintf("const version = \"%s\"", version)) | ||
updatedSource = updatedSource.WithNewFile("./pkg/version/build.go", buildFileContent) | ||
|
||
goModCache := dag.CacheVolume("replicated-go-mod-122") | ||
goBuildCache := dag.CacheVolume("replicated-go-build-121") | ||
|
||
replicatedBinary := dag.Container(). | ||
From("golang:1.22"). | ||
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source). | ||
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", updatedSource). | ||
WithWorkdir("/go/src/github.com/replicatedhq/replicated"). | ||
WithExec([]string{"make", "build"}). | ||
WithMountedCache("/go/pkg/mod", goModCache). | ||
WithEnvVariable("GOMODCACHE", "/go/pkg/mod"). | ||
WithMountedCache("/go/build-cache", goBuildCache). | ||
WithEnvVariable("GOCACHE", "/go/build-cache"). | ||
With(CacheBustingExec([]string{"make", "build"})). | ||
File("/go/src/github.com/replicatedhq/replicated/bin/replicated") | ||
|
||
dockerContainer := dag.Container(). | ||
|
@@ -65,6 +103,10 @@ func (r *Replicated) Release( | |
WithWorkdir("/out"). | ||
WithEntrypoint([]string{"/replicated"}). | ||
WithFile("/replicated", replicatedBinary) | ||
_, err = dockerContainer.Stdout(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
username, err := dag.Onepassword().FindSecret( | ||
onePasswordServiceAccountProduction, | ||
|
@@ -96,12 +138,17 @@ func (r *Replicated) Release( | |
panic(err) | ||
} | ||
|
||
goreleaserContainer := dag.Goreleaser(dagger.GoreleaserOpts{ | ||
Version: goreleaserVersion, | ||
}).Ctr().WithSecretVariable("GITHUB_TOKEN", githubToken) | ||
|
||
if snapshot { | ||
_, err := dag. | ||
Goreleaser(dagger.GoreleaserOpts{ | ||
Version: goreleaserVersion, | ||
Ctr: goreleaserContainer, | ||
}). | ||
WithSource(source). | ||
WithSource(updatedSource). | ||
Snapshot(ctx, dagger.GoreleaserSnapshotOpts{ | ||
Clean: clean, | ||
}) | ||
|
@@ -112,8 +159,9 @@ func (r *Replicated) Release( | |
_, err := dag. | ||
Goreleaser(dagger.GoreleaserOpts{ | ||
Version: goreleaserVersion, | ||
Ctr: goreleaserContainer, | ||
}). | ||
WithSource(source). | ||
WithSource(updatedSource). | ||
Release(ctx, dagger.GoreleaserReleaseOpts{ | ||
Clean: clean, | ||
}) | ||
|
@@ -125,11 +173,7 @@ 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 | ||
} | ||
func parseVersion(ctx context.Context, latestVersion string, version string) (int64, int64, int64, error) { | ||
parsedLatestVersion, err := semver.NewVersion(latestVersion) | ||
if err != nil { | ||
return 0, 0, 0, err | ||
|
@@ -168,8 +212,14 @@ func getLatestVersion(ctx context.Context) (string, error) { | |
return release.TagName, nil | ||
} | ||
|
||
var ( | ||
ErrGitTreeNotClean = errors.New("Your git tree is not clean. You cannot release what's not commited.") | ||
ErrMainBranch = errors.New("You must be on the main branch to release") | ||
ErrCommitNotInGitHub = errors.New("You must merge your changes into the main branch before releasing") | ||
) | ||
|
||
// checkGitTree will return true if the local git tree is clean | ||
func checkGitTree(ctx context.Context, source *dagger.Directory) (bool, error) { | ||
func checkGitTree(ctx context.Context, source *dagger.Directory, githubToken *dagger.Secret) (bool, error) { | ||
container := dag.Container(). | ||
From("alpine/git:latest"). | ||
WithMountedDirectory("/go/src/github.com/replicatedhq/replicated", source). | ||
|
@@ -181,8 +231,67 @@ func checkGitTree(ctx context.Context, source *dagger.Directory) (bool, error) { | |
return false, err | ||
} | ||
|
||
if len(output) == 0 { | ||
return true, nil | ||
if len(output) > 0 { | ||
return false, ErrGitTreeNotClean | ||
} | ||
|
||
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", "branch"}) | ||
|
||
output, err = container.Stdout(ctx) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if !strings.Contains(output, "* main") { | ||
return false, ErrMainBranch | ||
} | ||
|
||
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", "rev-parse", "HEAD"}) | ||
|
||
commit, err := container.Stdout(ctx) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/replicatedhq/replicated/commits/%s", commit), nil) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
githubTokenPlaintext, err := githubToken.Plaintext(ctx) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
req.Header.Set("Authorization", fmt.Sprintf("token %s", githubTokenPlaintext)) | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return false, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
type GitHubResponse struct { | ||
SHA string `json:"sha"` | ||
NodeID string `json:"node_id"` | ||
Status string `json:"status"` | ||
} | ||
|
||
var ghResp GitHubResponse | ||
if err := json.NewDecoder(resp.Body).Decode(&ghResp); err != nil { | ||
return false, err | ||
} | ||
|
||
if ghResp.Status == "422" { | ||
return false, ErrCommitNotInGitHub | ||
} | ||
|
||
return false, nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
package version | ||
|
||
// NOTE: these variables are injected at build time | ||
|
||
var ( | ||
version, gitSHA, buildTime string | ||
) | ||
const version = "unknown" |
Oops, something went wrong.