#  Workflow to perform a release on merge into main branch
# pushes a new tag and creates a GitHub release

name: Release
on:
  push:
    branches:
      - main

# set permissions for the workflow
permissions:
  contents: write
  packages: write
  id-token: write

jobs:
  release:
    runs-on: ubuntu-latest
    ## NOT on a specific commit
    # NOT IF THE LAST COMMIT IS A RELEASE COMMIT
    if: "!contains(github.event.head_commit.message, 'Bump version to v:')"
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Configure git identity
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
      - name: Cache cargo
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Install Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: 1.88.0
          components: rustfmt, clippy

      - name: Login to Cargo Registry
        run: |
          echo "${{ secrets.CARGO_REGISTRY_TOKEN }}" | cargo login

      - name: Create Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          make release
