Skip to content

Commit d5be6e0

Browse files
authored
Create python-publish.yml
1 parent 718fd78 commit d5be6e0

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

.github/workflows/python-publish.yml

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Publish Python Package to PyPI
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build-and-publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Check out repository
13+
uses: actions/checkout@v3
14+
with:
15+
fetch-depth: 0 # Fetch all history for checking version changes
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.11'
21+
22+
- name: Install build dependencies
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install build twine packaging
26+
27+
- name: Build package
28+
run: python -m build
29+
30+
# Add step to extract current version
31+
- name: Get current version
32+
id: current_version
33+
run: |
34+
# Try to get version from pyproject.toml first
35+
if [ -f "pyproject.toml" ]; then
36+
VERSION=$(grep -Po "(?<=version = \")[^\"]*" pyproject.toml)
37+
# Fallback to setup.py
38+
elif [ -f "setup.py" ]; then
39+
VERSION=$(python setup.py --version)
40+
else
41+
echo "No version file found"
42+
exit 1
43+
fi
44+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
45+
46+
# Check if version exists on PyPI
47+
- name: Check if version exists on PyPI
48+
id: check_version
49+
run: |
50+
# Get package name from setup.py or pyproject.toml
51+
if [ -f "pyproject.toml" ]; then
52+
PACKAGE_NAME=$(grep -Po "(?<=name = \")[^\"]*" pyproject.toml)
53+
elif [ -f "setup.py" ]; then
54+
PACKAGE_NAME=$(python setup.py --name)
55+
else
56+
echo "No package configuration found"
57+
exit 1
58+
fi
59+
60+
VERSION="${{ steps.current_version.outputs.version }}"
61+
62+
# Check if version already exists on PyPI
63+
if pip index versions "$PACKAGE_NAME" 2>/dev/null | grep -q "^$VERSION$"; then
64+
echo "Version $VERSION already exists on PyPI"
65+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
66+
else
67+
echo "Version $VERSION is new"
68+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
69+
fi
70+
71+
- name: Publish to PyPI
72+
if: steps.check_version.outputs.should_publish == 'true'
73+
env:
74+
TWINE_USERNAME: __token__
75+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
76+
run: |
77+
python -m twine upload dist/*
78+
79+
- name: Log skip publication
80+
if: steps.check_version.outputs.should_publish != 'true'
81+
run: |
82+
echo "Skipping publication to PyPI as version ${{ steps.current_version.outputs.version }} already exists"

0 commit comments

Comments
 (0)