Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 69 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,37 @@ jobs:
RUSTDOCFLAGS: -D warnings

test:
name: Test (${{ matrix.toolchain }})
name: Test (${{ matrix.name }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
# `msrv` resolves to the workspace `rust-version`.
toolchain: [ stable, msrv ]
include:
# The whole workspace on stable. No broker here: the live Redis tests skip (the `redis` job
# runs them with a broker and BQ_REQUIRE_REDIS).
- name: stable
toolchain: stable
manifest: Cargo.toml
command: cargo test --workspace --all-features --locked
# Each crate on its own floor: `msrv` resolves to the `rust-version` of the manifest. The
# core stays at the workspace floor; `babelqueue-redis` sets a higher one (the `redis`
# crate's), so its leg builds it, tests included, without running them against a broker.
- name: msrv core
toolchain: msrv
manifest: Cargo.toml
command: cargo test -p babelqueue --locked
- name: msrv redis
toolchain: msrv
manifest: crates/babelqueue-redis/Cargo.toml
command: cargo test -p babelqueue-redis --all-features --locked --no-run
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Resolve toolchain
id: tc
run: |
if [ "${{ matrix.toolchain }}" = "msrv" ]; then
v="$(sed -n 's/^rust-version *= *"\(.*\)"/\1/p' Cargo.toml | head -n1)"
[ -n "$v" ] || { echo "rust-version not found in Cargo.toml" >&2; exit 1; }
v="$(sed -n 's/^rust-version *= *"\(.*\)"/\1/p' "${{ matrix.manifest }}" | head -n1)"
[ -n "$v" ] || { echo "rust-version not found in ${{ matrix.manifest }}" >&2; exit 1; }
else
v="${{ matrix.toolchain }}"
fi
Expand All @@ -62,8 +78,53 @@ jobs:
run: rustup toolchain install "${{ steps.tc.outputs.version }}" --profile minimal && rustup default "${{ steps.tc.outputs.version }}"
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0
with:
key: ${{ matrix.toolchain }}
- run: cargo test --workspace --all-features --locked
key: ${{ matrix.name }}
- run: ${{ matrix.command }}

redis:
name: Redis (live broker) and results.json
runs-on: ubuntu-latest
# The redis conformance sections and the live tests need a real broker: BQ_REQUIRE_REDIS makes a
# missing one fail instead of letting the cases skip (broker-bindings section 1, runner contract).
services:
redis:
image: redis:7
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 5s --health-timeout 3s --health-retries 5
env:
REDIS_URL: redis://localhost:6379/0
BQ_REQUIRE_REDIS: "1"
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
- name: Install stable toolchain
run: rustup toolchain install stable --profile minimal && rustup default stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0
# Every crate with a conformance runner writes the sections it ran to a file of its own
# (BQ_CONFORMANCE_RESULTS); merge-conformance-results.py joins them and derives `bindings`
# from the crates in the tree.
- name: Core conformance (partial results)
run: cargo test -p babelqueue --all-features --locked --test conformance
env:
BQ_CONFORMANCE_RESULTS: ${{ runner.temp }}/results/core.json
- name: Redis tests, live and conformance (partial results)
run: cargo test -p babelqueue-redis --all-features --locked
env:
BQ_CONFORMANCE_RESULTS: ${{ runner.temp }}/results/redis.json
- name: Merge results.json (ADR-0032)
run: |
python3 scripts/merge-conformance-results.py "$RUNNER_TEMP/results.json" \
"$RUNNER_TEMP/results/core.json" "$RUNNER_TEMP/results/redis.json"
# Schema-valid, no `fail`, bindings derived from the crates in the tree, and every in-scope
# section and case present: a transport crate without its sections breaks CI.
- name: Validate results.json
run: |
python3 -m venv "$RUNNER_TEMP/venv"
"$RUNNER_TEMP/venv/bin/pip" install --quiet jsonschema==4.23.0
"$RUNNER_TEMP/venv/bin/python" -I scripts/check-conformance-results.py \
"$RUNNER_TEMP/results.json" testdata/conformance "$BQ_CONFORMANCE_VERSION"

conformance:
name: Conformance suite in sync
Expand Down Expand Up @@ -91,7 +152,7 @@ jobs:
ci-green:
name: CI green
runs-on: ubuntu-latest
needs: [lint, test, conformance, deny]
needs: [lint, test, redis, conformance, deny]
if: ${{ always() }}
steps:
- name: Fail if any required job did not pass
Expand Down
246 changes: 246 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
name: Release

# A `v*` tag publishes `babelqueue` (the core); a `redis/v*` tag publishes `babelqueue-redis` (the
# same prefix scheme as babelqueue-go). `babelqueue-redis` depends on the core by version, so the
# core must be on crates.io first: the publish job of a `redis/v*` run waits (up to 15 minutes) for
# that core version before publishing, so pushing both tags together is safe, and pushing the core
# tag first is still the clean order.
#
# The first release of each crate is made by hand with a token (crates.io trusted publishing can
# only be configured for a crate that exists). The publish step below asks crates.io whether the tag's
# version is already there and, if so, skips `cargo publish` and still creates the GitHub release
# with the results.json asset, so the order "manual `cargo publish` -> trusted publisher
# registration -> tag push" works without a failed run.
#
# A manual run (workflow_dispatch) is a dry run: tests, conformance, results.json and
# `cargo publish --dry-run` run, nothing is published or released.
on:
push:
tags:
- "v*" # babelqueue (core)
- "redis/v*" # babelqueue-redis
workflow_dispatch:

concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false

permissions:
contents: read

env:
CARGO_TERM_COLOR: always
CARGO_INCREMENTAL: "0"
# The conformance suite release this repo is vendored from and must report against.
BQ_CONFORMANCE_VERSION: "1.11.0"
# sha256 of that release's fetch.sh: the script runs on the runner, so it is pinned rather than
# trusted from a mutable tag. Bump together with BQ_CONFORMANCE_VERSION (and ci.yml).
BQ_FETCH_SH_SHA256: 8bb6ba567b288fb0ac71a70fd17041b2503df119e01998d76fdad372570bf849

# Third-party actions are pinned to a commit SHA (supply chain, ADR-0041 D7).
# actions/checkout v7.0.1
# Swatinem/rust-cache v2.9.3
# actions/upload-artifact v4.6.2
# actions/download-artifact v4.3.0
# rust-lang/crates-io-auth-action v1.0.6
# softprops/action-gh-release v3.0.5
jobs:
verify:
name: Verify (tests, conformance, publish dry run)
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
crate: ${{ steps.target.outputs.crate }}
version: ${{ steps.target.outputs.version }}
# A Redis broker for the published results.json: the redis conformance sections and the live
# tests must run (BQ_REQUIRE_REDIS makes a missing broker fail), as in ci.yml.
services:
redis:
image: redis:7
ports:
- 6379:6379
options: >-
--health-cmd "redis-cli ping"
--health-interval 5s --health-timeout 3s --health-retries 5
env:
REDIS_URL: redis://localhost:6379/0
BQ_REQUIRE_REDIS: "1"
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- name: Install stable toolchain
run: rustup toolchain install stable --profile minimal && rustup default stable
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0

# The tag decides the crate; the manifest decides the version, and they must agree. A tag
# that matches no crate, or a version the manifest does not carry, stops the release here.
- name: Resolve crate and version from the tag
id: target
if: ${{ github.event_name == 'push' }}
env:
REF_NAME: ${{ github.ref_name }}
run: |
set -euo pipefail
case "$REF_NAME" in
redis/v*) crate=babelqueue-redis; version="${REF_NAME#redis/v}" ;;
v*) crate=babelqueue; version="${REF_NAME#v}" ;;
*) echo "release: tag '$REF_NAME' maps to no crate" >&2; exit 1 ;;
esac
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "release: '$version' is not a semantic version" >&2; exit 1
fi
manifest="$(cargo metadata --no-deps --format-version 1 --locked \
| jq -r --arg c "$crate" '.packages[] | select(.name == $c) | .version')"
if [ "$manifest" != "$version" ]; then
echo "release: tag version '$version' != $crate manifest version '$manifest'" >&2; exit 1
fi
# A dated heading: `## [0.1.0] - Unreleased` is a draft and must not be tagged.
if ! grep -Eq "^## \[${version//./\\.}\] - [0-9]{4}-[0-9]{2}-[0-9]{2}" CHANGELOG.md; then
echo "release: CHANGELOG.md has no dated '## [$version] - YYYY-MM-DD' section" >&2; exit 1
fi
echo "crate=$crate" >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"

