aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/upload_github_release_asset.py
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2020-04-27 21:30:54 -0400
committerJack O'Connor <[email protected]>2020-04-28 11:04:29 -0400
commitba468fbb4f7bb8b149eea60d0eab37858d32f23c (patch)
treea6c8acd87b78dc9846440126d4bc2fe1e1e32baf /.github/workflows/upload_github_release_asset.py
parentfcb4b83419ceb1eff8e7a059abe70b57a2f7f387 (diff)
build b3sum binaries in CI for new tags
These configs and code are adapted from the CI workflow in https://github.com/oconnor663/blake3-py, especially the upload_github_release_asset.py script, which is copied verbatim.
Diffstat (limited to '.github/workflows/upload_github_release_asset.py')
-rwxr-xr-x.github/workflows/upload_github_release_asset.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/.github/workflows/upload_github_release_asset.py b/.github/workflows/upload_github_release_asset.py
new file mode 100755
index 0000000..c1cbf51
--- /dev/null
+++ b/.github/workflows/upload_github_release_asset.py
@@ -0,0 +1,65 @@
+#! /usr/bin/env python3
+
+import github
+import os
+import sys
+
+RETRIES = 10
+
+g = github.Github(os.environ["GITHUB_TOKEN"])
+tag_name = os.environ["GITHUB_TAG"]
+tag_prefix = "refs/tags/"
+if tag_name.startswith(tag_prefix):
+ tag_name = tag_name[len(tag_prefix):]
+assert len(sys.argv) == 2
+asset_path = sys.argv[1]
+asset_name = os.path.basename(asset_path)
+
+repo = g.get_repo(os.environ["GITHUB_REPOSITORY"])
+
+tags = list(repo.get_tags())
+
+for tag in tags:
+ if tag.name == tag_name:
+ break
+else:
+ raise RuntimeError("no tag named " + repr(tag_name))
+
+try:
+ print("Creating GitHub release for tag " + repr(tag_name) + "...")
+ repo.create_git_release(tag_name, tag_name, tag.commit.commit.message)
+except github.GithubException as github_error:
+ if github_error.data["errors"][0]["code"] == "already_exists":
+ print("Release for tag " + repr(tag_name) + " already exists.")
+ else:
+ raise
+
+releases = list(repo.get_releases())
+for release in releases:
+ if release.tag_name == tag_name:
+ break
+else:
+ raise RuntimeError("no release for tag " + repr(tag_name))
+
+print("Uploading " + repr(asset_path) + "...")
+for i in range(RETRIES):
+ try:
+ print("Upload attempt #{} of {}...".format(i + 1, RETRIES))
+ release.upload_asset(asset_path)
+ break
+ except github.GithubException as github_error:
+ # Unfortunately the asset upload API is flaky. Even worse, it often
+ # partially succeeds, returning an error to the caller but leaving the
+ # release in a state where subsequent uploads of the same asset will
+ # fail with an "already_exists" error. (Though the asset is not visible
+ # on github.com, so we can't just declare victory and move on.) If we
+ # detect this case, explicitly delete the asset and continue retrying.
+ print(github_error)
+ for asset in release.get_assets():
+ if asset.name == asset_name:
+ print("Found uploaded asset after failure. Deleting...")
+ asset.delete_asset()
+else:
+ raise RuntimeError("All upload attempts failed.")
+
+print("Success!")