Skip to content

Commit

Permalink
Create CI script to trigger a release on new version in Cargo.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-opp committed Dec 1, 2022
1 parent 7fca949 commit 724598f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/trigger-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Build

on:
push:
branches:
- "main"

jobs:
check:
name: Trigger Release
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v3

- name: "Install Python Libraries"
run: python -m pip install --user -r .github/workflows/trigger-release/requirements.txt

- name: "Run release script"
run: "python3 .github/workflows/trigger-release/trigger-release.py"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/trigger-release/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
toml
42 changes: 42 additions & 0 deletions .github/workflows/trigger-release/trigger-release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import toml
import requests
import subprocess

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

api_url = "https://crates.io/api/v1/crates/bootloader/" + crate_version
released_version = requests.get(api_url).json()

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

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

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
])

subprocess.run([
"gh", "api", "--method", "POST", "-H", "Accept: application/vnd.github+json",
"/repos/rust-osdev/bootloader/releases",
"-f", f"tag_name='{tag_name}'", "-f", f"target_commitish='{sha}'",
"-f", f"name='{tag_name}'",
"-f", "body='[Changelog](https://github.com/rust-osdev/bootloader/blob/main/Changelog.md)'",
"-F", "draft=false", "-F", "prerelease=false", "-F", "generate_release_notes=false",
])

print(" Done")

0 comments on commit 724598f

Please sign in to comment.