name: Test Emscripten/Pyodide build

on:
  pull_request:
    branches:
      - main
      - maintenance/**
  # Note: this workflow gets triggered on the same schedule as the
  # wheels.yml workflow to upload WASM wheels to Anaconda.org.
  schedule:
    #        ┌───────────── minute (0 - 59)
    #        │  ┌───────────── hour (0 - 23)
    #        │  │ ┌───────────── day of the month (1 - 31)
    #        │  │ │ ┌───────────── month (1 - 12 or JAN-DEC)
    #        │  │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
    #        │  │ │ │ │
    - cron: "42 2 * * SUN,WED"
  workflow_dispatch:
    inputs:
      push_wheels:
        # Can be 'true' or 'false'. Default is 'false'.
        # Warning: this will overwrite existing wheels.
        description: >
          Push wheels to Anaconda.org if the build succeeds
        required: false
        default: 'false'

env:
  FORCE_COLOR: 3

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true


jobs:
  build-wasm-emscripten:
    permissions:
      contents: read  # to fetch code (actions/checkout)
    name: Build NumPy distribution for Pyodide
    runs-on: ubuntu-22.04
    # To enable this workflow on a fork, comment out:
    if: github.repository == 'numpy/numpy'
    steps:
      - name: Checkout NumPy
        uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683  # v4.2.2
        with:
          submodules: recursive
          fetch-tags: true
          persist-credentials: false

      - uses: pypa/cibuildwheel@9c00cb4f6b517705a3794b22395aedc36257242c  # v3.2.1
        env:
          CIBW_PLATFORM: pyodide

      - name: Upload wheel artifact(s)
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02  # v4.6.2
        with:
          name: cp312-pyodide_wasm32
          path: ./wheelhouse/*.whl
          if-no-files-found: error

  # Push to https://anaconda.org/scientific-python-nightly-wheels/numpy
  # WARNING: this job will overwrite any existing WASM wheels.
  upload-wheels:
    name: Upload NumPy WASM wheels to Anaconda.org
    runs-on: ubuntu-22.04
    permissions: {}
    needs: [build-wasm-emscripten]
    if: >-
      (github.repository == 'numpy/numpy') &&
      (github.event_name == 'workflow_dispatch' && github.event.inputs.push_wheels == 'true') ||
      (github.event_name == 'schedule')
    steps:
      - name: Download wheel artifact(s)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093  # v4.3.0
        with:
          path: wheelhouse/
          merge-multiple: true

      - name: Push to Anaconda PyPI index
        uses: scientific-python/upload-nightly-action@b36e8c0c10dbcfd2e05bf95f17ef8c14fd708dbf  # v0.6.2
        with:
          artifacts_path: wheelhouse/
          anaconda_nightly_upload_token: ${{ secrets.NUMPY_NIGHTLY_UPLOAD_TOKEN }}
