Skip to content

Upload Python Package to PyPi #108

Upload Python Package to PyPi

Upload Python Package to PyPi #108

name: Upload Python Package to PyPi
on:
workflow_run:
workflows: ["Run Tests", "Run Formatter"]
types:
- completed
branches:
- main
jobs:
check-workflows:
runs-on: ubuntu-latest
outputs:
should_publish: ${{ steps.check.outputs.should_publish }}
steps:
- name: Check workflow conclusions
id: check
env:
COMMIT_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
tests_status=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/run-tests.yml/runs?branch=main" \
| jq --arg SHA "$COMMIT_SHA" '.workflow_runs[] | select(.head_sha==$SHA) | .conclusion')
formatter_status=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/run-formatter.yml/runs?branch=main" \
| jq --arg SHA "$COMMIT_SHA" '.workflow_runs[] | select(.head_sha==$SHA) | .conclusion')
# Tests must succeed, Formatter must either succeed or not exist (sometimes it's not triggered)
if [ "$tests_status" = "\"success\"" ] && ([ "$formatter_status" = "\"success\"" ] || [ -z "$formatter_status" ]); then
echo "should_publish=true" >> $GITHUB_OUTPUT
else
echo "should_publish=false" >> $GITHUB_OUTPUT
echo "Tests status for commit $COMMIT_SHA: $tests_status"
echo "Formatter status for commit $COMMIT_SHA: $formatter_status"
fi
deploy:
needs: check-workflows
if: needs.check-workflows.outputs.should_publish == 'true'
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Version bump
uses: monperrus/pyproject-bump-pypi@main
id: bump_version
with:
file_to_bump: "./pyproject.toml"
bump_type: "minor"
- name: Get new version
id: get_version
run: |
VERSION=$(grep -m1 'version = ' pyproject.toml | cut -d '"' -f2)
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5.1.0
with:
commit_message: "Bump version to ${{ steps.get_version.outputs.version }}"
branch: ${{ github.head_ref }}
- name: Create and push tag
run: |
git tag v${{ steps.get_version.outputs.version }}
git push origin v${{ steps.get_version.outputs.version }}
- name: Install build dependencies
run: |
python -m pip install --upgrade pip
pip install build twine==6.0.0
- name: Build package
run: python -m build
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_KEY }}
run: |
twine upload dist/*