name: release

on:
  release:
    types: [published]
  workflow_dispatch:
  schedule:
    - cron: "0 3 * * 1"

env:
  FORCE_COLOR: 3

jobs:
  build:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          submodules: recursive

      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: 3.13

      # IMPORTANT: always build sdist, and then the wheel from
      # the sdist (like it is currently done here). This is
      # because we want to ensure that no extra files get
      # copied, which can be the case with building in-tree.
      # The MANIFEST.in file ensures that the sdist doesn't
      # contain any unnecessary files.
      - name: Build package distributions
        run: pipx run build

      - name: Store the distribution packages
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: python-package-distributions
          path: dist/
          if-no-files-found: error

  deploy:
    runs-on: ubuntu-latest
    needs: [build]
    if: github.event_name == 'release' && github.event.action == 'published'
    permissions:
      id-token: write
      attestations: write
      contents: read
    environment:
      name: pypi
      url: https://pypi.org/p/micropip
    steps:
      - name: Download all the dists
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          path: dist/
          merge-multiple: true

      - name: Generate artifact attestations
        uses: actions/attest-build-provenance@db473fddc028af60658334401dc6fa3ffd8669fd # v2.3.0
        with:
          subject-path: "dist/*"

      - name: Publish distribution 📦 to PyPI
        uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
