|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + inputs: |
| 8 | + version_bump: |
| 9 | + description: 'Type of version bump' |
| 10 | + required: true |
| 11 | + default: 'patch' |
| 12 | + type: choice |
| 13 | + options: |
| 14 | + - patch |
| 15 | + - minor |
| 16 | + - major |
| 17 | + |
| 18 | +jobs: |
| 19 | + bump-and-release: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + if: github.event_name == 'workflow_dispatch' |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + # Use a token with write permissions to push the version bump |
| 27 | + token: ${{ secrets.PAT }} |
| 28 | + |
| 29 | + - name: Set up Python |
| 30 | + uses: actions/setup-python@v4 |
| 31 | + with: |
| 32 | + python-version: '3.10' |
| 33 | + |
| 34 | + - name: Install tomlkit |
| 35 | + run: pip install tomlkit |
| 36 | + |
| 37 | + - name: Bump version |
| 38 | + id: bump_version |
| 39 | + run: | |
| 40 | + python -c " |
| 41 | + import tomlkit |
| 42 | + import os |
| 43 | +
|
| 44 | + bump_type = '${{ github.event.inputs.version_bump }}' |
| 45 | +
|
| 46 | + with open('pyproject.toml', 'r') as f: |
| 47 | + pyproject = tomlkit.parse(f.read()) |
| 48 | +
|
| 49 | + current_version = pyproject['project']['version'] |
| 50 | + major, minor, patch = map(int, current_version.split('.')) |
| 51 | +
|
| 52 | + if bump_type == 'major': |
| 53 | + major += 1 |
| 54 | + minor = 0 |
| 55 | + patch = 0 |
| 56 | + elif bump_type == 'minor': |
| 57 | + minor += 1 |
| 58 | + patch = 0 |
| 59 | + else: # patch |
| 60 | + patch += 1 |
| 61 | +
|
| 62 | + new_version = f'{major}.{minor}.{patch}' |
| 63 | + pyproject['project']['version'] = new_version |
| 64 | +
|
| 65 | + with open('pyproject.toml', 'w') as f: |
| 66 | + f.write(tomlkit.dumps(pyproject)) |
| 67 | +
|
| 68 | + # Use GITHUB_OUTPUT to pass the new version to other steps |
| 69 | + print(f"new_version={new_version}" >> $GITHUB_OUTPUT) |
| 70 | + " |
| 71 | +
|
| 72 | + - name: Commit version bump |
| 73 | + run: | |
| 74 | + git config --local user.email "[email protected]" |
| 75 | + git config --local user.name "GitHub Action" |
| 76 | + git add pyproject.toml |
| 77 | + git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}" |
| 78 | + git push |
| 79 | +
|
| 80 | + - name: Create Release |
| 81 | + uses: actions/create-release@v1 |
| 82 | + env: |
| 83 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + with: |
| 85 | + tag_name: v${{ steps.bump_version.outputs.new_version }} |
| 86 | + release_name: Release v${{ steps.bump_version.outputs.new_version }} |
| 87 | + body: "Automated release for version ${{ steps.bump_version.outputs.new_version }}" |
| 88 | + draft: false |
| 89 | + prerelease: false |
| 90 | + |
| 91 | + publish: |
| 92 | + runs-on: ubuntu-latest |
| 93 | + if: github.event_name == 'release' && github.event.action == 'published' |
| 94 | + steps: |
| 95 | + - uses: actions/checkout@v4 |
| 96 | + |
| 97 | + - name: Set up Python |
| 98 | + uses: actions/setup-python@v4 |
| 99 | + with: |
| 100 | + python-version: '3.10' |
| 101 | + |
| 102 | + - name: Install dependencies |
| 103 | + run: | |
| 104 | + python -m pip install --upgrade pip |
| 105 | + pip install build |
| 106 | +
|
| 107 | + - name: Build package |
| 108 | + run: python -m build |
| 109 | + |
| 110 | + - name: Publish package to PyPI |
| 111 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 112 | + with: |
| 113 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 114 | + skip_existing: true |
0 commit comments