name: Deploy

on:
  push:
    branches:
      - master
      - "*deploy*"
  release:
    types:
      - published

jobs:
  build:
    if: github.repository == 'pytest-dev/iniconfig'
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Cache
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: deploy-${{ hashFiles('**/pyproject.toml') }}
          restore-keys: |
            deploy-

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

      - name: Install build + twine
        run: python -m pip install build twine setuptools_scm

      - name: git describe output
        run: git describe --tags

      - id: scm_version
        run: |
          VERSION=$(python -m setuptools_scm --strip-dev)
          echo SETUPTOOLS_SCM_PRETEND_VERSION=$VERSION >> $GITHUB_ENV

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

      - name: Publish package to PyPI
        if: github.event.action == 'published'
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}

      - name: Publish package to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          user: __token__
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/
