name: Publish
on: push

jobs:
  build:
    name: Build distribution 📦
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
      - uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5

      - name: Install dependencies
        run: python3 -m pip install .
      - name: Install pypa/build
        run: >-
          python3 -m pip install build --user
      - name: Build a binary wheel and a source tarball
        run: python3 -m build
      - name: Store the distribution packages
        uses: actions/upload-artifact@834a144ee995460fba8ed112a2fc961b36a5ec5a # v4
        with:
          name: python-package-distributions
          path: dist/
  publish-to-pypi:
    name: Publish/PyPI
    if: startsWith(github.ref, 'refs/tags/v')
    needs:
      - build
    runs-on: ubuntu-latest
    environment:
      name: release
      url: https://pypi.org/p/ecourts
    permissions:
      id-token: write
    steps:
      - name: Download all the dists
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
        with:
          name: python-package-distributions
          path: dist/
      - name: Publish distribution 📦 to PyPI
        uses: pypa/gh-action-pypi-publish@ec4db0b4ddc65acdf4bff5fa45ac92d78b56bdf0 # release/v1
  github-release:
    name: Upload Signed release
    needs:
      - publish-to-pypi
    runs-on: ubuntu-latest

    permissions:
      contents: write
      id-token: write

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
        with:
          name: python-package-distributions
          path: dist/
      - name: Sign the dists with Sigstore
        uses: sigstore/gh-action-sigstore-python@f514d46b907ebcd5bedc05145c03b69c1edd8b46 # v3.0.0
        with:
          inputs: ./dist/*.tar.gz ./dist/*.whl
      - name: Create GitHub Release
        if: startsWith(github.ref, 'refs/tags/') # only tags are signed
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: gh release create '${{ github.ref_name }}' --repo '${{ github.repository }}' --generate-notes --latest --verify-tag dist/**
