Skip to content

Commit

Permalink
Upload assets when we create the release (#25)
Browse files Browse the repository at this point in the history
* Upload assets when we create the release

When we create the github release, upload the assets at the same time, so that github is responsible for apply the latest flag to the release and only applies latest when all assets are uploaded. Right now there is a delay between when the release is created and when the assets are available, which causes the "install latest" script to fail for a short period of time after a release is created. I looked into managing the latest flag ourselves, so that we can flip it after we upload the assets but gh is doing a lot of logic around if it's the most recent and highest semver tag, and I'd like to not replicate that logic.

In the past I have seen create release fail when uploading assets during release creation, which is why it was originally split into two steps. Now that the code is checking for the existence of the release and re-uploading assets, that shouldn't be a problem. If a release fails while uploading, it won't have latest applied yet to it and we can kick the build to retry the upload to the release. I am also ensuring that the release isn't still marked as a draft when we retry uploading the assets. If asset upload fails during release creation, the release is left in draft and the draft flag needs to be removed once we finish uploading the assets during retry.

I have also turned on automatic release note creation so that we don't need to do that manually after the release is finished.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Remove unused parameter

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

---------

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs authored Apr 13, 2023
1 parent 9593117 commit 4f470a5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
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"))
}

0 comments on commit 4f470a5

Please sign in to comment.