---
name: release
on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"
  pull_request:
    branches:
      - "master"

permissions:
  contents: read
  packages: write

env:
  CIBW_PROJECT_REQUIRES_PYTHON: ">=3.10"
  python-version: "3.10"

jobs:
  # Heavily inspired on https://github.com/tornadoweb/tornado/blob/master/.github/workflows/build.yml
  build_sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - name: Set up Python
        id: setup-python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.python-version }}
      - name: Install dependencies
        run: |
          pip install -U pip
      - name: Build package
        run: python setup.py sdist

      - uses: actions/upload-artifact@v4
        with:
          name: artifact-sdist
          path: ./dist/thumbor-*.tar.gz

  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macOS-latest

    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: ${{ env.python-version }}
      - name: Set up QEMU
        if: runner.os == 'Linux'
        uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3
        with:
          platforms: all

      - name: Build wheels
        uses: pypa/cibuildwheel@faf86a6ed7efa889faf6996aa23820831055001a # v2.23.3

      - uses: actions/upload-artifact@v4
        with:
          name: artifact-${{ matrix.os }}
          path: ./wheelhouse/*.whl
          overwrite: true

  upload_pypi:
    name: Upload to PyPI
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v7
        with:
          pattern: artifact-*
          path: dist
          merge-multiple: true

      - run: ls -lah dist

      - uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
        if: github.event_name != 'pull_request'
        with:
          user: __token__
          password: ${{ secrets.PYPI_API_TOKEN }}
          skip-existing: true

  docker:
    needs: upload_pypi
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python_version:
          - "3.10"
          - "3.11"
          - "3.12"
          - "3.13"
    steps:
      - name: Checkout
        uses: actions/checkout@v5
        with:
          fetch-depth: "0"

      - uses: actions/download-artifact@v7
        with:
          pattern: artifact-*
          path: dist
          merge-multiple: true

      - name: Set output on new tags
        if: github.event_name != 'pull_request'
        run: echo "THUMBOR_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

      - name: Set output on PR
        if: github.event_name == 'pull_request'
        run: echo "THUMBOR_VERSION=$(cat thumbor/__init__.py | grep "__version__" | cut -d'"' -f2)" >> $GITHUB_ENV

      - name: Enable docker cache push
        if: github.event_name != 'pull_request'
        run: echo "CACHE_TO=type=registry,ref=ghcr.io/thumbor/thumbor:buildcache-${{ matrix.python_version }},mode=max" >> $GITHUB_ENV

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

      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3

      - name: Login to GHCR
        if: github.event_name != 'pull_request'
        uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Login to Quay.io
        if: github.event_name != 'pull_request'
        uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_IO_USERNAME }}
          password: ${{ secrets.QUAY_IO_PASSWORD }}

      - name: Login to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5
        with:
          flavor: |
            suffix=-py-${{ matrix.python_version }}
          images: |
            ghcr.io/thumbor/thumbor
            quay.io/thumbor/thumbor
            thumbororg/thumbor
          tags: |
            type=schedule
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha

      - name: Build and push
        uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v5
        with:
          context: .
          file: ./docker/Dockerfile
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=registry,ref=ghcr.io/thumbor/thumbor:buildcache-${{ matrix.python_version }}
          cache-to: ${{ env.CACHE_TO }}
          build-args: |
            THUMBOR_VERSION=${{ env.THUMBOR_VERSION }}
            PYTHON_VERSION=${{ matrix.python_version }}
