# Upload a Python Package using Twine when a release is created
# Adapted from mne-bids-pipeline

name: Build
on:  # yamllint disable-line rule:truthy
  release:
    types: [published]
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

permissions:
  contents: read

jobs:
  package:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up Python
        uses: actions/setup-python@v6
        with:
          python-version: '3.13'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install build twine
      - name: Build package
        run: python -m build --sdist --wheel
      - name: Check package
        run: twine check --strict dist/*
      - name: Check env vars
        run: |
          echo "Triggered by: ${{ github.event_name }}"
      - uses: actions/upload-artifact@v7
        with:
          name: dist
          path: dist

  # PyPI on release
  pypi:
    needs: package
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    permissions:
      id-token: write  # for trusted publishing
    environment:
      name: pypi
      url: https://pypi.org/p/sphinx-gallery
    steps:
      - uses: actions/download-artifact@v8
        with:
          name: dist
          path: dist
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
