Skip to content

Create a CI script for automated releases #281

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

Merged
merged 9 commits into from
Jul 19, 2021
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
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
branches:
- 'master'

jobs:
release:
name: "Release"
runs-on: ubuntu-latest
timeout-minutes: 15
environment: crates_io_release

steps:
- name: "Checkout Repository"
uses: actions/checkout@v1

- name: "Install Python Libraries"
run: python -m pip install --user -r requirements.txt
working-directory: "scripts"

- name: "Run release script"
run: "python3 scripts/ci-release.py"
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
37 changes: 37 additions & 0 deletions scripts/ci-release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import toml
import requests
import subprocess

cargo_toml = toml.load("Cargo.toml")
crate_version = cargo_toml["package"]["version"]
print("Detected crate version " + crate_version)

api_url = "https://crates.io/api/v1/crates/x86_64/versions"
crates_io_versions = requests.get(api_url).json()
Comment on lines +9 to +10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be made (slightly) more efficient by querying something like https://crates.io/api/v1/crates/x86_64/0.14.3 directly, and then just checking if the returned JSON has a "version" field, or that version.num is the requested field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the existing solution is also fine, let's see if it works for 0.14.4

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! I didn't know about this API endpoint. Is there a complete reference for it somewhere?


new_version = True
for version in crates_io_versions["versions"]:
assert (version["crate"] == "x86_64")
if version["num"] == crate_version:
new_version = False
break

if new_version:
print("Could not find version " + crate_version + " on crates.io; creating a new release")

print(" Running `cargo publish`")
subprocess.run(["cargo", "publish"], check=True)

tag_name = "v" + crate_version
print(" Tagging commit as " + tag_name)
sha = subprocess.run(["git", "rev-parse", "HEAD"], check=True, stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
subprocess.run([
"gh", "api", "/repos/rust-osdev/x86_64/git/refs",
"-X", "POST", "-H", "Accept: application/vnd.github.v3+json",
"-F", "ref=refs/tags/" + tag_name,
"-F", "sha="+sha
])

print(" Done")
else:
print("Version " + crate_version + " already exists on crates.io")
1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toml