name: Publishing

on:
  workflow_dispatch:
  push:
    tags:
      - '*'

jobs:

  tests:
    name: Run tests
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: 3.13
      - run: pip install tox
      - run: tox
        env:
          TOXENV: 3.13

  linters:
    name: Run linters
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        toxenv: [flake8, pydocstyle, mypy, pylint]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: 3.13
      - run: pip install tox
      - run: tox
        env:
          TOXENV: ${{ matrix.toxenv }}

  build-sdist:
    name: Build source tarball
    needs: [tests, linters]
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: 3.13
      - run: |
          python -m pip install --upgrade build
          python -m build --sdist
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-sdist
          path: ./dist/*

  build-wheels:
    name: Build wheels
    needs: [tests, linters]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-24.04, ubuntu-24.04-arm, windows-2019, macos-14]
    env:
      CIBW_SKIP: cp27-*
    steps:
      - uses: actions/checkout@v3
      - name: Build wheels
        uses: pypa/cibuildwheel@v2.20.0
      - uses: actions/upload-artifact@v4
        with:
          name: cibw-wheels-x86-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

  publish:
    name: Publish on PyPI
    needs: [build-sdist, build-wheels]
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: cibw-*
          path: dist
          merge-multiple: true
      - uses: pypa/gh-action-pypi-publish@release/v1
        with:
          user: __token__
          password: ${{ secrets.PYPI_API_TOKEN }}
#           For publishing to Test PyPI, uncomment next two lines:
#          password: ${{ secrets.TEST_PYPI_API_TOKEN }}
#          repository_url: https://test.pypi.org/legacy/

  publish-docs:
    name: Publish docs
    needs: [publish]
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-python@v4
        with:
          python-version: 3.13
      - run: pip install awscli
      - run: pip install -r requirements-doc.txt
      - run: pip install -e .
      - run: (cd docs && make clean html)
      - run: |
          aws s3 sync docs/_build/html s3://python-dependency-injector-docs --delete
          aws cloudfront create-invalidation --distribution-id ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} --path "/*" > /dev/null
          echo "Cache invalidation triggered"
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