# The vendored suite must be byte-identical to the pinned conformance release, or the
# results.json would attest a suite nobody can reproduce.
- name: Verify vendored conformance matches the pinned release
run: |
curl -fsSL -o "$RUNNER_TEMP/fetch.sh" \
"https://raw.githubusercontent.com/BabelQueue/conformance/v${BQ_CONFORMANCE_VERSION}/fetch.sh"
echo "${BQ_FETCH_SH_SHA256} $RUNNER_TEMP/fetch.sh" | sha256sum -c -
bash "$RUNNER_TEMP/fetch.sh" --check "$BQ_CONFORMANCE_VERSION" testdata/conformance

- name: Test the workspace
run: cargo test --workspace --all-features --locked

# Each crate with a conformance runner writes the sections it ran to a file of its own;
# merge-conformance-results.py joins them and derives `bindings` from the crates in the tree.
- name: Core conformance (partial results)
run: cargo test -p babelqueue --all-features --locked --test conformance
env:
BQ_CONFORMANCE_RESULTS: ${{ runner.temp }}/results/core.json
- name: Redis conformance (partial results)
run: cargo test -p babelqueue-redis --all-features --locked --test conformance
env:
BQ_CONFORMANCE_RESULTS: ${{ runner.temp }}/results/redis.json
- name: Merge results.json (ADR-0032)
run: |
python3 scripts/merge-conformance-results.py "$RUNNER_TEMP/results.json" \
"$RUNNER_TEMP/results/core.json" "$RUNNER_TEMP/results/redis.json"
# Schema-valid, no `fail`, bindings derived from the crates in the tree, and every in-scope
# section and case present: a crate without its sections breaks the release.
- name: Validate results.json
run: |
python3 -m venv "$RUNNER_TEMP/venv"
"$RUNNER_TEMP/venv/bin/pip" install --quiet jsonschema==4.23.0
"$RUNNER_TEMP/venv/bin/python" -I scripts/check-conformance-results.py \
"$RUNNER_TEMP/results.json" testdata/conformance "$BQ_CONFORMANCE_VERSION"
mkdir -p "$RUNNER_TEMP/release-assets"
cp "$RUNNER_TEMP/results.json" "$RUNNER_TEMP/release-assets/results.json"

