Skip to content

Commit 769a15e

Browse files
authored
Create a CI script for automated releases (#281)
* Create a basic release script * Install python requirements * Bump version for testing * Run cargo publish without --dry-run * Push the git tag to the repo * Set up crates.io token for releasing * Fix tagging * Authenticate `gh` tool * Run script only on master branch and set version back to 0.14.3
1 parent cd9d748 commit 769a15e

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

.github/workflows/release.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
8+
jobs:
9+
release:
10+
name: "Release"
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 15
13+
environment: crates_io_release
14+
15+
steps:
16+
- name: "Checkout Repository"
17+
uses: actions/checkout@v1
18+
19+
- name: "Install Python Libraries"
20+
run: python -m pip install --user -r requirements.txt
21+
working-directory: "scripts"
22+
23+
- name: "Run release script"
24+
run: "python3 scripts/ci-release.py"
25+
env:
26+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

scripts/ci-release.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import toml
2+
import requests
3+
import subprocess
4+
5+
cargo_toml = toml.load("Cargo.toml")
6+
crate_version = cargo_toml["package"]["version"]
7+
print("Detected crate version " + crate_version)
8+
9+
api_url = "https://crates.io/api/v1/crates/x86_64/versions"
10+
crates_io_versions = requests.get(api_url).json()
11+
12+
new_version = True
13+
for version in crates_io_versions["versions"]:
14+
assert (version["crate"] == "x86_64")
15+
if version["num"] == crate_version:
16+
new_version = False
17+
break
18+
19+
if new_version:
20+
print("Could not find version " + crate_version + " on crates.io; creating a new release")
21+
22+
print(" Running `cargo publish`")
23+
subprocess.run(["cargo", "publish"], check=True)
24+
25+
tag_name = "v" + crate_version
26+
print(" Tagging commit as " + tag_name)
27+
sha = subprocess.run(["git", "rev-parse", "HEAD"], check=True, stdout=subprocess.PIPE).stdout.decode("utf-8").strip()
28+
subprocess.run([
29+
"gh", "api", "/repos/rust-osdev/x86_64/git/refs",
30+
"-X", "POST", "-H", "Accept: application/vnd.github.v3+json",
31+
"-F", "ref=refs/tags/" + tag_name,
32+
"-F", "sha="+sha
33+
])
34+
35+
print(" Done")
36+
else:
37+
print("Version " + crate_version + " already exists on crates.io")

scripts/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
toml

0 commit comments

Comments
 (0)