@@ -273,6 +273,10 @@ jobs:
273273 test-publishing :
274274 name : Build and publish Python 🐍 distributions 📦 to TestPyPI
275275 runs-on : ubuntu-latest
276+ environment : testpypi
277+ permissions :
278+ contents : read # Required by actions/checkout
279+ id-token : write # Needed for OIDC-based Trusted Publishing
276280 needs :
277281 - cache
278282 - prepare
@@ -281,34 +285,41 @@ jobs:
281285 steps :
282286 - name : Check out committed code
283287 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
288+ - name : Prepare uv
298289 run : |
290+ pip install uv
291+ uv venv --seed venv
299292 . venv/bin/activate
300- uv pip install build
301- - name : Build a binary wheel and a source tarball
293+ uv pip install toml
294+ - name : Check for existing package on TestPyPI
295+ id : check_package
302296 run : |
303297 . 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
298+ PACKAGE_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
299+ PACKAGE_NAME=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])")
300+
301+ # Use jq to check for the version in the releases object
302+ EXISTING_VERSIONS=$(curl -s "https://test.pypi.org/pypi/$PACKAGE_NAME/json" | jq '.releases | keys[]')
303+
304+ echo "Checking for package: $PACKAGE_NAME==$PACKAGE_VERSION"
305+
306+ if [[ "$EXISTING_VERSIONS" =~ "$PACKAGE_VERSION" ]]; then
307+ echo "Package version already exists. Skipping upload."
308+ echo "should_publish=false" >> $GITHUB_OUTPUT
309+ else
310+ echo "Package version does not exist. Proceeding with upload."
311+ echo "should_publish=true" >> $GITHUB_OUTPUT
312+ fi
313+ - name : Build
314+ if : steps.check_package.outputs.should_publish == 'true'
315+ run : |
316+ . venv/bin/activate
317+ uv build
318+ - name : Publish distribution 📦 to TestPyPI
319+ if : steps.check_package.outputs.should_publish == 'true'
320+ run : |
321+ . venv/bin/activate
322+ uv publish --publish-url https://test.pypi.org/legacy/
312323
313324 complexity :
314325 name : Process test complexity
0 commit comments