Skip to content
Merged
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
40 changes: 34 additions & 6 deletions .github/workflows/trigger-release/trigger-release.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import toml
import requests
import subprocess
Expand All @@ -6,16 +7,23 @@
crate_version = cargo_toml["workspace"]["package"]["version"]
print("Detected crate version " + crate_version)

# crates.io enforces a data-access policy that requires a descriptive
# User-Agent with contact info; requests without one get rejected, which
# previously made this script misclassify already-published versions as
# unreleased. See https://crates.io/data-access.
headers = {
"User-Agent": "bootloader-release-ci (https://github.com/rust-osdev/bootloader)"
}
api_url = "https://crates.io/api/v1/crates/bootloader/" + crate_version
released_version = requests.get(api_url).json()
response = requests.get(api_url, headers=headers)

if "version" in released_version:
version = released_version["version"]
if response.status_code == 200 and "version" in response.json():
version = response.json()["version"]
assert (version["crate"] == "bootloader")
assert (version["num"] == crate_version)
print("Version " + crate_version + " already exists on crates.io")

else:
elif response.status_code == 404:
print("Could not find version " + crate_version +
" on crates.io; creating a new release")

Expand All @@ -33,6 +41,26 @@
"-F", "draft=false", "-F", "prerelease=false", "-F", "generate_release_notes=false",
]
print(" Running `" + ' '.join(command) + '`')
subprocess.run(command, check=True)
result = subprocess.run(command, capture_output=True, text=True)
sys.stdout.write(result.stdout)
sys.stderr.write(result.stderr)
if result.returncode != 0:
# A release for this tag may already exist (e.g. the version was
# published on crates.io but the crates.io check briefly failed, or
# this workflow is being re-run). Treat that as a no-op instead of a
# hard failure; fail loudly on any other error.
if "already_exists" in result.stdout or "already_exists" in result.stderr:
print(f" Release {tag_name} already exists; nothing to do")
else:
raise SystemExit(result.returncode)
else:
print(" Done")

print(" Done")
else:
# Any other response (rate limiting, data-access policy rejection, 5xx,
# etc.) is ambiguous: we can't tell whether the version is published.
# Fail loudly rather than guessing and attempting to re-create a release.
raise SystemExit(
f"Unexpected response from crates.io (HTTP {response.status_code}): "
f"{response.text}"
)
Loading