@@ -12,60 +12,66 @@ jobs:
12
12
- name : Check out repository
13
13
uses : actions/checkout@v3
14
14
with :
15
- fetch-depth : 0 # Fetch all history for checking version changes
15
+ fetch-depth : 0
16
16
17
17
- name : Set up Python
18
18
uses : actions/setup-python@v4
19
19
with :
20
- python-version : ' 3.11 '
20
+ python-version : ' 3.x '
21
21
22
22
- name : Install build dependencies
23
23
run : |
24
24
python -m pip install --upgrade pip
25
- pip install build twine packaging
25
+ pip install build twine packaging requests
26
26
27
27
- name : Build package
28
28
run : python -m build
29
29
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
33
33
run : |
34
- # Try to get version from pyproject.toml first
35
34
if [ -f "pyproject.toml" ]; then
36
35
VERSION=$(grep -Po "(?<=version = \")[^\"]*" pyproject.toml)
37
- # Fallback to setup.py
36
+ PACKAGE_NAME=$(grep -Po "(?<=name = \")[^\"]*" pyproject.toml)
38
37
elif [ -f "setup.py" ]; then
39
38
VERSION=$(python setup.py --version)
39
+ PACKAGE_NAME=$(python setup.py --name)
40
40
else
41
41
echo "No version file found"
42
42
exit 1
43
43
fi
44
44
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
45
+ echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
45
46
46
- # Check if version exists on PyPI
47
+ # Check PyPI version using API
47
48
- name : Check if version exists on PyPI
48
49
id : check_version
49
50
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 }}"
59
53
60
- VERSION="${{ steps.current_version.outputs. version }} "
54
+ echo "Checking version $VERSION for package $PACKAGE_NAME "
61
55
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"
68
71
echo "should_publish=true" >> "$GITHUB_OUTPUT"
72
+ else
73
+ echo "Error checking PyPI API: HTTP status $HTTP_STATUS"
74
+ exit 1
69
75
fi
70
76
71
77
- name : Publish to PyPI
79
85
- name : Log skip publication
80
86
if : steps.check_version.outputs.should_publish != 'true'
81
87
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