name: Release to crates.io
'on':
pull_request:
branches: [master]
types: [closed]
workflow_dispatch:
inputs:
version:
description: 'Version to release, e.g. 0.3.0'
required: true
type: string
permissions:
contents: write
jobs:
release:
name: Release
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || (github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release'))
env:
RELEASE_VERSION: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.version || github.head_ref }}
GH_TOKEN: ${{ github.token }}
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: master
fetch-depth: 0
- name: Setup Rust
run: |
rustup update stable --no-self-update
rustup default stable
- name: Install cargo-release
run: |
curl -LsSf https://github.com/crate-ci/cargo-release/releases/download/v0.25.10/cargo-release-v0.25.10-x86_64-unknown-linux-gnu.tar.gz | tar xzf - -C ${CARGO_HOME:-~/.cargo}/bin
- name: Setup Git
run: |
git config --global user.name "${{ github.actor }}"
git config user.email "${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com"
git fetch --tags
- name: Release to crates.io
run: |
VERSION="${RELEASE_VERSION#release/}"
CURRENT_VERSION="$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].version')"
test "$CURRENT_VERSION" = "$VERSION"
cargo login "$CARGO_REGISTRY_TOKEN"
cargo release --no-confirm --execute
- name: Create a GitHub release
run: |
VERSION="${RELEASE_VERSION#release/}"
TAG="v${VERSION#v}"
gh release create "$TAG" --generate-notes --verify-tag || gh release view "$TAG"