From fca510c7e978f4d877331c9e828bc5b71358c488 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 12:04:16 +0100 Subject: [PATCH 1/6] ci: publish to PyPI with Trusted Publishing The release job exchanges its OIDC token for a short-lived PyPI token instead of uploading with the long-lived PYPI_TOKEN secret. --- .github/workflows/release.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a991e46..0109511 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,6 +9,7 @@ on: permissions: contents: write pull-requests: read + id-token: write jobs: Release: @@ -35,14 +36,13 @@ jobs: with: python-version: "3.10" - - name: Publish to PyPi - env: - TWINE_USERNAME: "__token__" - TWINE_PASSWORD: "${{ secrets.PYPI_TOKEN }}" + - name: Build run: | - pip install -q twine==3.7.1 wheel==0.37.1 + pip install -q wheel==0.37.1 python setup.py sdist bdist_wheel - twine upload --non-interactive dist/* + + - name: Publish to PyPI (Trusted Publishing) + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 - name: Create release on GitHub uses: ncipollo/release-action@v1 From bbd7802e63a8b580366049becd7da437be027948 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 13:30:43 +0100 Subject: [PATCH 2/6] ci: release on push to master and publish from a locked environment Building runs in its own job, so only the publish job can mint a PyPI token. Releasing on push to master lets the pypi environment allow master only, which a branch run cannot reach. --- .github/workflows/release.yml | 83 +++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 18 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0109511..2bdd9d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,36 +1,55 @@ name: Release on: - pull_request: - types: [closed] + push: branches: - master -permissions: - contents: write - pull-requests: read - id-token: write +permissions: {} jobs: - Release: - name: 🚀 Release - if: github.event.pull_request.merged && startsWith(github.head_ref, 'release-') + # A merged release PR bumps __version__, so a version without its tag is a new release. + check: + name: 🔎 Check for a new version runs-on: ubuntu-latest + permissions: + contents: read + outputs: + release: ${{ steps.version.outputs.release }} + version: ${{ steps.version.outputs.version }} steps: - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: actions/github-script@v5 + - id: version + run: | + version="v$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' stream_chat/__pkg__.py)" + echo "version=$version" >> "$GITHUB_OUTPUT" + if git rev-parse -q --verify "refs/tags/$version" >/dev/null; then + echo "release=false" >> "$GITHUB_OUTPUT" + else + echo "release=true" >> "$GITHUB_OUTPUT" + fi + + build: + name: 🏗️ Build + needs: check + if: needs.check.outputs.release == 'true' + runs-on: ubuntu-latest + permissions: + contents: read + outputs: + changelog: ${{ steps.changelog.outputs.changelog }} + steps: + - uses: actions/checkout@v3 + + - id: changelog + uses: actions/github-script@v5 with: script: | const get_change_log_diff = require('./scripts/get_changelog_diff.js') - core.exportVariable('CHANGELOG', get_change_log_diff()) - - // Getting the release version from the PR source branch - // Source branch looks like this: release-1.0.0 - const version = context.payload.pull_request.head.ref.split('-')[1] - core.exportVariable('VERSION', version) + core.setOutput('changelog', get_change_log_diff()) - uses: actions/setup-python@v3 with: @@ -41,12 +60,40 @@ jobs: pip install -q wheel==0.37.1 python setup.py sdist bdist_wheel + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist + path: dist/ + + # The only job that can mint a PyPI token, so it runs no project code. The PyPI trusted + # publisher requires the pypi environment, which only master can deploy to. + publish: + name: 📦 Publish to PyPI + needs: build + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: dist + path: dist/ + - name: Publish to PyPI (Trusted Publishing) uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 + release: + name: 🚀 Release + needs: [check, build, publish] + runs-on: ubuntu-latest + permissions: + contents: write + steps: - name: Create release on GitHub uses: ncipollo/release-action@v1 with: - body: ${{ env.CHANGELOG }} - tag: ${{ env.VERSION }} + body: ${{ needs.build.outputs.changelog }} + tag: ${{ needs.check.outputs.version }} + commit: ${{ github.sha }} token: ${{ secrets.GITHUB_TOKEN }} From f7563bbcf1f1d5906284af40562fc8b5b1c3f891 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 13:47:17 +0100 Subject: [PATCH 3/6] ci: skip already uploaded files when publishing A release whose GitHub release step failed can then be finished by rerunning the workflow, instead of failing on the duplicate upload until the tag is created by hand. --- .github/workflows/release.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2bdd9d6..c1ae07c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -80,8 +80,11 @@ jobs: name: dist path: dist/ + # Skipping uploaded files lets a rerun finish a release whose GitHub release step failed. - name: Publish to PyPI (Trusted Publishing) uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 + with: + skip-existing: true release: name: 🚀 Release From fdfbf9731baa1654dea94ed95d50a9856a8af0f6 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 13:56:08 +0100 Subject: [PATCH 4/6] ci: tag the release commit before publishing to PyPI The tag marks the version as released, so a later push to master skips it instead of rebuilding different code under the same version. A failed upload is finished by rerunning the publish job, which reuses the built files. --- .github/workflows/release.yml | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c1ae07c..0687a39 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -65,11 +65,27 @@ jobs: name: dist path: dist/ + # Tags the release commit before the upload, so a later push to master never rebuilds this version. + release: + name: 🚀 Release + needs: [check, build] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Create release on GitHub + uses: ncipollo/release-action@v1 + with: + body: ${{ needs.build.outputs.changelog }} + tag: ${{ needs.check.outputs.version }} + commit: ${{ github.sha }} + token: ${{ secrets.GITHUB_TOKEN }} + # The only job that can mint a PyPI token, so it runs no project code. The PyPI trusted # publisher requires the pypi environment, which only master can deploy to. publish: name: 📦 Publish to PyPI - needs: build + needs: [build, release] runs-on: ubuntu-latest environment: pypi permissions: @@ -80,23 +96,8 @@ jobs: name: dist path: dist/ - # Skipping uploaded files lets a rerun finish a release whose GitHub release step failed. + # Rerunning this job after a partial upload skips the files PyPI already has. - name: Publish to PyPI (Trusted Publishing) uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 with: skip-existing: true - - release: - name: 🚀 Release - needs: [check, build, publish] - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - name: Create release on GitHub - uses: ncipollo/release-action@v1 - with: - body: ${{ needs.build.outputs.changelog }} - tag: ${{ needs.check.outputs.version }} - commit: ${{ github.sha }} - token: ${{ secrets.GITHUB_TOKEN }} From b5e9354e6e9be8666139ae1fc27ac7d179bd3cd3 Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 14:05:56 +0100 Subject: [PATCH 5/6] ci: release until the version is on PyPI and rebuild the tagged commit Checking PyPI instead of the tag lets a full rerun or a later push finish a release whose upload failed. Once the version is tagged, the tagged commit is rebuilt and the existing GitHub release is kept, so the uploaded files always match the tag. --- .github/workflows/release.yml | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 0687a39..6845425 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,13 +8,15 @@ on: permissions: {} jobs: - # A merged release PR bumps __version__, so a version without its tag is a new release. + # A merged release PR bumps __version__, so a version missing on PyPI is a release to finish. + # Once tagged, the tagged commit is rebuilt, so a rerun or a later push publishes the same code. check: name: 🔎 Check for a new version runs-on: ubuntu-latest permissions: contents: read outputs: + ref: ${{ steps.version.outputs.ref }} release: ${{ steps.version.outputs.release }} version: ${{ steps.version.outputs.version }} steps: @@ -26,10 +28,11 @@ jobs: run: | version="v$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' stream_chat/__pkg__.py)" echo "version=$version" >> "$GITHUB_OUTPUT" - if git rev-parse -q --verify "refs/tags/$version" >/dev/null; then + if curl -sf --retry 3 -o /dev/null "https://pypi.org/pypi/stream-chat/${version#v}/json"; then echo "release=false" >> "$GITHUB_OUTPUT" else echo "release=true" >> "$GITHUB_OUTPUT" + echo "ref=$(git rev-parse -q --verify "refs/tags/$version^{commit}" || echo "$GITHUB_SHA")" >> "$GITHUB_OUTPUT" fi build: @@ -43,6 +46,8 @@ jobs: changelog: ${{ steps.changelog.outputs.changelog }} steps: - uses: actions/checkout@v3 + with: + ref: ${{ needs.check.outputs.ref }} - id: changelog uses: actions/github-script@v5 @@ -65,7 +70,7 @@ jobs: name: dist path: dist/ - # Tags the release commit before the upload, so a later push to master never rebuilds this version. + # Tags the release commit before the upload. A rerun keeps the existing release. release: name: 🚀 Release needs: [check, build] @@ -78,7 +83,8 @@ jobs: with: body: ${{ needs.build.outputs.changelog }} tag: ${{ needs.check.outputs.version }} - commit: ${{ github.sha }} + commit: ${{ needs.check.outputs.ref }} + skipIfReleaseExists: true token: ${{ secrets.GITHUB_TOKEN }} # The only job that can mint a PyPI token, so it runs no project code. The PyPI trusted @@ -96,7 +102,7 @@ jobs: name: dist path: dist/ - # Rerunning this job after a partial upload skips the files PyPI already has. + # A rerun after a partial upload skips the files PyPI already has. - name: Publish to PyPI (Trusted Publishing) uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 with: From 1b13dff730d838e6430b211bf61d3de40f6158fb Mon Sep 17 00:00:00 2001 From: Yun Wang Date: Tue, 17 Nov 2026 14:21:07 +0100 Subject: [PATCH 6/6] ci: run one release at a time and require both files on PyPI A release counts as done only when PyPI has the sdist and the wheel, so a partial upload is finished by the next run. Serializing the runs keeps overlapping pushes from building the same version from different commits. --- .github/workflows/release.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 6845425..59f26f2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,8 +7,13 @@ on: permissions: {} +# One release run at a time, so overlapping pushes cannot build the same version twice. +concurrency: + group: release + cancel-in-progress: false + jobs: - # A merged release PR bumps __version__, so a version missing on PyPI is a release to finish. + # A merged release PR bumps __version__, so a version without both files on PyPI is a release to finish. # Once tagged, the tagged commit is rebuilt, so a rerun or a later push publishes the same code. check: name: 🔎 Check for a new version @@ -28,7 +33,8 @@ jobs: run: | version="v$(sed -n 's/^__version__ = "\(.*\)"$/\1/p' stream_chat/__pkg__.py)" echo "version=$version" >> "$GITHUB_OUTPUT" - if curl -sf --retry 3 -o /dev/null "https://pypi.org/pypi/stream-chat/${version#v}/json"; then + if curl -sf --retry 3 "https://pypi.org/pypi/stream-chat/${version#v}/json" | + jq -e '[.urls[].packagetype] | index("sdist") and index("bdist_wheel")' >/dev/null; then echo "release=false" >> "$GITHUB_OUTPUT" else echo "release=true" >> "$GITHUB_OUTPUT"