Skip to content

Publish to PyPI

Publish to PyPI #16

Workflow file for this run

name: Publish to PyPI
on:
release:
types: [published]
workflow_dispatch:
inputs:
target:
description: 'Publish target'
required: true
default: 'test'
type: choice
options:
- test
- production
bump_type:
description: 'Version bump level'
required: true
default: 'patch'
type: choice
options:
- patch
- minor
- major
jobs:
publish:
runs-on: ubuntu-latest
environment:
name: ${{ (inputs.target == 'production' || github.event_name == 'release') && 'pypi' || 'testpypi' }}
url: ${{ (inputs.target == 'production' || github.event_name == 'release') && 'https://pypi.org/p/review-classification' || 'https://test.pypi.org/p/review-classification' }}
permissions:
id-token: write
contents: write # Required to push version bump commit
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0 # Fetch full history for proper git operations
- name: Install uv
uses: astral-sh/setup-uv@v4
with:
enable-cache: true
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version-file: "pyproject.toml"
- name: Bump Version
id: bump
run: |
BUMP_TYPE="${{ inputs.bump_type || 'patch' }}"
uv version --bump "$BUMP_TYPE"
NEW_VERSION=$(uv version --short)
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Update Changelog
run: |
VERSION="${{ steps.bump.outputs.version }}"
DATE=$(date +%Y-%m-%d)
PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$PREVIOUS_TAG" ]; then
COMMITS=$(git log --oneline --pretty=format:"- %s")
else
COMMITS=$(git log ${PREVIOUS_TAG}..HEAD --oneline --pretty=format:"- %s")
fi
# Prepare the new section
echo -e "\n## [$VERSION] - $DATE\n$COMMITS" > new_changelog_entry.md
# Prepend after "## [Unreleased]"
sed -i "/## \[Unreleased\]/r new_changelog_entry.md" CHANGELOG.md
rm new_changelog_entry.md
- name: Build
run: uv build
- name: Publish to TestPyPI
if: inputs.target == 'test' && github.event_name == 'workflow_dispatch'
run: uv publish --publish-url https://test.pypi.org/legacy/ --token ${{ secrets.UV_PUBLISH_TOKEN_TEST }}
- name: Publish to PyPI
if: inputs.target == 'production' || github.event_name == 'release'
run: uv publish --token ${{ secrets.UV_PUBLISH_TOKEN }}
- name: Commit Version Bump
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add pyproject.toml uv.lock CHANGELOG.md
git commit -m "chore: bump version to ${{ steps.bump.outputs.version }} [skip ci]"
git push
- name: Tag Version
run: |
git tag "v${{ steps.bump.outputs.version }}"
git push origin "v${{ steps.bump.outputs.version }}"