aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUebelAndre <github@uebelandre.com>2023-09-04 08:11:44 -0700
committerGitHub <noreply@github.com>2023-09-04 08:11:44 -0700
commitea4282ff93e24cf8f97e03f9a06150232863d937 (patch)
tree66bcad8204deecdad1fe9c27c04dcaadc941b8c0
parent0a1e587d96c612d93615743c12e9ea22863da574 (diff)
downloadbazelbuild-rules_rust-ea4282ff93e24cf8f97e03f9a06150232863d937.tar.gz
Added github action to publish cargo-bazel binaries. (#2134)
closes https://github.com/bazelbuild/rules_rust/issues/2131
-rw-r--r--.github/workflows/cargo_bazel_release.yaml47
-rw-r--r--crate_universe/BUILD.bazel14
-rw-r--r--crate_universe/tests/version_test.rs44
-rw-r--r--crate_universe/version.bzl2
4 files changed, 106 insertions, 1 deletions
diff --git a/.github/workflows/cargo_bazel_release.yaml b/.github/workflows/cargo_bazel_release.yaml
new file mode 100644
index 00000000..113b97ba
--- /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 d551d85f..a8f9abb3 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 00000000..7aa47b36
--- /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 9a896c30..f3984159 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"