88 # paths-ignore:
99 # - 'llm_dialog_manager/__init__.py'
1010 # - 'pyproject.toml'
11- # TODO: fix version issue
11+ # TODO: fix version issue
1212
13- # Add these permission settings
1413permissions :
15- contents : write # This allows pushing to the repository
16- pull-requests : write # This allows creating PRs if needed
17- issues : write # This allows creating issues if needed
14+ contents : write # Allows pushing to the repository
15+ pull-requests : write # Allows creating PRs if needed
16+ issues : write # Allows creating issues if needed
1817
1918jobs :
2019 publish :
2322 steps :
2423 - uses : actions/checkout@v4
2524 with :
26- fetch-depth : 0
25+ fetch-depth : 0 # Ensure all history and tags are fetched
2726
2827 - name : Set up Python
2928 uses : actions/setup-python@v4
3837 - name : Get version from pyproject.toml
3938 id : get_version
4039 run : |
41- # First verify the file exists and extract version with error handling
4240 CURRENT_VERSION=$(python -c '
4341 import toml
4442 import sys
@@ -53,77 +51,93 @@ jobs:
5351 sys.exit(1)
5452 ')
5553
56- # Store the version in GitHub Actions output
5754 echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT
5855
59- # Split version into major, minor, patch components
6056 IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
6157 echo "major_version=${major}" >> $GITHUB_OUTPUT
6258 echo "minor_version=${minor}" >> $GITHUB_OUTPUT
63-
64- - name : Update version
59+
60+ - name : Check if version exists on PyPI
61+ id : check_pypi
6562 run : |
66- set -e # Exit immediately if a command exits with a non-zero status
63+ PACKAGE_NAME=$(python -c "
64+ import toml
65+ config = toml.load('pyproject.toml')
66+ print(config['project']['name'])
67+ ")
68+ VERSION="${{ steps.get_version.outputs.current_version }}"
69+ PYPI_URL="https://pypi.org/pypi/${PACKAGE_NAME}/${VERSION}/json"
6770
68- # Get major and minor version from previous step
69- MAJOR_VERSION="${{ steps.get_version.outputs.major_version }}"
70- MINOR_VERSION="${{ steps.get_version.outputs.minor_version }}"
71+ echo "Checking PyPI for ${PACKAGE_NAME} version ${VERSION}..."
7172
72- # Find the latest tag matching the current major.minor version
73- VERSION_PREFIX="v${MAJOR_VERSION}.${MINOR_VERSION}."
74- LAST_VERSION_TAG=$(git tag --list "${VERSION_PREFIX}*" --sort=-v:refname | head -n 1)
73+ RESPONSE_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$PYPI_URL")
7574
76- if [ -z "$LAST_VERSION_TAG" ]; then
77- # No tag exists for this major.minor version, start from 1
78- COMMIT_COUNT=1
75+ if [ "$RESPONSE_CODE" -eq 200 ]; then
76+ echo "Version ${VERSION} already exists on PyPI."
77+ NEW_VERSION="${VERSION}-dev"
78+ echo "new_version=${NEW_VERSION}" >> $GITHUB_OUTPUT
7979 else
80- # Count commits since the last version tag
81- COMMIT_COUNT=$(git rev-list ${LAST_VERSION_TAG}..HEAD --count)
82- # Increment commit count to start from the next patch version
83- COMMIT_COUNT=$((COMMIT_COUNT + 1))
80+ echo "Version ${VERSION} is available on PyPI."
81+ echo "new_version=${VERSION}" >> $GITHUB_OUTPUT
8482 fi
83+
84+ - name : Debug - Version Check
85+ run : |
86+ echo "New Version: ${{ steps.check_pypi.outputs.new_version }}"
87+
88+ - name : Update version
89+ run : |
90+ set -e # Exit immediately if a command exits with a non-zero status
8591
86- # Create the new version
87- NEW_VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${COMMIT_COUNT}"
88- echo "New version will be: ${NEW_VERSION}"
92+ # Use the new_version from the PyPI check
93+ NEW_VERSION="${{ steps.check_pypi.outputs.new_version }}"
94+ echo "Using new version: ${NEW_VERSION}"
95+
96+ # Split version into major, minor, patch components
97+ IFS='.-' read -r major minor patch <<< "$NEW_VERSION"
98+ echo "Major: $major, Minor: $minor, Patch: $patch"
8999
90100 # Update version in __init__.py
91101 sed -i "s/__version__ = \".*\"/__version__ = \"${NEW_VERSION}\"/" llm_dialog_manager/__init__.py
92102
93103 # Update version in pyproject.toml
94104 python -c "
95105 import toml
96-
97- # Read the current content
98106 with open('pyproject.toml', 'r') as f:
99107 data = toml.load(f)
100-
101- # Update the version in the project section
102108 data['project']['version'] = '${NEW_VERSION}'
103-
104- # Write back to the file
105109 with open('pyproject.toml', 'w') as f:
106110 toml.dump(data, f)
107111 "
108112
109- # Commit the version update
110- git config --local user.email "github-actions[bot]@users.noreply.github.com"
111- git config --local user.name "github-actions[bot]"
112- git add llm_dialog_manager/__init__.py pyproject.toml
113- git commit -m "Bump version to ${NEW_VERSION} [skip ci]"
114-
115- # Tag the new version
116- git tag "v${NEW_VERSION}"
113+ # Commit the version update if it's a dev version
114+ if [[ "$NEW_VERSION" == *"-dev" ]]; then
115+ git config --local user.email "github-actions[bot]@users.noreply.github.com"
116+ git config --local user.name "github-actions[bot]"
117+ git add llm_dialog_manager/__init__.py pyproject.toml
118+ git commit -m "Bump version to ${NEW_VERSION} [skip ci]"
119+ fi
117120
118- # Push changes and tags
119- git push origin main
120- git push origin "v${NEW_VERSION}"
121+ # Tag the new version if it's not a dev version
122+ if [[ "$NEW_VERSION" != *"-dev" ]]; then
123+ git tag "v${NEW_VERSION}"
124+ git push origin main
125+ git push origin "v${NEW_VERSION}"
126+ fi
121127
122128 - name : Build package
123129 run : python -m build
124130
125131 - name : Publish to PyPI
132+ if : ${{ steps.check_pypi.outputs.new_version != format('{0}-dev', steps.get_version.outputs.current_version) }}
126133 env :
127134 TWINE_USERNAME : __token__
128135 TWINE_PASSWORD : ${{ secrets.PYPI_API_TOKEN }}
129136 run : twine upload dist/*
137+
138+ - name : Publish to Test PyPI
139+ if : ${{ steps.check_pypi.outputs.new_version == format('{0}-dev', steps.get_version.outputs.current_version) }}
140+ env :
141+ TWINE_USERNAME : __token__
142+ TWINE_PASSWORD : ${{ secrets.TEST_PYPI_API_TOKEN }}
143+ run : twine upload --repository-url https://test.pypi.org/legacy/ dist/*
0 commit comments