Skip to content

Commit

Permalink
Added github action to publish cargo-bazel binaries. (#2134)
Browse files Browse the repository at this point in the history
closes #2131
  • Loading branch information
UebelAndre authored Sep 4, 2023
1 parent 0a1e587 commit ea4282f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
47 changes: 47 additions & 0 deletions .github/workflows/cargo_bazel_release.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
14 changes: 14 additions & 0 deletions crate_universe/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 44 additions & 0 deletions crate_universe/tests/version_test.rs
Original file line number Diff line number Diff line change
@@ -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.");
}
2 changes: 1 addition & 1 deletion crate_universe/version.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" Version info for the `cargo-bazel` repository """

VERSION = "0.8.0"
VERSION = "0.9.0"

0 comments on commit ea4282f

Please sign in to comment.