name: Post-release jobs

on:
  release:
    types: [published]

jobs:
  containers:
    name: Build container images
    runs-on: ubuntu-22.04

    strategy:
      fail-fast: false
      matrix:
        include:
          - dockerfile: Dockerfile
            tag-suffix: ""
          - dockerfile: Dockerfile.bookworm
            tag-suffix: "-bookworm"

    steps:
      - uses: actions/checkout@v6

      - name: Detect pre-release
        id: detect-prerelease
        run: |
          TAG_NAME="${{ github.event.release.tag_name }}"
          if [ "${{ github.event.release.prerelease }}" == "true" ] || [[ "$TAG_NAME" =~ -(alpha|beta|rc)[0-9]*$ ]]; then
            echo "is_prerelease=true" >> $GITHUB_ENV
          else
            echo "is_prerelease=false" >> $GITHUB_ENV
          fi
        shell: bash


      - name: Generate stable tag
        id: stable-tag
        run: |
          if [ "${{ env.is_prerelease }}" == "false" ]; then
            if [ "${{ matrix.tag-suffix }}" == "" ]; then
              echo "stable_tag=type=raw,value=stable" >> $GITHUB_OUTPUT
            elif [ "${{ matrix.tag-suffix }}" == "-bookworm" ]; then
              echo "stable_tag=type=raw,value=stable-bookworm" >> $GITHUB_OUTPUT
            else
              echo "stable_tag=" >> $GITHUB_OUTPUT
            fi
          else
            echo "stable_tag=" >> $GITHUB_OUTPUT
          fi

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          flavor: |
            latest=false
          images: |
            schemathesis/schemathesis
            ghcr.io/schemathesis/schemathesis
          tags: |
            type=semver,pattern={{raw}}${{ matrix.tag-suffix }}
            type=semver,pattern={{version}}${{ matrix.tag-suffix }}
            ${{ steps.stable-tag.outputs.stable_tag }}

      - name: Build metadata
        run: |
          echo "Tags: ${{ steps.meta.outputs.tags }}"
          echo "Is prerelease: ${{ env.is_prerelease }}"
          echo "Tag suffix: '${{ matrix.tag-suffix }}'"

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ${{ matrix.dockerfile }}
          platforms: linux/amd64,linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

  release_package:
    name: Build and publish package to pypi.org
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 1

      - uses: actions/setup-python@v6
        with:
          python-version: "3.10"

      - uses: astral-sh/setup-uv@v7

      - name: Build package
        run: uvx hatchling build

      - name: Publish distribution package to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