# Both crates in one call: `babelqueue-redis` depends on the core by version, and the core is
# not on crates.io yet before its first release, so cargo verifies them together (the core is
# taken from the same run). A single `-p babelqueue-redis` dry run cannot resolve it then.
- name: Publish dry run (both crates)
run: cargo publish --dry-run -p babelqueue -p babelqueue-redis --locked

- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
with:
name: release-assets
path: ${{ runner.temp }}/release-assets/results.json
if-no-files-found: error

publish:
name: Publish to crates.io and create the GitHub release
needs: verify
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
# The environment name and this workflow file are what the crates.io trusted publisher is
# registered against; id-token is requested only here, never in the verify job.
environment: crates-io
permissions:
contents: write
id-token: write
env:
CRATE: ${{ needs.verify.outputs.crate }}
VERSION: ${{ needs.verify.outputs.version }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1
with:
persist-credentials: false
- name: Install stable toolchain
run: rustup toolchain install stable --profile minimal && rustup default stable
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093
with:
name: release-assets
path: release-assets

# The first-release mechanism: ask crates.io about exactly this crate and version. 200 means
# it was published by hand (or by an earlier run): skip the publish. 404 means publish. Any
# other answer is not a decision, so the run fails rather than guessing. A yanked version
# still answers 200: it cannot be published again.
- name: Is this version already on crates.io?
id: published
run: |
set -euo pipefail
code="$(curl -sS -o /dev/null -w '%{http_code}' \
-A "babelqueue-rust-release (https://github.com/BabelQueue/babelqueue-rust)" \
"https://crates.io/api/v1/crates/${CRATE}/${VERSION}")"
case "$code" in
200) echo "exists=true" >> "$GITHUB_OUTPUT"; echo "$CRATE $VERSION is already on crates.io: skipping cargo publish" ;;
404) echo "exists=false" >> "$GITHUB_OUTPUT" ;;
*) echo "crates.io answered HTTP $code for $CRATE $VERSION" >&2; exit 1 ;;
esac

