name: Wheels

on:
  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@v6
        with:
          submodules: true
          fetch-depth: 0

      - uses: actions/setup-python@v6
        name: Install Python
        with:
          python-version: '3.11'

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

  test_dist_pypi:
    needs: [build_artifacts]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: releases
          path: dist

      - name: test
        run: |
          ls
          ls dist

  upload_pypi:
    needs: [build_artifacts, test_dist_pypi]
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags/v')
    environment:
      name: releases
      url: https://pypi.org/p/zarr
    permissions:
      id-token: write
      attestations: write
      artifact-metadata: write
    steps:
      - uses: actions/download-artifact@v7
        with:
          name: releases
          path: dist
      - name: Generate artifact attestation
        uses: actions/attest@v4
        with:
          subject-path: dist/*
      - name: Publish package to PyPI
        uses: pypa/gh-action-pypi-publish@v1.13.0
