name: Release

on:
  push:
    branches: [main]

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

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    if: "!startsWith(github.event.head_commit.message, 'chore:')"
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.INFRA_DEPLOY_TOKEN }}

      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      - name: Install knope
        run: |
          curl -sSL "https://github.com/knope-dev/knope/releases/latest/download/knope-x86_64-unknown-linux-musl.tgz" | tar xz --strip-components=1
          mv knope /usr/local/bin/

      - name: Configure git
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Authenticate with crates.io
        id: crates-auth
        uses: rust-lang/crates-io-auth-action@v1

      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.INFRA_DEPLOY_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ steps.crates-auth.outputs.token }}
        run: knope release || echo "No unreleased changes"
