name: Release

on:
  release:
    types:
      - created
  push:
    branches:
      - main
    tags:
      - "*"

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: [3.8, 3.11, 3.12]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}
          cache: 'pip'

      - name: Install dependencies
        run: pip install .[all]

      - name: Run tests
        run: |
          pbs-install 3.10 -d test1 -v
          version=$(test1/bin/python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
          [[ $version == "3.10" ]] || exit 1

      - name: Run tests with build
        run: |
          pbs-install 3.10 -d test2 -v --build-dir
          version=$(test2/install/bin/python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
          [[ $version == "3.10" ]] || exit 1

  release:
    name: Build and Release
    runs-on: ubuntu-latest
    needs: test
    if: startsWith(github.ref, 'refs/tags/') || github.event_name == 'repository_dispatch'
    permissions:
      id-token: write
    environment:
      name: release
      url: https://pypi.org/project/pbs-install/
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-python@v5
        with:
          python-version: 3.11
          cache: 'pip'

      - name: Build
        run: pipx run build

      - name: Publish package distributions to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
