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

Upload assets when we create the release #25

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
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
22 changes: 15 additions & 7 deletions releases/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,27 @@ func AddFilesToRelease(repo string, tag string, dir string) {
mgx.Must(err)

if !releaseExists(repo, tag) {
// Mark canary releases as a draft
// Mark canary releases as a pre-release
draft := ""
if strings.HasPrefix(tag, "canary") {
draft = "-p"
}

// Create the GH release
must.Command("gh", "release", "create", "-R", repo, tag, "--notes=", draft).CollapseArgs().RunV()
}
// Create the GH release and upload the assets at the same time
// The release stays in draft until all assets are uploaded
must.Command("gh", "release", "create", "-R", repo, tag, "--generate-notes", draft).
Args(files...).CollapseArgs().RunV()
} else {
// We must have failed when creating the release last time, and someone kicked the build to retry
// Get the release back into the desired state (see gh release create above for what we want to look like)

// Upload the release assets and overwrite existing assets
must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV()
// Upload the release assets and overwrite existing assets
must.Command("gh", "release", "upload", "--clobber", "-R", repo, tag).
Args(files...).RunV()

// The release may still be stuck in draft from a previous failed upload while creating the release, make sure draft is cleared
must.Command("gh", "release", "edit", "--draft=false", "-R", repo, tag).RunV()
}
}

func getReleaseAssets(dir string) ([]string, error) {
Expand Down
5 changes: 4 additions & 1 deletion tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
var (
must = shx.CommandBuilder{StopOnError: true}

// DefaultGitHubClientVersion is the version of gh that is installed when it's not present
DefaultGitHubClientVersion = "2.27.0"

// DefaultKindVersion is the default version of KinD that is installed when it's not present
DefaultKindVersion = "v0.12.0"

Expand Down Expand Up @@ -66,7 +69,7 @@ func EnsureGitHubClient() {
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/cli/cli/releases/download/v{{.VERSION}}/gh_{{.VERSION}}_{{.GOOS}}_{{.GOARCH}}{{.EXT}}",
Name: "gh",
Version: "1.8.1",
Version: DefaultGitHubClientVersion,
OsReplacement: map[string]string{
"darwin": "macOS",
},
Expand Down
25 changes: 24 additions & 1 deletion tools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestEnsureStaticCheck(t *testing.T) {
oldPath := os.Getenv("PATH")
defer os.Setenv("PATH", oldPath)
os.Setenv("PATH", tmp)

tools.EnsureStaticCheck()
xplat.PrependPath(gopath.GetGopathBin())

Expand All @@ -59,3 +59,26 @@ func TestEnsureStaticCheck(t *testing.T) {
require.NoError(t, err, "IsCommandAvailable failed")
assert.True(t, found, "staticcheck was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}

func TestEnsureGitHubClient(t *testing.T) {
tmp, err := os.MkdirTemp("", "magefiles")
require.NoError(t, err, "Error creating temp directory")
defer os.RemoveAll(tmp)

oldGoPath := os.Getenv("GOPATH")
defer os.Setenv("GOPATH", oldGoPath)
os.Setenv("GOPATH", tmp)

oldPath := os.Getenv("PATH")
defer os.Setenv("PATH", oldPath)
os.Setenv("PATH", tmp)

tools.EnsureGitHubClient()
xplat.PrependPath(gopath.GetGopathBin())

require.FileExists(t, filepath.Join(tmp, "bin", "gh"+xplat.FileExt()))

found, err := pkg.IsCommandAvailable("gh", "--version", tools.DefaultGitHubClientVersion)
require.NoError(t, err, "IsCommandAvailable failed")
assert.True(t, found, "gh was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}