@@ -273,6 +273,9 @@ jobs:
273273 test-publishing :
274274 name : Build and publish Python 🐍 distributions 📦 to TestPyPI
275275 runs-on : ubuntu-latest
276+ environment : testpypi
277+ permissions :
278+ id-token : write
276279 needs :
277280 - cache
278281 - prepare
@@ -281,34 +284,41 @@ jobs:
281284 steps :
282285 - name : Check out committed code
283286 uses : actions/checkout@v4
284- - name : Set up Python ${{ env.DEFAULT_PYTHON }}
285- id : python
286- uses : actions/setup-python@v5
287- with :
288- python-version : ${{ env.DEFAULT_PYTHON }}
289- - name : Create or reuse cache
290- id : cache-reuse
291- uses : ./.github/actions/restore-venv
292- with :
293- cache-key : ${{ needs.cache.outputs.cache-key }}
294- python-version : ${{ steps.python.outputs.python-version }}
295- venv-dir : ${{ env.VENV }}
296- precommit-home : ${{ env.PRE_COMMIT_HOME }}
297- - name : Install pypa/build
287+ - name : Prepare uv
298288 run : |
289+ pip install uv
290+ uv venv --seed venv
299291 . venv/bin/activate
300- uv pip install build
301- - name : Build a binary wheel and a source tarball
292+ uv pip install toml
293+ - name : Check for existing package on TestPyPI
294+ id : check_package
302295 run : |
303296 . venv/bin/activate
304- python3 -m build
305- - name : Publish distribution 📦 to Test PyPI
306- uses : pypa/gh-action-pypi-publish@release/v1
307- continue-on-error : true
308- with :
309- password : ${{ secrets.testpypi_token }}
310- repository_url : https://test.pypi.org/legacy/
311- skip_existing : true
297+ PACKAGE_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
298+ PACKAGE_NAME=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])")
299+
300+ # Use jq to check for the version in the releases object
301+ EXISTING_VERSIONS=$(curl -s "https://test.pypi.org/pypi/$PACKAGE_NAME/json" | jq '.releases | keys[]')
302+
303+ echo "Checking for package: $PACKAGE_NAME==$PACKAGE_VERSION"
304+
305+ if [[ "$EXISTING_VERSIONS" =~ "$PACKAGE_VERSION" ]]; then
306+ echo "Package version already exists. Skipping upload."
307+ echo "should_publish=false" >> $GITHUB_OUTPUT
308+ else
309+ echo "Package version does not exist. Proceeding with upload."
310+ echo "should_publish=true" >> $GITHUB_OUTPUT
311+ fi
312+ - name : Build
313+ if : steps.check_package.outputs.should_publish == 'true'
314+ run : |
315+ . venv/bin/activate
316+ uv build
317+ - name : Publish distribution 📦 to TestPyPI
318+ if : steps.check_package.outputs.should_publish == 'true'
319+ run : |
320+ . venv/bin/activate
321+ uv publish --publish-url https://test.pypi.org/legacy/
312322
313323 complexity :
314324 name : Process test complexity
0 commit comments