name: release-crate
on:
  release:
    types:
      - published

jobs:
  check-secret:
    runs-on: ubuntu-22.04
    if: contains(github.ref, 'v2.')
    environment: crates-io-static-web-server
    outputs:
      publish: ${{ steps.check.outputs.publish }}
    steps:
      - id: check
        env:
          CRATES_TOKEN: "${{ secrets.CRATES_TOKEN }}"
        if: "${{ env.CRATES_TOKEN != '' }}"
        run: echo "publish=true" >> $GITHUB_OUTPUT

  publish:
    needs: check-secret
    runs-on: ubuntu-22.04
    if: needs.check-secret.outputs.publish == 'true'
    environment: crates-io-static-web-server
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 1

      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@nightly

      - name: Publish workspace packages
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_TOKEN }}
          RUSTDOCFLAGS: '--cfg=docsrs --deny=warnings'
        run: cargo publish --features all --package static-web-server
