name: Release

on:
  push:
    branches:
      - main
    paths:
      - '**'
      - '!.github/**'
      - '.github/workflows/release.yml'
      - '!.pre-commit-config.yaml'

permissions:
  contents: read

jobs:
  coverage:
    name: coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov
      - name: Generate code coverage
        run: cargo llvm-cov --lcov --output-path lcov.info
      - name: Upload to Coveralls
        uses: coverallsapp/github-action@v2
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}

  release-please:
    needs: coverage
    runs-on: ubuntu-slim
    environment: release
    permissions:
      contents: write # for googleapis/release-please-action to create release commit
      issues: write # for googleapis/release-please-action to create labels
      pull-requests: write # for googleapis/release-please-action to create release PR
    steps:
      - uses: actions/create-github-app-token@v3
        id: app-token
        with:
          app-id: ${{ vars.CI_GITHUB_APP_ID }}
          private-key: ${{ secrets.CI_GITHUB_APP_PRIVATE_KEY }}
      - uses: googleapis/release-please-action@v5
        id: release
        with:
          token: ${{ steps.app-token.outputs.token }}
          target-branch: main
    outputs:
      release_created: ${{ steps.release.outputs.release_created }}
      release_tag_name: ${{ steps.release.outputs.tag_name }}

  release:
    if: ${{ needs.release-please.outputs.release_created }}
    needs: release-please
    runs-on: ubuntu-slim
    environment: release
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-release
      - name: publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo release publish --execute --no-confirm
