Publish to PyPI #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to PyPI | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| release_type: | |
| description: 'Release type (patch, minor, major)' | |
| required: true | |
| default: 'patch' | |
| type: choice | |
| options: | |
| - patch | |
| - minor | |
| - major | |
| create_release: | |
| description: 'Create GitHub Release' | |
| required: true | |
| default: true | |
| type: boolean | |
| jobs: | |
| build-and-publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v3 | |
| with: | |
| fetch-depth: 0 | |
| - 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 twine wheel setuptools ruff | |
| pip install -r requirements.txt | |
| if [ -f requirements-dev.txt ]; then pip install -r requirements-dev.txt; fi | |
| - name: Run Ruff linting | |
| run: | | |
| # Run Ruff linter | |
| ruff check . | |
| # Run Ruff formatter check (without modifying files) | |
| ruff format --check . | |
| - name: Run tests | |
| run: | | |
| pytest | |
| - name: Calculate new version | |
| id: version | |
| run: | | |
| # Get current version from pyproject.toml | |
| CURRENT_VERSION=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])") | |
| echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT | |
| # Parse version components | |
| IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" | |
| # Calculate new version based on release type | |
| case "${{ github.event.inputs.release_type }}" in | |
| "major") | |
| NEW_MAJOR=$((MAJOR + 1)) | |
| NEW_MINOR=0 | |
| NEW_PATCH=0 | |
| ;; | |
| "minor") | |
| NEW_MAJOR=$MAJOR | |
| NEW_MINOR=$((MINOR + 1)) | |
| NEW_PATCH=0 | |
| ;; | |
| "patch") | |
| NEW_MAJOR=$MAJOR | |
| NEW_MINOR=$MINOR | |
| NEW_PATCH=$((PATCH + 1)) | |
| ;; | |
| esac | |
| NEW_VERSION="${NEW_MAJOR}.${NEW_MINOR}.${NEW_PATCH}" | |
| echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION" | |
| - name: Update version files | |
| run: | | |
| CURRENT_VERSION="${{ steps.version.outputs.current_version }}" | |
| NEW_VERSION="${{ steps.version.outputs.new_version }}" | |
| # Update pyproject.toml | |
| sed -i "s/version = \"$CURRENT_VERSION\"/version = \"$NEW_VERSION\"/" pyproject.toml | |
| # Update __init__.py | |
| sed -i "s/__version__ = \"$CURRENT_VERSION\"/__version__ = \"$NEW_VERSION\"/" stagehand/__init__.py | |
| echo "Updated version to $NEW_VERSION in pyproject.toml and __init__.py" | |
| - name: Commit version bump | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| git add pyproject.toml stagehand/__init__.py | |
| git commit -m "Bump version to ${{ steps.version.outputs.new_version }}" | |
| git tag "v${{ steps.version.outputs.new_version }}" | |
| - name: Build package | |
| run: | | |
| python -m build | |
| - name: Upload to PyPI | |
| env: | |
| TWINE_USERNAME: __token__ | |
| TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
| run: | | |
| twine upload dist/* | |
| - name: Push version bump | |
| run: | | |
| git push | |
| git push --tags | |
| - name: Create GitHub Release | |
| if: ${{ github.event.inputs.create_release == 'true' }} | |
| uses: softprops/action-gh-release@v1 | |
| with: | |
| tag_name: v${{ steps.version.outputs.new_version }} | |
| name: Release v${{ steps.version.outputs.new_version }} | |
| generate_release_notes: true |