name: Wheels

on:
  release:
    types:
      - published
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: read

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

jobs:

  build_artifacts:
    name: Build wheel on ubuntu-latest
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          submodules: true
          fetch-depth: 0
          persist-credentials: false

      - uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
        name: Install Python
        with:
          python-version: '3.12'

      - name: Install Hatch
        uses: pypa/hatch@257e27e51a6a5616ed08a39a408a21c35c9931bc
        with:
          version: '1.16.5'
      - name: Build wheel and sdist
        run: hatch build
      - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: releases
          path: dist

  test_dist_pypi:
    name: Test distribution artifacts
    needs: [build_artifacts]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
        with:
          name: releases
          path: dist

      - name: test
        run: |
          ls
          ls dist

  upload_pypi:
    name: Upload to PyPI
    needs: [build_artifacts, test_dist_pypi]
    runs-on: ubuntu-latest
    if: github.event_name == 'release'
    environment:
      name: releases
      url: https://pypi.org/p/zarr
    permissions:
      id-token: write # Required for OIDC trusted publishing to PyPI
      attestations: write # Required for artifact attestation
      artifact-metadata: write # Required for artifact attestation metadata
    steps:
      - uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0
        with:
          name: releases
          path: dist
      - name: Generate artifact attestation
        uses: actions/attest@59d89421af93a897026c735860bf21b6eb4f7b26 # v4.1.0
        with:
          subject-path: dist/*
      - name: Publish package to PyPI
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0
