name: Publish to pypi

on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"

permissions: {}

jobs:
  build-distribution:
    name: Build distribution
    outputs:
      status: ${{ job.status }}
    permissions:
      contents: read
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.12"]

    steps:
      - name: Checkout repository
        uses: actions/checkout@v5
        with:
          persist-credentials: false

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v6
        with:
          python-version: ${{ matrix.python-version }}

      - name: Install dependencies
        run: |
          python -m venv ./venv
          source ./venv/bin/activate
          echo "PATH=${PATH}" >> "${GITHUB_ENV}"
          python -m pip install --upgrade pip
          python -m pip install --group all .

      - name: Build a binary wheel and a source tarball
        run: |
          source ./venv/bin/activate
          python3 -m build

      - name: Store the distribution packages
        uses: actions/upload-artifact@v4
        with:
          name: python-package-distributions
          path: dist/

  publish-to-pypi:
    environment:
      name: pypi
      url: https://pypi.org/p/senzing
    if: startsWith(github.ref, 'refs/tags/') # only publish to PyPI on tag pushes
    name: Publish Python distribution to PyPI
    needs:
      - build-distribution
    outputs:
      status: ${{ job.status }}
    permissions:
      id-token: write
    runs-on: ubuntu-latest

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v5
        with:
          name: python-package-distributions
          path: dist/

      - name: Publish distribution to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1

  github-release:
    name: Sign the Python distribution with Sigstore and upload them to GitHub Release
    needs:
      - publish-to-pypi
    outputs:
      status: ${{ job.status }}
    permissions:
      contents: write # IMPORTANT: mandatory for making GitHub Releases
      id-token: write # IMPORTANT: mandatory for sigstore
    runs-on: ubuntu-latest

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v5
        with:
          name: python-package-distributions
          path: dist/

      - name: Sign the dists with Sigstore
        uses: sigstore/gh-action-sigstore-python@v3.0.1
        with:
          inputs: >-
            ./dist/*.tar.gz
            ./dist/*.whl

      - name: Upload artifact signatures to GitHub Release
        env:
          GITHUB_REF: ${{ github.ref_name }}
          GITHUB_TOKEN: ${{ github.token }}
        # Upload to GitHub Release using the `gh` CLI.
        # `dist/` contains the built packages, and the
        # sigstore-produced signatures and certificates.
        run: gh release upload '${{ env.GITHUB_REF }}' dist/** --repo '${{ github.repository }}'

  slack-notification:
    needs: [build-distribution, publish-to-pypi, github-release]
    if: ${{ always() && contains(fromJSON('["failure", "cancelled"]'), needs.build-distribution.outputs.status ) && contains(fromJSON('["failure", "cancelled"]'), needs.publish-to-pypi.outputs.status ) && contains(fromJSON('["failure", "cancelled"]'), needs.github-release.outputs.status ) && github.ref_name == github.event.repository.default_branch }}
    secrets:
      SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
    uses: senzing-factory/build-resources/.github/workflows/build-failure-slack-notification.yaml@v3
    with:
      job-status: ${{ needs.build-distribution.outputs.status && needs.publish-to-pypi.outputs.status && needs.github-release.outputs.status }}
