Skip to content

Add workflow to automatically publish to PyPI #49

Add workflow to automatically publish to PyPI

Add workflow to automatically publish to PyPI #49

name: Publish Python 🐍 distributions πŸ“¦ to PyPI and TestPyPI
on:
push:
branches:
- main
# This job should be kept in sync with the job in `build-test.yml`,
# except the version in that file should actually publish the built artifacts,
# not just validate that they build.
jobs:
build-n-publish:
name: Build and publish Python 🐍 distributions πŸ“¦ to PyPI and TestPyPI
runs-on: ubuntu-latest
steps:
- name: Check out Repo
uses: actions/checkout@v3
with:
fetch-depth: 2 # Enough to compute the diff for the current 'push'
- name: Check for Version Change
id: version
run: |
if [ -n "$(git cat-file -p HEAD | grep ^parent | cut -d' ' -f2 | xargs git diff | grep '^+version =')" ]; then
echo "version_changed=true" >> "$GITHUB_OUTPUT"
else
echo "version_changed=false" >> "$GITHUB_OUTPUT"
fi
- name: Set up Python
if: steps.version.outputs.version_changed == 'true'
uses: actions/setup-python@v4
with:
python-version: "3.x"
- name: Install pypa/build
if: steps.version.outputs.version_changed == 'true'
run: >-
python3 -m
pip install
build
--user
- name: Build a binary wheel and a source tarball
if: steps.version.outputs.version_changed == 'true'
run: >-
cat pyproject.toml ;
python3 -m
build
--sdist
--wheel
--outdir dist/
.
- name: Publish distribution πŸ“¦ to Test PyPI
if: steps.version.outputs.version_changed == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository-url: https://test.pypi.org/legacy/
- name: Publish distribution πŸ“¦ to PyPI
if: steps.version.outputs.version_changed == 'true'
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_API_TOKEN }}