Skip to content

Commit c01e9ec

Browse files
authored
Merge pull request #286 from plugwise/tp
Migrate to Trusted Publishing (pypi)
2 parents 039af0e + 2dc1164 commit c01e9ec

File tree

3 files changed

+77
-42
lines changed

3 files changed

+77
-42
lines changed

.github/workflows/merge.yml

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,52 @@ on:
1313
types: closed
1414
branches:
1515
- main
16-
- async
1716

1817
jobs:
1918
publishing:
2019
name: Build and publish Python 🐍 distributions 📦 to PyPI
2120
runs-on: ubuntu-latest
21+
environment: pypi
22+
permissions:
23+
contents: read # Required by actions/checkout
24+
id-token: write # Needed for OIDC-based Trusted Publishing
2225
# Only trigger on merges, not just closes
2326
if: github.event.pull_request.merged == true
2427
steps:
2528
- name: Check out committed code
2629
uses: actions/checkout@v4
27-
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
28-
id: python
29-
uses: actions/setup-python@v5
30-
with:
31-
python-version: ${{ env.DEFAULT_PYTHON }}
32-
- name: Install pypa/build
33-
run: >-
34-
python3 -m
35-
pip install
36-
build
37-
--user
38-
- name: Build a binary wheel and a source tarball
39-
run: python3 -m build
30+
- name: Prepare uv
31+
run: |
32+
pip install uv
33+
uv venv --seed venv
34+
. venv/bin/activate
35+
uv pip install toml
36+
- name: Check for existing package on PyPI
37+
id: check_package
38+
run: |
39+
. venv/bin/activate
40+
PACKAGE_VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
41+
PACKAGE_NAME=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['name'])")
42+
43+
# Use jq to check for the version in the releases object
44+
EXISTING_VERSIONS=$(curl -s "https://pypi.org/pypi/$PACKAGE_NAME/json" | jq '.releases | keys[]')
45+
46+
echo "Checking for package: $PACKAGE_NAME==$PACKAGE_VERSION"
47+
48+
if [[ "$EXISTING_VERSIONS" =~ "$PACKAGE_VERSION" ]]; then
49+
echo "Package version already exists. Skipping upload."
50+
echo "should_publish=false" >> $GITHUB_OUTPUT
51+
else
52+
echo "Package version does not exist. Proceeding with upload."
53+
echo "should_publish=true" >> $GITHUB_OUTPUT
54+
fi
55+
- name: Build
56+
if: steps.check_package.outputs.should_publish == 'true'
57+
run: |
58+
. venv/bin/activate
59+
uv build
4060
- name: Publish distribution 📦 to PyPI
41-
uses: pypa/gh-action-pypi-publish@release/v1
42-
with:
43-
password: ${{ secrets.pypi_token }}
44-
skip_existing: true
61+
if: steps.check_package.outputs.should_publish == 'true'
62+
run: |
63+
. venv/bin/activate
64+
uv publish

.github/workflows/verify.yml

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Ongoing / v0.44.8a0
4+
5+
- Chores move module publishing on (test)pypi to Trusted Publishing (and using uv) - released as alpha 0.44.8a0 to demonstrate functionality
6+
37
## v0.44.7 - 2025-07-08
48

59
- PR [282](https://github.com/plugwise/python-plugwise-usb/pull/282): Finalize switch implementation

0 commit comments

Comments
 (0)