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
name: Publish Python 🐍 distributions 📦 to PyPI | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
release: | |
runs-on: ubuntu-latest | |
if: "contains(github.event.head_commit.message, 'deploy')" | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Install dependencies | |
run: | | |
pip install bumpversion setuptools wheel twine | |
- name: Generate Git Tag | |
id: generate_tag | |
run: | | |
set -e # Exit immediately if a command exits with a non-zero status | |
VERSION_PREFIX="v" | |
VERSION_MAJOR_MINOR="1.0" | |
# Fetch all tags from the remote repository | |
git fetch --tags | |
# Get the latest patch version for the given major.minor version | |
VERSION_PATCH=$(git tag --list "${VERSION_PREFIX}${VERSION_MAJOR_MINOR}.*" --sort=-version:refname | head -n 1 | grep -oE '[0-9]+$') | |
if [ $? -ne 0 ]; then | |
echo "Failed to retrieve the latest patch version." | |
VERSION_PATCH=0 | |
fi | |
# Increment the patch version or start at 0 if none exists | |
if [ -z "$VERSION_PATCH" ]; then | |
VERSION_PATCH=0 | |
else | |
VERSION_PATCH=$((VERSION_PATCH + 1)) | |
fi | |
PYPI_TAG="${VERSION_MAJOR_MINOR}.${VERSION_PATCH}" | |
NEW_TAG="${VERSION_PREFIX}${PYPI_TAG}" | |
echo "Generated new tag: $NEW_TAG" | |
echo "Generated new PYPI tag: $PYPI_TAG" | |
# Set environment variables for use in later steps | |
echo "NEW_TAG=$NEW_TAG" >> $GITHUB_ENV | |
echo "PYPI_TAG=$PYPI_TAG" >> $GITHUB_ENV | |
# Replace VERSION_PLACEHOLDER in setup.py with the new version | |
sed -i "s/{{VERSION_PLACEHOLDER}}/${PYPI_TAG}/g" setup.py | |
- name: Build package | |
run: python setup.py sdist bdist_wheel | |
- name: Publish distribution 📦 to PyPI | |
uses: pypa/gh-action-pypi-publish@master | |
with: | |
password: ${{ secrets.PYPI_API_TOKEN }} | |
- name: Push Git Tag | |
run: | | |
git config user.name "GitHub Actions" | |
git config user.email "github-actions@users.noreply.github.com" | |
git tag $NEW_TAG | |
git push origin $NEW_TAG |