Skip to content

Publish to PyPI

Publish to PyPI #2

Workflow file for this run

name: Publish to PyPI
on:
release:
types: [published]
workflow_dispatch:
inputs:
version_bump:
description: 'Type of version bump'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
jobs:
bump-and-release:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Use a token with write permissions to push the version bump
token: ${{ secrets.PAT }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install tomlkit
run: pip install tomlkit
- name: Bump version
id: bump_version
run: |
python -c "
import tomlkit
import os
bump_type = '${{ github.event.inputs.version_bump }}'
with open('pyproject.toml', 'r') as f:
pyproject = tomlkit.parse(f.read())
current_version = pyproject['project']['version']
major, minor, patch = map(int, current_version.split('.'))
if bump_type == 'major':
major += 1
minor = 0
patch = 0
elif bump_type == 'minor':
minor += 1
patch = 0
else: # patch
patch += 1
new_version = f'{major}.{minor}.{patch}'
pyproject['project']['version'] = new_version
with open('pyproject.toml', 'w') as f:
f.write(tomlkit.dumps(pyproject))
# Use GITHUB_OUTPUT to pass the new version to other steps
print(f"new_version={new_version}" >> $GITHUB_OUTPUT)
"
- name: Commit version bump
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add pyproject.toml
git commit -m "Bump version to ${{ steps.bump_version.outputs.new_version }}"
git push
- name: Create Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: v${{ steps.bump_version.outputs.new_version }}
release_name: Release v${{ steps.bump_version.outputs.new_version }}
body: "Automated release for version ${{ steps.bump_version.outputs.new_version }}"
draft: false
prerelease: false
publish:
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: python -m build
- name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}
skip_existing: true