diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..17583b0e1 --- /dev/null +++ b/.github/workflows/release.yml @@ -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 }} diff --git a/scripts/ci-release.py b/scripts/ci-release.py new file mode 100644 index 000000000..2386fe85c --- /dev/null +++ b/scripts/ci-release.py @@ -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() + +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") diff --git a/scripts/requirements.txt b/scripts/requirements.txt new file mode 100644 index 000000000..bd79a658f --- /dev/null +++ b/scripts/requirements.txt @@ -0,0 +1 @@ +toml