name: Publish to PyPI
on:
release:
types: [created]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch full history for setuptools_scm
ref: ${{ github.event.release.tag_name }} # Explicitly check out the tagged commit
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install uv
run: |
curl -sSfL https://astral.sh/uv/install.sh | sh -s --
echo "$HOME/.cargo/bin" >> $GITHUB_PATH
- name: Create virtual environment
run: |
python -m venv .venv
source .venv/bin/activate
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install uv
uv pip install --system build twine setuptools_scm
- name: Debug Git information
run: |
echo "Current commit: $(git rev-parse HEAD)"
echo "Tags at current commit: $(git tag --points-at HEAD)"
echo "All tags: $(git tag -l)"
echo "Git status: $(git status)"
- name: Generate dynamic version
run: |
uv pip install setuptools_scm
# Get version from setuptools_scm
scm_version=$(python -c "from setuptools_scm import get_version; print(get_version())")
echo "SCM detected version: $scm_version"
# Extract version from tag if this is a release event
if [[ "${{ github.event_name }}" == "release" && -n "${{ github.event.release.tag_name }}" ]]; then
# Remove 'v' prefix if present
tag_version="${{ github.event.release.tag_name }}"
tag_version="${tag_version#v}"
echo "Using tag-based version: $tag_version"
echo "VERSION=$tag_version" >> $GITHUB_ENV
else
# Use setuptools_scm version if not a tagged release
echo "Using SCM-based version: $scm_version"
echo "VERSION=$scm_version" >> $GITHUB_ENV
fi
- name: Check if version exists on PyPI
run: |
VERSION=${{ env.VERSION }}
if curl -sSfL "https://pypi.org/pypi/duckduckgo-mcp/json" | \
python3 -c "import sys, json; d=json.load(sys.stdin); print(any(d['releases'].get('$VERSION', [])))" | grep -q "True"; then
echo "Version $VERSION already exists on PyPI with files. Skipping upload."
exit 1
fi
- name: Prepare build with correct version
run: |
# Create an environment variable with version override if needed
if [[ "${{ github.event_name }}" == "release" ]]; then
# For release events, we'll override setuptools_scm with the exact tag version
export SETUPTOOLS_SCM_PRETEND_VERSION="${VERSION}"
echo "Setting SETUPTOOLS_SCM_PRETEND_VERSION=${VERSION}"
fi
# Store this in the environment for subsequent steps
echo "SETUPTOOLS_SCM_PRETEND_VERSION=${SETUPTOOLS_SCM_PRETEND_VERSION}" >> $GITHUB_ENV
- name: Build package
env:
SETUPTOOLS_SCM_PRETEND_VERSION: ${{ env.SETUPTOOLS_SCM_PRETEND_VERSION }}
run: |
echo "Building with version override: $SETUPTOOLS_SCM_PRETEND_VERSION"
python -m build
- name: Publish to PyPI
if: github.event_name == 'release' && startsWith(github.event.release.tag_name, 'v')
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: distributions
path: dist/