Make PyPI Release #25
This file contains 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
# See https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries | |
name: Make PyPI Release | |
on: | |
workflow_run: | |
workflows: ["Python Tests"] | |
types: [completed] | |
workflow_dispatch: | |
release: | |
types: [published, released] | |
permissions: | |
contents: read | |
jobs: | |
check-version: | |
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }} | |
runs-on: ubuntu-latest | |
outputs: | |
should_publish: ${{ steps.check.outputs.should_publish }} | |
version: ${{ steps.version.outputs.version }} | |
package_name: ${{ steps.check.outputs.package_name }} | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: "3.x" | |
- name: Get package version | |
id: version | |
run: | | |
pip install toml | |
version=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])") | |
echo "version=$version" >> "$GITHUB_OUTPUT" | |
- name: Check PyPI version | |
id: check | |
run: | | |
set -e | |
pip install requests | |
version="${{ steps.version.outputs.version }}" | |
package_name=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])") | |
echo "package_name=$package_name" >> "$GITHUB_OUTPUT" | |
echo "Checking version $version for package $package_name" | |
if python -c "import requests, sys; package_name=sys.argv[1]; version=sys.argv[2]; exit(0 if version in requests.get(f'https://pypi.org/pypi/{package_name}/json').json()['releases'] else 1)" "$package_name" "$version"; then | |
echo "Version already on PyPI" | |
echo "should_publish=false" >> "$GITHUB_OUTPUT" | |
else | |
echo "New version - should publish" | |
echo "should_publish=true" >> "$GITHUB_OUTPUT" | |
fi | |
release-build: | |
needs: check-version | |
if: needs.check-version.outputs.should_publish == 'true' | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: actions/setup-python@v5 | |
with: | |
python-version: "3.x" | |
- name: Build release distributions | |
run: | | |
python -m pip install build | |
python -m build | |
- name: Upload distributions | |
uses: actions/upload-artifact@v4 | |
with: | |
name: release-dists | |
path: dist/ | |
pypi-publish: | |
needs: [check-version, release-build] | |
if: needs.check-version.outputs.should_publish == 'true' | |
runs-on: ubuntu-latest | |
environment: | |
name: pypi | |
url: https://pypi.org/project/${{ needs.check-version.outputs.package_name }}/${{ needs.check-version.outputs.version }} | |
permissions: | |
id-token: write | |
steps: | |
- name: Retrieve release distributions | |
uses: actions/download-artifact@v4 | |
with: | |
name: release-dists | |
path: dist/ | |
- name: Publish to PyPI | |
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc #v1.12.2 | |
with: | |
packages-dir: dist/ |