From ea4282ff93e24cf8f97e03f9a06150232863d937 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Mon, 4 Sep 2023 08:11:44 -0700 Subject: [PATCH] Added github action to publish cargo-bazel binaries. (#2134) closes https://github.com/bazelbuild/rules_rust/issues/2131 --- .github/workflows/cargo_bazel_release.yaml | 47 ++++++++++++++++++++++ crate_universe/BUILD.bazel | 14 +++++++ crate_universe/tests/version_test.rs | 44 ++++++++++++++++++++ crate_universe/version.bzl | 2 +- 4 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/cargo_bazel_release.yaml create mode 100644 crate_universe/tests/version_test.rs diff --git a/.github/workflows/cargo_bazel_release.yaml b/.github/workflows/cargo_bazel_release.yaml new file mode 100644 index 0000000000..113b97ba63 --- /dev/null +++ b/.github/workflows/cargo_bazel_release.yaml @@ -0,0 +1,47 @@ +--- +name: Release Cargo-bazel +on: + workflow_dispatch: + push: + branches: + - main + paths: + - crate_universe/version.bzl + +defaults: + run: + shell: bash + +jobs: + validation: + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + # TODO: Unfortunately it's not obvious how to restrict `workflow_dispatch` to a particular branch + # so this step ensures releases are always done off of `main`. + - name: Ensure branch is 'main' + run: | + git fetch origin &> /dev/null + branch="$(git rev-parse --abbrev-ref HEAD)" + if [[ "${branch}" != "main" ]]; then + echo "The release branch must be main. Got '${branch}'' instead." >&2 + exit 1 + else + echo "Branch is '${branch}'" + fi + builds: + needs: validation + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v3 + - name: Install rust toolchains for host + run: | + # Detect the current version of rust + version="$(grep 'DEFAULT_RUST_VERSION =' ./rust/private/common.bzl | grep -o '[[:digit:].]\+')" + rustup override set "${version}" + rustup update stable && rustup default stable + - name: Publish to crates.io + run: cargo publish --token ${CRATES_TOKEN} + working-directory: ./crate_universe + env: + CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }} diff --git a/crate_universe/BUILD.bazel b/crate_universe/BUILD.bazel index d551d85fca..a8f9abb356 100644 --- a/crate_universe/BUILD.bazel +++ b/crate_universe/BUILD.bazel @@ -131,6 +131,20 @@ rust_test( ), ) +rust_test( + name = "versions_test", + srcs = ["tests/version_test.rs"], + data = [ + "Cargo.toml", + "version.bzl", + ], + edition = "2021", + rustc_env = { + "CARGO_TOML": "$(rootpath :Cargo.toml)", + "VERSION_BZL": "$(rootpath :version.bzl)", + }, +) + rust_doc( name = "rustdoc", crate = ":cargo_bazel", diff --git a/crate_universe/tests/version_test.rs b/crate_universe/tests/version_test.rs new file mode 100644 index 0000000000..7aa47b36a9 --- /dev/null +++ b/crate_universe/tests/version_test.rs @@ -0,0 +1,44 @@ +//! A small test binary for ensuring the version of the rules matches the binary version + +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::path::PathBuf; + +#[test] +fn test_cargo_and_bazel_versions() { + // Parse the version field from the `cargo-bazel` Cargo.toml file + let cargo_version = { + let cargo_path = PathBuf::from(env!("CARGO_TOML")); + let file = File::open(cargo_path).expect("Failed to load Cargo.toml file"); + BufReader::new(file) + .lines() + .flatten() + .find(|line| line.contains("version = ")) + .map(|line| { + line.trim() + .replace("version = ", "") + .trim_matches('\"') + .to_owned() + }) + .expect("The version.bzl file should have a line with `version = `") + }; + + // Parse the version global from the Bazel module + let bazel_version = { + let bazel_path = PathBuf::from(env!("VERSION_BZL")); + let file = File::open(bazel_path).expect("Failed to load versions.bzl file"); + BufReader::new(file) + .lines() + .flatten() + .find(|line| line.contains("VERSION = ")) + .map(|line| { + line.trim() + .replace("VERSION = ", "") + .trim_matches('\"') + .to_owned() + }) + .expect("The version.bzl file should have a line with `VERSION = `") + }; + + assert_eq!(cargo_version, bazel_version, "make sure `//crate_universe:version.bzl` and `//crate_universe:Cargo.toml` have matching versions."); +} diff --git a/crate_universe/version.bzl b/crate_universe/version.bzl index 9a896c3078..f3984159bf 100644 --- a/crate_universe/version.bzl +++ b/crate_universe/version.bzl @@ -1,3 +1,3 @@ """ Version info for the `cargo-bazel` repository """ -VERSION = "0.8.0" +VERSION = "0.9.0"