name: Wheel building

on:
  schedule:
    # run every day at 4am UTC
    - cron: '0 4 * * *'
  workflow_dispatch:
  push:
  pull_request:
    # We also want this workflow triggered if the 'Build all wheels' label is added
    # or present when PR is updated
    types:
      - synchronize
      - labeled

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    # This does the actual wheel building as part of the cron job
    # or if triggered manually via the workflow dispatch, or for a tag.
    permissions:
      contents: none
    uses: OpenAstronomy/github-actions-workflows/.github/workflows/publish.yml@2835f0cacddf3f8de198db9afdb5354a5cebe0ef # v2.6.3
    if: |
      github.repository == 'astropy/astropy' && (
        startsWith(github.ref, 'refs/tags/v') ||
        github.event_name == 'schedule' ||
        github.event_name == 'workflow_dispatch' ||
        contains(github.event.pull_request.labels.*.name, 'Build all wheels')
      )
    with:

      # We use trusted publishing so the upload is handled in a separate job below
      upload_to_pypi: false
      save_artifacts: true
      upload_to_anaconda: ${{ (github.event_name == 'schedule' || github.event_name == 'workflow_dispatch') }}
      anaconda_user: astropy
      anaconda_package: astropy
      anaconda_keep_n_latest: 10
      env: |
        EXTENSION_HELPERS_PY_LIMITED_API: 'cp311'

      test_extras: test
      # FIXME: we exclude the test_data_out_of_range test since it
      # currently fails, see https://github.com/astropy/astropy/issues/10409
      # We also exclude test_set_locale as it sometimes relies on the correct locale
      # packages being installed, which it isn't always.
      test_command: pytest -Wdefault --astropy-header -m "not hypothesis" -k "not test_data_out_of_range and not test_set_locale and not TestQuantityTyping" --strict-markers --pyargs astropy
      targets: |
        # Linux wheels
        - cp3*-manylinux_x86_64
        - target: cp3*-manylinux_aarch64
          runs-on: ubuntu-24.04-arm

        # Note that following wheels are not currently tested:
        - cp3*-musllinux_x86_64

        # MacOS X wheels - as noted in https://github.com/astropy/astropy/pull/12379 we deliberately
        # do not build universal2 wheels.
        - cp3*-macosx_x86_64
        - cp3*-macosx_arm64

        # Windows wheels
        - cp3*-win32
        - cp3*-win_amd64
        - cp3*-win_arm64

    secrets:
      anaconda_token: ${{ secrets.anaconda_token }}

  upload:
    # Upload to PyPI using trusted publishing for all tags starting with v but not ones ending in .dev
    if: startsWith(github.ref, 'refs/tags/v') && !endsWith(github.ref, '.dev') && github.event_name == 'push'
    name: Upload release to PyPI
    runs-on: ubuntu-latest
    needs: [build]
    environment: pypi
    permissions:
      id-token: write
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          merge-multiple: true
          pattern: dist-*
          path: dist
      - name: Upload to PyPI
        uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
