# Make PyPI releases
name: Release

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      task:
        type: choice
        options: [release, test-release]
        default: release
        description: Release to PyPI or TestPyPI.

permissions:
  contents: read

jobs:
  test:
    # run test.yml first to ensure that the test suite is passing
    uses: ./.github/workflows/test.yml

  build_sdist:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - name: Check out repo
        uses: actions/checkout@v4

      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Build source distribution
        run: |
          pip install build
          python -m build --sdist

      - uses: actions/upload-artifact@v4
        with:
          path: dist/*.tar.gz

  build_wheels:
    needs: test
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, macos-13, windows-latest]
        python-version: ["310", "311", "312"]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out repo
        uses: actions/checkout@v4

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.16.5
        env:
          CIBW_BUILD: cp${{ matrix.python-version }}-*

      - name: Save artifact
        uses: actions/upload-artifact@v4
        with:
          name: dist-${{ matrix.os }}-${{ matrix.python-version }}
          path: ./wheelhouse/*.whl

  release:
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/p/pymatgen
    permissions:
      # For PyPI trusted publishing
      id-token: write
    steps:
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - name: Get build artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: dist-*
          merge-multiple: true
          path: dist

      - name: Publish to PyPI or TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true
          verbose: true
