name: Release

on:
  release:
    types: [published]

permissions:
    contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

      - name: Install uv
        uses: astral-sh/setup-uv@85856786d1ce8acfbcc2f13a5f3fbd6b938f9f41 # v7.1.2
        with:
          python-version: "3.13"

      - name: Set pyproject.toml version from release tag
        run: |
          uv version "${GITHUB_REF##*/}"

      - name: Install python
        run: uv python install

      - name: build
        run: uv build

      - name: Upload artifact
        uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
        with:
          name: package
          path: dist/
          retention-days: 7
          if-no-files-found: error

  pypi:
    if: startsWith(github.ref, 'refs/tags/')  # only publish to PyPI on tag pushes
    needs: [build]
    runs-on: ubuntu-latest
    permissions:
      id-token: write
    environment:
        name: gha
        url: https://pypi.org/project/jenkinsapi/
    steps:
      - name: Download artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: package
          path: dist

      - name: Show tree
        run: tree

      - name: Publish
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # v1.13.0

  asset:
    needs: [build]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download artifact
        uses: actions/download-artifact@018cc2cf5baa6db3ef3c5f8a56943fffe632ef53 # v6.0.0
        with:
          name: package
          path: dist

      - name: Show tree
        run: tree

      - name: Add release asset
        uses: softprops/action-gh-release@6da8fa9354ddfdc4aeace5fc48d7f679b5214090
        with:
          tag_name: ${{ github.event.release.tag_name }}
          fail_on_unmatched_files: true
          files: |
            dist/*
