name: Build sdist and wheels
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#concurrency
# https://docs.github.com/en/developers/webhooks-and-events/events/github-event-types#pullrequestevent
concurrency:
  group: ${{ github.workflow }}-${{ github.event.number || github.event.ref }}-${{ github.event.type }}
  cancel-in-progress: true

on:
  push:
  pull_request:
  release:
    types:
      - published

jobs:
  build_sdist:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source
        uses: actions/checkout@v6

      - name: Create sdist
        shell: bash -l {0}
        # For non-tags/releases this won't produce a valid version number in the package
        # We'd have to download the entire git history which is wasteful in CI
        run: |
          python -m pip install -q build
          python -m build -s

      - name: Upload sdist to build artifacts
        uses: actions/upload-artifact@v5
        with:
          name: sdist
          path: dist/*.tar.gz


  build_wheels:
    name: "Build wheels on ${{ matrix.os }} ${{ matrix.cibw_archs }}"
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            cibw_archs: "AMD64"
          - os: windows-latest
            cibw_archs: "ARM64"
          - os: macos-13
            cibw_archs: "x86_64"
          - os: macos-14
            cibw_archs: "arm64"
          - os: "ubuntu-24.04-arm"
            cibw_archs: "aarch64"
          - os: "ubuntu-22.04"
            cibw_archs: "x86_64"

    steps:
      - uses: actions/checkout@v6
      - run: |
          git fetch --prune --unshallow

      - name: Build wheels
        uses: pypa/cibuildwheel@v3.3.0
        env:
          CIBW_SKIP: "cp39-* cp310-* *i686 *-musllinux*"
          CIBW_ENABLE: cpython-freethreading
          CIBW_ARCHS: "${{ matrix.cibw_archs }}"
          CIBW_TEST_COMMAND: "python -c \"import pyresample; assert 'unknown' not in pyresample.__version__, 'incorrect version found'\""
          CIBW_TEST_SKIP: "*-win_arm64"
          CIBW_BUILD_VERBOSITY: 1

      - name: Upload wheel(s) as build artifacts
        uses: actions/upload-artifact@v5
        with:
          name: "wheels-${{ matrix.os }}-${{ matrix.cibw_archs }}"
          path: ./wheelhouse/*.whl

  upload_test_pypi:
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest
    # upload to PyPI on every tag starting with 'v'
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    steps:
      - name: Download sdist artifact
        uses: actions/download-artifact@v6
        with:
          name: sdist
          path: dist
      - name: Download wheels artifact
        uses: actions/download-artifact@v6
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist
      - name: Publish package to PyPI
        if: github.event.action != 'published'
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.test_pypi_password }}
          repository_url: https://test.pypi.org/legacy/

  upload_pypi:
    needs: [build_sdist, build_wheels]
    runs-on: ubuntu-latest
    steps:
      - name: Download sdist artifact
        uses: actions/download-artifact@v6
        with:
          name: sdist
          path: dist
      - name: Download wheels artifact
        uses: actions/download-artifact@v6
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist
      - name: Publish package to PyPI
        if: github.event.action == 'published'
        uses: pypa/gh-action-pypi-publish@v1.13.0
        with:
          user: __token__
          password: ${{ secrets.pypi_password }}
          skip_existing: true
