name: Publish to crates.io

on:
  release:
    types:
      - published
  workflow_dispatch:

permissions: {}

jobs:
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest

    environment:
      name: crates.io
      url: https://crates.io/crates/helr

    permissions:
      id-token: write # for trusted publishing to crates.io

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - uses: dtolnay/rust-toolchain@4be9e76fd7c4901c61fb841f559994984270fce7 # stable

      - uses: rust-lang/crates-io-auth-action@b7e9a28eded4986ec6b1fa40eeee8f8f165559ec # v1.0.3
        id: auth

      - name: Publish helr
        run: |
          echo "::notice::Publishing helr (dry-run: ${DRY_RUN})"
          cargo publish ${DRY_RUN}
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
          DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && '--dry-run' || '' }}
