name: Release
on:
push:
branches: [main]
workflow_dispatch:
inputs:
dry-run:
description: Check, test and `cargo publish --dry-run` only. Nothing is published, tagged or released.
type: boolean
default: true
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
jobs:
release:
name: Publish to crates.io
if: github.repository == 'wuapidev/wuapi-rust'
runs-on: ubuntu-latest
timeout-minutes: 20
permissions:
contents: write id-token: write env:
DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && inputs.dry-run }}
steps:
- name: Refuse to publish from a ref other than main
if: github.ref != 'refs/heads/main' && env.DRY_RUN != 'true'
run: |
echo "::error::Publishing runs from main only (this run is on ${GITHUB_REF}). Re-run on main, or tick dry-run."
exit 1
- uses: actions/checkout@v5
with:
fetch-depth: 0
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- name: Plan
id: plan
run: |
meta="$(cargo metadata --no-deps --format-version 1)"
name="$(jq -r '.packages[0].name' <<< "$meta")"
version="$(jq -r '.packages[0].version' <<< "$meta")"
# 200: crates.io has this version. 404: it does not. Anything else
# (rate limit, outage) stops the run instead of guessing.
status="$(curl -sS -o /dev/null -w '%{http_code}' -A "wuapi-rust release (github.com/${GITHUB_REPOSITORY})" "https://crates.io/api/v1/crates/${name}/${version}")"
case "$status" in
200) published=true ;;
404) published=false ;;
*) echo "::error::crates.io answered ${status} for ${name} ${version}."; exit 1 ;;
esac
case "$version" in *-*) prerelease=true ;; *) prerelease=false ;; esac
{
echo "name=${name}"
echo "version=${version}"
echo "tag=v${version}"
echo "published=${published}"
echo "prerelease=${prerelease}"
} | tee -a "$GITHUB_OUTPUT"
- name: Format, lint, test
run: cargo fmt --check && cargo clippy --all-targets -- -D warnings && cargo test
- name: Package (dry run)
if: steps.plan.outputs.published == 'false'
run: cargo publish --dry-run
- name: Authenticate to crates.io
id: auth
if: steps.plan.outputs.published == 'false' && env.DRY_RUN != 'true'
uses: rust-lang/crates-io-auth-action@v1
- name: Publish ${{ steps.plan.outputs.name }} ${{ steps.plan.outputs.version }}
if: steps.plan.outputs.published == 'false' && env.DRY_RUN != 'true'
env:
CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
run: cargo publish
- name: Already on crates.io
if: steps.plan.outputs.published == 'true'
run: echo "::notice::${{ steps.plan.outputs.name }} ${{ steps.plan.outputs.version }} is already on crates.io; nothing to publish."
- name: Tag and GitHub Release
if: env.DRY_RUN != 'true'
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ steps.plan.outputs.tag }}
PRERELEASE: ${{ steps.plan.outputs.prerelease }}
run: |
if gh release view "$TAG" > /dev/null 2>&1; then
echo "Release $TAG exists; nothing to do."
exit 0
fi
args=(--target "$GITHUB_SHA" --title "$TAG" --generate-notes)
previous="$(git tag --list 'v*' --sort=-v:refname | grep -vx "$TAG" | head -n 1 || true)"
if [ -n "$previous" ]; then args+=(--notes-start-tag "$previous"); fi
if [ "$PRERELEASE" = "true" ]; then args+=(--prerelease); fi
gh release create "$TAG" "${args[@]}"