name: release

on:
  push:
    tags:
      - "v*.*.*"

env:
  PYPI_URL: https://pypi.org/p/dinglehopper

jobs:
  test:
    uses: ./.github/workflows/test.yml

  build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Upgrade pip
        run: python3 -m pip install --upgrade pip
      - name: Install setuptools
        run: |
          python3 -m pip install --upgrade setuptools
          # For OCR-D tools, we need setuptools-ocrd to get the version
          if [ -e ocrd-tool.json ]; then
            python3 -m pip install setuptools-ocrd
          fi
      - name: Check git tag vs package version
        run: .github/workflows/release-check-version-tag
      - name: Build package
        run: python3 -m pip install --upgrade build && python3 -m build
      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/

  github-release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Download dist
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/
      - name: Create release on GitHub
        uses: softprops/action-gh-release@v1
        with:
          files: dist/*

  pypi-publish:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: ${{ env.PYPI_URL }}
    permissions:
      id-token: write  # IMPORTANT: this permission is mandatory for trusted publishing
    steps:
      - name: Download dist
        uses: actions/download-artifact@v4
        with:
          name: dist
          path: dist/
      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1