Skip to content

Commit 08302b6

Browse files
authored
Update python-publish.yml
1 parent 28de07a commit 08302b6

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

.github/workflows/python-publish.yml

+32-26
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,66 @@ jobs:
1212
- name: Check out repository
1313
uses: actions/checkout@v3
1414
with:
15-
fetch-depth: 0 # Fetch all history for checking version changes
15+
fetch-depth: 0
1616

1717
- name: Set up Python
1818
uses: actions/setup-python@v4
1919
with:
20-
python-version: '3.11'
20+
python-version: '3.x'
2121

2222
- name: Install build dependencies
2323
run: |
2424
python -m pip install --upgrade pip
25-
pip install build twine packaging
25+
pip install build twine packaging requests
2626
2727
- name: Build package
2828
run: python -m build
2929

30-
# Add step to extract current version
31-
- name: Get current version
32-
id: current_version
30+
# Get current version and package name
31+
- name: Get package info
32+
id: package_info
3333
run: |
34-
# Try to get version from pyproject.toml first
3534
if [ -f "pyproject.toml" ]; then
3635
VERSION=$(grep -Po "(?<=version = \")[^\"]*" pyproject.toml)
37-
# Fallback to setup.py
36+
PACKAGE_NAME=$(grep -Po "(?<=name = \")[^\"]*" pyproject.toml)
3837
elif [ -f "setup.py" ]; then
3938
VERSION=$(python setup.py --version)
39+
PACKAGE_NAME=$(python setup.py --name)
4040
else
4141
echo "No version file found"
4242
exit 1
4343
fi
4444
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
45+
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
4546
46-
# Check if version exists on PyPI
47+
# Check PyPI version using API
4748
- name: Check if version exists on PyPI
4849
id: check_version
4950
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
51+
PACKAGE_NAME="${{ steps.package_info.outputs.package_name }}"
52+
VERSION="${{ steps.package_info.outputs.version }}"
5953
60-
VERSION="${{ steps.current_version.outputs.version }}"
54+
echo "Checking version $VERSION for package $PACKAGE_NAME"
6155
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"
56+
# Use PyPI JSON API to check version
57+
HTTP_STATUS=$(curl -s -o response.json -w "%{http_code}" "https://pypi.org/pypi/$PACKAGE_NAME/json")
58+
59+
if [ "$HTTP_STATUS" -eq 200 ]; then
60+
# Check if version exists in releases
61+
if cat response.json | python -c "import sys, json; data = json.load(sys.stdin); sys.exit(0 if '$VERSION' in data['releases'] else 1)"; then
62+
echo "Version $VERSION already exists on PyPI"
63+
echo "should_publish=false" >> "$GITHUB_OUTPUT"
64+
else
65+
echo "Version $VERSION is new"
66+
echo "should_publish=true" >> "$GITHUB_OUTPUT"
67+
fi
68+
elif [ "$HTTP_STATUS" -eq 404 ]; then
69+
# Package doesn't exist on PyPI yet
70+
echo "Package not found on PyPI - this is a new package"
6871
echo "should_publish=true" >> "$GITHUB_OUTPUT"
72+
else
73+
echo "Error checking PyPI API: HTTP status $HTTP_STATUS"
74+
exit 1
6975
fi
7076
7177
- name: Publish to PyPI
@@ -79,4 +85,4 @@ jobs:
7985
- name: Log skip publication
8086
if: steps.check_version.outputs.should_publish != 'true'
8187
run: |
82-
echo "Skipping publication to PyPI as version ${{ steps.current_version.outputs.version }} already exists"
88+
echo "Skipping publication to PyPI as version ${{ steps.package_info.outputs.version }} already exists"

0 commit comments

Comments
 (0)