name: Lanelet2 CD

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

jobs:
  build_wheels:
    name: Build wheel for ${{ matrix.python }}-${{ matrix.machine.platform }} on ${{ matrix.machine.os }}
    strategy:
      fail-fast: false
      matrix:
        machine:
          - { os: 'windows-latest', platform: 'win_amd64' }
          - { os: 'ubuntu-latest', platform: 'manylinux_x86_64' }
          - { os: 'macos-latest', platform: 'macosx_x86_64' }
        python: [ "cp39", "cp310", "cp311", "cp312" ]
    runs-on: ${{ matrix.machine.os }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.16.2
        env:
          CIBW_BUILD: ${{ matrix.python }}-${{ matrix.machine.platform }}

      - name: Upload wheels
        uses: actions/upload-artifact@v3
        with:
          name: lanelet2x.${{ matrix.python }}-${{ matrix.machine.platform }}.whl
          path: ./wheelhouse/*.whl

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: artifact
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Build sdist
        run: pipx run build --sdist

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: artifact
          path: dist/*.tar.gz

  publish:
    name: Publish
    if: contains(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    needs: [ build_wheels, build_sdist ]
    environment: pypi
    permissions:
      id-token: write
      contents: write

    steps:
      - name: Restore artifacts
        uses: actions/download-artifact@v3
        with:
          name: artifact
          path: dist

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          files: dist/*

      - name: Publish package to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1