Skip to content

Merge pull request #77 from patrickfleith/fix-docs-deploy #6

Merge pull request #77 from patrickfleith/fix-docs-deploy

Merge pull request #77 from patrickfleith/fix-docs-deploy #6

Workflow file for this run

name: Publish Python Package
on:
push:
branches: [ main ]
workflow_dispatch: # Allows manual triggering
# Required permissions for the workflow to function properly
permissions:
contents: write # Needed to push version bump commit and tags
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine setuptools wheel
- name: Configure Git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
- name: Bump version
id: version_bump
run: |
# Extract current version from pyproject.toml - more robust pattern
CURRENT_VERSION=$(grep -o 'version = "[0-9]*\.[0-9]*\.[0-9]*"' pyproject.toml | grep -o '[0-9]*\.[0-9]*\.[0-9]*')
# Trim any whitespace
CURRENT_VERSION=$(echo "$CURRENT_VERSION" | xargs)
echo "Current version: '$CURRENT_VERSION'"
# Validate version format
if [[ ! "$CURRENT_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid version format: $CURRENT_VERSION"
cat pyproject.toml # Debug: print the file contents
echo "---"
grep "version" pyproject.toml # Debug: check for version line
exit 1
fi
# Get latest tag version (if any)
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
TAG_VERSION=${LATEST_TAG#v}
echo "Latest tag version: '$TAG_VERSION'"
# Compare versions to detect manual change
if [ "$CURRENT_VERSION" = "$TAG_VERSION" ]; then
# No manual change, so increment patch version
IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR="${VERSION_PARTS[0]}"
MINOR="${VERSION_PARTS[1]}"
PATCH="${VERSION_PARTS[2]}"
# Increment patch version
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
# Update pyproject.toml with new version
sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml
echo "Incremented version to: '$NEW_VERSION'"
else
# Manual version change detected
NEW_VERSION="$CURRENT_VERSION"
echo "Manual version change detected. Keeping version: '$NEW_VERSION'"
fi
# Validate final version
if [[ ! "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Error: Invalid final version format: $NEW_VERSION"
exit 1
fi
# Set output for later steps
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
- name: Build package
run: python -m build
- name: Commit version bump
run: |
git add pyproject.toml
# Only commit if there are changes to commit
if ! git diff --cached --quiet; then
git commit -m "Bump version to ${{ steps.version_bump.outputs.new_version }} [skip docs] [skip ci]"
else
echo "No version changes to commit"
fi
- name: Tag version
run: |
git tag -a "v${{ steps.version_bump.outputs.new_version }}" -m "Version ${{ steps.version_bump.outputs.new_version }}"
git push origin main --tags
- name: Publish to PyPI
env:
TWINE_USERNAME: "__token__"
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: |
twine check dist/*
twine upload dist/*