|
| 1 | +name: Publish to Test PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + id-token: write # Required for trusted publishing |
| 9 | + |
| 10 | +jobs: |
| 11 | + test-publish: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout repository |
| 16 | + uses: actions/checkout@v6 |
| 17 | + |
| 18 | + - name: Set up Python |
| 19 | + uses: actions/setup-python@v6 |
| 20 | + with: |
| 21 | + python-version: '3.12' # tomllib requires >= 3.11 |
| 22 | + |
| 23 | + - name: Install uv |
| 24 | + uses: astral-sh/setup-uv@v7 |
| 25 | + |
| 26 | + - name: Append .dev suffix for unique Test PyPI versions |
| 27 | + run: | |
| 28 | + python -c " |
| 29 | + import tomllib, pathlib, re |
| 30 | + path = pathlib.Path('pyproject.toml') |
| 31 | + text = path.read_text() |
| 32 | + data = tomllib.loads(text) |
| 33 | + version = data['project']['version'] |
| 34 | + dev_version = f'{version}.dev${{ github.run_number }}' |
| 35 | + # Only replace the version inside the [project] section to avoid |
| 36 | + # accidentally matching a version key in [tool.*] sections. |
| 37 | + def replace_in_project_section(text, old_ver, new_ver): |
| 38 | + project_match = re.search(r'^\[project\]', text, re.MULTILINE) |
| 39 | + if not project_match: |
| 40 | + raise RuntimeError('[project] section not found in pyproject.toml') |
| 41 | + start = project_match.start() |
| 42 | + # Find the next top-level section header or end of file |
| 43 | + next_section = re.search(r'^\[(?!project[.\]])', text[start+1:], re.MULTILINE) |
| 44 | + end = (start + 1 + next_section.start()) if next_section else len(text) |
| 45 | + section = text[start:end] |
| 46 | + section = re.sub( |
| 47 | + r'(version\s*=\s*\")' + re.escape(old_ver) + r'\"', |
| 48 | + r'\g<1>' + new_ver + '\"', |
| 49 | + section, count=1, |
| 50 | + ) |
| 51 | + return text[:start] + section + text[end:] |
| 52 | + text = replace_in_project_section(text, version, dev_version) |
| 53 | + path.write_text(text) |
| 54 | + print(f'Version set to {dev_version}') |
| 55 | + " |
| 56 | +
|
| 57 | + - name: Build package |
| 58 | + run: uv build |
| 59 | + |
| 60 | + - name: Publish to Test PyPI |
| 61 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 62 | + with: |
| 63 | + repository-url: https://test.pypi.org/legacy/ |
0 commit comments