name: Publish integration package to PyPI

on:
  push:
    tags:
      - "prefect-\\w+-[0-9]+.*"

jobs:
  build-pypi-dists:
    name: Build Python package
    runs-on: ubuntu-latest
    outputs:
      PACKAGE_NAME: ${{ steps.package_name.outputs.PACKAGE_NAME }}
      PACKAGE_VERSION: ${{ steps.package_version.outputs.PACKAGE_VERSION }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          ref: ${{ github.event.inputs.commit }}
          fetch-depth: 0
          persist-credentials: false

      - name: Extract package name
        id: package_name
        run: |
          TAG_NAME=${{ github.ref_name }}
          PACKAGE_NAME=$(echo $TAG_NAME | sed 's/-[0-9].*$//')
          echo "PACKAGE_NAME=$PACKAGE_NAME" >> $GITHUB_OUTPUT

      - name: Extract package version
        id: package_version
        run: |
          TAG_NAME=${{ github.ref_name }}
          PACKAGE_VERSION=$(echo $TAG_NAME | sed -E 's/.*-([0-9][0-9A-Za-z\.\-]*)$/\1/')
          echo "PACKAGE_VERSION=$PACKAGE_VERSION" >> $GITHUB_OUTPUT

      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.10"
          cache: "pip"
          cache-dependency-path: "requirements*.txt"

      - name: Set up uv
        uses: astral-sh/setup-uv@v7
        if: steps.package_name.outputs.PACKAGE_NAME == 'prefect-aws'
        with:
          version: latest

      - name: Set up node
        uses: actions/setup-node@v6
        if: steps.package_name.outputs.PACKAGE_NAME == 'prefect-aws'
        with:
          node-version-file: "src/integrations/prefect-aws/infra/.nvmrc"
          cache-dependency-path: "src/integrations/prefect-aws/infra/package-lock.json"

      - name: Set up just
        uses: extractions/setup-just@v3

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install --upgrade build
        working-directory: src/integrations/${{ steps.package_name.outputs.PACKAGE_NAME }}

      - name: Generate CloudFormation templates
        if: steps.package_name.outputs.PACKAGE_NAME == 'prefect-aws'
        run: |
          just generate-cfn
        working-directory: src/integrations/prefect-aws/infra

      - name: Build a binary wheel and a source tarball
        run: |
          python -m build --wheel
          python -m build --sdist
        working-directory: src/integrations/${{ steps.package_name.outputs.PACKAGE_NAME }}

      - name: Publish build artifacts
        uses: actions/upload-artifact@v6
        with:
          name: ${{ steps.package_name.outputs.PACKAGE_NAME }}-pypi-dists
          path: "./src/integrations/${{ steps.package_name.outputs.PACKAGE_NAME }}/dist"

  publish-pypi-dists:
    name: Publish to PyPI
    environment: "prod"
    needs: build-pypi-dists
    runs-on: ubuntu-latest
    permissions:
      # this permission is mandatory for trusted publishing
      id-token: write

    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@v7
        with:
          name: ${{ needs.build-pypi-dists.outputs.PACKAGE_NAME}}-pypi-dists
          path: "./dist"

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

  get-prefect-version:
    name: Get latest Prefect version
    needs: [build-pypi-dists, publish-pypi-dists]
    if: needs.build-pypi-dists.outputs.PACKAGE_NAME == 'prefect-aws'
    runs-on: ubuntu-latest
    outputs:
      PREFECT_VERSION: ${{ steps.get_version.outputs.PREFECT_VERSION }}
    steps:
      - name: Get latest Prefect version
        id: get_version
        run: |
          PREFECT_VERSION=$(gh api repos/${{ github.repository }}/releases/latest --jq '.tag_name')
          echo "PREFECT_VERSION=$PREFECT_VERSION" >> $GITHUB_OUTPUT
        env:
          GH_TOKEN: ${{ github.token }}

  trigger-docker-build:
    name: Build prefect-aws Docker images
    needs: [build-pypi-dists, publish-pypi-dists, get-prefect-version]
    if: needs.build-pypi-dists.outputs.PACKAGE_NAME == 'prefect-aws'
    uses: ./.github/workflows/prefect-aws-docker-images.yaml
    with:
      prefect_version: ${{ needs.get-prefect-version.outputs.PREFECT_VERSION }}
      prefect_aws_version: ${{ needs.build-pypi-dists.outputs.PACKAGE_VERSION }}
    secrets: inherit
