name: Release
on:
workflow_dispatch:
inputs:
dry_run:
description: "Rehearse only - build + verify, no publish, no tag, no GitHub Release"
type: boolean
default: false
permissions:
contents: read
concurrency:
group: release
cancel-in-progress: false
env:
CARGO_TERM_COLOR: always
jobs:
preflight:
name: Preflight
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
tag: ${{ steps.version.outputs.tag }}
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Test
run: cargo test --locked
- name: Verify the crate publishes
run: cargo publish --dry-run --locked
- name: Read version from Cargo.toml + guard against re-publish
id: version
run: |
set -euo pipefail
VERSION="$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')"
echo "Cargo.toml version: $VERSION"
# Fail early if this version is already on crates.io — the "I forgot to
# bump Cargo.toml" guard, before anything irreversible runs.
code=$(curl -sS -o /dev/null -w '%{http_code}' \
-A "spherenet-admin-release (github-actions)" \
"https://crates.io/api/v1/crates/spherenet-admin/${VERSION}")
if [ "$code" = "200" ]; then
echo "::error::spherenet-admin ${VERSION} is already published on crates.io — bump the version in Cargo.toml first."
exit 1
fi
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
echo "tag=v${VERSION}" >> "$GITHUB_OUTPUT"
build:
name: Build ${{ matrix.target }}
needs: preflight
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-gnu
os: ubuntu-latest
- target: aarch64-apple-darwin
os: macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
with:
key: ${{ matrix.target }}
- name: Build
run: cargo build --release --locked --target ${{ matrix.target }}
- name: Package
shell: bash
run: |
set -euo pipefail
bin="spherenet-admin"
dist="${bin}-${{ needs.preflight.outputs.version }}-${{ matrix.target }}"
mkdir -p "staging/${dist}"
cp "target/${{ matrix.target }}/release/${bin}" "staging/${dist}/"
cp README.md LICENSE "staging/${dist}/"
mkdir -p dist
tar -czf "dist/${dist}.tar.gz" -C staging "${dist}"
( cd dist && shasum -a 256 "${dist}.tar.gz" > "${dist}.tar.gz.sha256" )
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.target }}
path: |
dist/*.tar.gz
dist/*.sha256
if-no-files-found: error
publish:
name: Publish to crates.io
needs: [preflight, build]
if: ${{ ! inputs.dry_run }}
runs-on: ubuntu-latest
environment: release
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- name: Publish
run: cargo publish --locked
github-release:
name: GitHub Release
needs: [preflight, build, publish]
if: ${{ ! inputs.dry_run }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: Publish GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.preflight.outputs.tag }}
name: ${{ needs.preflight.outputs.tag }}
target_commitish: ${{ github.sha }}
generate_release_notes: true
fail_on_unmatched_files: true
files: |
dist/*.tar.gz
dist/*.sha256