# `babelqueue-redis` resolves `babelqueue` from crates.io at publish time, by the version
# requirement in its own manifest (not by the workspace version). Wait for the lowest version
# that requirement names: when both tags are pushed at once the core run may still be
# publishing. A redis-only release whose requirement an already-published core satisfies (say
# `0.1.0` while the workspace is 0.1.1) therefore needs no new core release. Bounded.
- name: Wait for the core version on crates.io (redis tag only)
if: ${{ steps.published.outputs.exists == 'false' && env.CRATE == 'babelqueue-redis' }}
run: |
set -euo pipefail
# cargo metadata normalizes `version = "0.1.0"` to the requirement "^0.1.0"; strip the
# operator and keep the version it names (^, ~, = and >= all name their lowest version).
req="$(cargo metadata --no-deps --format-version 1 --locked \
| jq -r '.packages[] | select(.name == "babelqueue-redis") | .dependencies[] | select(.name == "babelqueue") | .req')"
core="$(printf '%s' "$req" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?' | head -n1 || true)"
[ -n "$core" ] || { echo "release: no babelqueue version found in the requirement '$req' of babelqueue-redis" >&2; exit 1; }
echo "babelqueue-redis requires babelqueue $req; waiting for $core"
for attempt in $(seq 1 45); do
code="$(curl -sS -o /dev/null -w '%{http_code}' \
-A "babelqueue-rust-release (https://github.com/BabelQueue/babelqueue-rust)" \
"https://crates.io/api/v1/crates/babelqueue/${core}")"
if [ "$code" = "200" ]; then
echo "babelqueue $core is on crates.io"
sleep 15 # let the sparse index catch up with the API before cargo resolves it
exit 0
fi
echo "babelqueue $core not on crates.io yet (HTTP $code), attempt $attempt/45"
sleep 20
done
echo "release: babelqueue $core did not appear on crates.io within 15 minutes; publish the core first" >&2
exit 1

- name: Authenticate to crates.io (trusted publishing)
id: auth
if: ${{ steps.published.outputs.exists == 'false' }}
uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9

- name: cargo publish
if: ${{ steps.published.outputs.exists == 'false' }}
run: cargo publish -p "$CRATE" --locked
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}

- name: Create GitHub Release
uses: softprops/action-gh-release@88b783939ccdbabad21b9a37510aed7fad5f2e60
with:
tag_name: ${{ github.ref_name }}
generate_release_notes: true
# A version with a pre-release suffix (0.2.0-rc.1) must not become "Latest".
prerelease: ${{ contains(needs.verify.outputs.version, '-') }}
files: release-assets/results.json
Loading