Skip to content

Publish to PyPI

Publish to PyPI #2497

Workflow file for this run

name: Publish to PyPI
on:
workflow_dispatch:
push:
branches:
- main
paths-ignore:
- 'llm_dialog_manager/__init__.py'
- 'pyproject.toml'
jobs:
publish:
if: "!contains(github.event.head_commit.message, 'Bump version')"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GH_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build twine setuptools wheel
- name: Get version from pyproject.toml
id: get_version
run: |
CURRENT_VERSION=$(grep '^version = ' pyproject.toml | sed 's/version = "\(.*\)"/\1/')
echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT
# Split version into parts
IFS='.' read -r major minor _ <<< "$CURRENT_VERSION"
echo "major_version=${major}" >> $GITHUB_OUTPUT
echo "minor_version=${minor}" >> $GITHUB_OUTPUT
- name: Update version
run: |
# Get major and minor version from previous step
MAJOR_VERSION="${{ steps.get_version.outputs.major_version }}"
MINOR_VERSION="${{ steps.get_version.outputs.minor_version }}"
# Find the latest tag matching the current major.minor version
VERSION_PREFIX="v${MAJOR_VERSION}.${MINOR_VERSION}."
LAST_VERSION_TAG=$(git tag --list "${VERSION_PREFIX}*" --sort=-v:refname | head -n 1)
if [ -z "$LAST_VERSION_TAG" ]; then
# No tag exists for this major.minor version, start from 0
COMMIT_COUNT=0
else
# Count commits since the last version tag
COMMIT_COUNT=$(git rev-list ${LAST_VERSION_TAG}..HEAD --count)
# Increment commit count to start from the next patch version
COMMIT_COUNT=$((COMMIT_COUNT + 1))
fi
# Create the new version
NEW_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${COMMIT_COUNT}"
# Update version in __init__.py
sed -i "s/__version__ = \".*\"/__version__ = \"${NEW_VERSION}\"/" llm_dialog_manager/__init__.py
# Update version in pyproject.toml
sed -i "s/version = \".*\"/version = \"${NEW_VERSION}\"/" pyproject.toml
# Commit the version update
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add llm_dialog_manager/__init__.py pyproject.toml
git commit -m "Bump version to ${NEW_VERSION} [skip ci]"
git tag "v${NEW_VERSION}"
git push && git push --tags
- name: Build package
run: python -m build
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
run: twine upload dist/*