Skip to content

Publish Package to PyPI #18

Publish Package to PyPI

Publish Package to PyPI #18

Workflow file for this run

name: Build & Publish to PyPI
run-name: Publish Package to PyPI
on:
push:
branches:
- release
concurrency:
group: pypi-publish
cancel-in-progress: true
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Ruff
run: pip install ruff
- name: Run Ruff
run: ruff check . && ruff format --check .
build-and-publish:
needs: lint
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
environment: pypi
steps:
- name: Checkout code
uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 - --version 1.8.3
poetry install --no-interaction --no-root
echo "$HOME/.local/bin" >> $GITHUB_PATH
- name: Check if version exists
id: check_version
run: |
# 1. Get metadata from pyproject.toml
PACKAGE_NAME=$(poetry version | awk '{print $1}')
PACKAGE_VERSION=$(poetry version --short)
echo "pkg_name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
echo "pkg_ver=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
# 2. Check the SPECIFIC version JSON endpoint
# We use the JSON API because it's reliable for bots
URL="https://pypi.org/pypi/$PACKAGE_NAME/$PACKAGE_VERSION/json"
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
if [ "$STATUS" == "200" ]; then
echo "should_publish=false" >> $GITHUB_OUTPUT
echo "### ⏭️ Skip: Version $PACKAGE_VERSION already exists" >> $GITHUB_STEP_SUMMARY
else
echo "should_publish=true" >> $GITHUB_OUTPUT
echo "### 🚀 New version $PACKAGE_VERSION detected" >> $GITHUB_STEP_SUMMARY
fi
- name: Debug OIDC
run: |
echo "ACTIONS_ID_TOKEN_REQUEST_URL=$ACTIONS_ID_TOKEN_REQUEST_URL"
echo "ACTIONS_ID_TOKEN_REQUEST_TOKEN=$ACTIONS_ID_TOKEN_REQUEST_TOKEN"
- name: Configure and Publish
if: steps.check_version.outputs.should_publish == 'true'
run: |
# poetry config repositories.${{ env.REPO_ALIAS }} ${{ env.PYPI_URL }}
# poetry config pypi-token.${{ env.REPO_ALIAS }} ${{ secrets.PYPI_TOKEN }}
poetry publish --build
# Add Success Summary
NAME=${{ steps.check_version.outputs.pkg_name }}
VER=${{ steps.check_version.outputs.pkg_ver }}
echo "### ✅ Success: Published $NAME v$VER" >> $GITHUB_STEP_SUMMARY
echo "View your package on PyPI: [rz-sample](https://pypi.org/project/$NAME/)" >> $GITHUB_STEP_SUMMARY