name: Release

on:
  push:
    branches:
      - master
  release:
    types:
      - published

jobs:
  build:
    if: github.repository == 'jazzband/sorl-thumbnail'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Get pip cache dir
        id: pip-cache
        run: |
          echo "CACHE_DIR=$(pip cache dir)" >> "$GITHUB_OUTPUT"

      - name: Cache
        uses: actions/cache@v4
        with:
          path: ${{ steps.pip-cache.outputs.CACHE_DIR }}
          key: release-${{ hashFiles('**/pyproject.toml') }}
          restore-keys: |
            release-

      - name: Install dependencies
        run: |
          python -m pip install -U pip build twine

      - name: Build package
        run: |
          python -m build
          twine check dist/*

      - name: Upload packages to Jazzband
        if: github.event.action == 'published'
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          user: jazzband
          password: ${{ secrets.JAZZBAND_RELEASE_KEY }}
          repository_url: https://jazzband.co/projects/sorl-thumbnail/upload
