name: Release
on:
push:
tags: ['v*']
workflow_dispatch:
inputs:
tag:
description: Existing published tag to complete its GitHub Release without republishing
required: true
type: string
permissions:
contents: read
concurrency:
group: release-${{ github.repository }}
cancel-in-progress: false
jobs:
validate:
name: Validate release tag
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
version: ${{ steps.metadata.outputs.version }}
tag: ${{ steps.metadata.outputs.tag }}
prerelease: ${{ steps.metadata.outputs.prerelease }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref }}
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Match the tag to Cargo.toml and require a main commit
id: metadata
env:
RELEASE_TAG: ${{ github.event_name == 'workflow_dispatch' && inputs.tag || github.ref_name }}
run: python scripts/validate_release.py --tag "$RELEASE_TAG"
checks:
name: Release checks
needs: validate
if: ${{ github.event_name == 'push' }}
uses: ./.github/workflows/ci.yml
publish:
name: Publish to crates.io
needs: [validate, checks]
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: dtolnay/rust-toolchain@stable
- name: Require the publishing secret
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
if [ -z "$CARGO_REGISTRY_TOKEN" ]; then
echo '::error::Set the repository Actions secret CARGO_REGISTRY_TOKEN to a crates.io publishing token.'
exit 1
fi
- name: Verify publication without uploading
run: cargo publish --registry crates-io --locked --all-features --dry-run
- name: Publish the tagged version
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: cargo publish --registry crates-io --locked --all-features
github-release:
name: Create GitHub Release
needs: [validate, publish]
if: ${{ always() && needs.validate.result == 'success' && (github.event_name == 'workflow_dispatch' || needs.publish.result == 'success') }}
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: write
steps:
- name: Download the archive published to crates.io
env:
RELEASE_VERSION: ${{ needs.validate.outputs.version }}
run: |
set -euo pipefail
mkdir -p release-artifacts
archive="release-artifacts/agentstat-${RELEASE_VERSION}.crate"
curl --fail --location --silent --show-error \
--retry 12 --retry-all-errors --retry-delay 5 \
--connect-timeout 10 --max-time 30 \
--output "$archive" \
"https://static.crates.io/crates/agentstat/agentstat-${RELEASE_VERSION}.crate"
test -s "$archive"
tar -tzf "$archive" > /dev/null
- name: Create release notes and attach the crate
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
RELEASE_TAG: ${{ needs.validate.outputs.tag }}
RELEASE_VERSION: ${{ needs.validate.outputs.version }}
PRERELEASE: ${{ needs.validate.outputs.prerelease }}
run: |
archive="release-artifacts/agentstat-${RELEASE_VERSION}.crate"
if gh release view "$RELEASE_TAG" > /dev/null 2>&1; then
gh release upload "$RELEASE_TAG" "$archive" --clobber
elif [ "$PRERELEASE" = true ]; then
gh release create "$RELEASE_TAG" "$archive" --verify-tag --generate-notes --title "$RELEASE_TAG" --prerelease
else
gh release create "$RELEASE_TAG" "$archive" --verify-tag --generate-notes --title "$RELEASE_TAG"
fi