name: release
on:
  push:
    branches:
      - main
  pull_request:
concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
jobs:
  cargo:
    permissions:
      id-token: write
    runs-on: ubuntu-latest
    environment: ${{ github.ref == 'refs/heads/main' && 'release' || '' }}
    steps:
      - uses: actions/checkout@v6
      - uses: raviqqe/cargo-cache@v1
      - run: cargo install cargo-workspaces
      - uses: rust-lang/crates-io-auth-action@v1
        id: auth
        if: github.ref == 'refs/heads/main'
      - run: cargo workspaces publish -y --from-git ${{ github.ref != 'refs/heads/main' && '--dry-run' || '' }}
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
  goreleaser:
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: raviqqe/cargo-cache@v1
      - id: version
        run: echo version=v$(cargo run -- --version | grep -o '[0-9.]*') > ${{ github.output }}
      - uses: raviqqe/goreleaser-action@v1
        with:
          version: ${{ steps.version.outputs.version }}
          snapshot: ${{ github.ref != 'refs/heads/main' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  release:
    needs:
      - cargo
      - goreleaser
    if: always()
    runs-on: ubuntu-latest
    steps:
      - run: for result in ${{ join(needs.*.result, ' ') }}; do [ $result = success ]; done
