name: Publish Release

on:
  release:
    types: [published]

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  publish:
    name: publish crate
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7

      - name: Run tests
        run: cargo test --verbose

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          version=$(cargo metadata --format-version=1 --no-deps | jq -r '.packages[0].version')
          echo "## Publish Result" >> $GITHUB_STEP_SUMMARY
          if cargo publish; then
            echo ":white_check_mark: *clio-auth \`v${version}\` published*" >> $GITHUB_STEP_SUMMARY
          else
            echo ":x: clio-auth v${version} _*NOT*_ published!" >> $GITHUB_STEP_SUMMARY
            echo "See action log for details" >> $GITHUB_STEP_SUMMARY
            exit 1
          fi
