diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a991e46..59f26f2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,52 +1,115 @@ name: Release on: - pull_request: - types: [closed] + push: branches: - master -permissions: - contents: write - pull-requests: read +permissions: {} + +# One release run at a time, so overlapping pushes cannot build the same version twice. +concurrency: + group: release + cancel-in-progress: false 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 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 runs-on: ubuntu-latest + permissions: + contents: read + outputs: + ref: ${{ steps.version.outputs.ref }} + 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 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" + echo "ref=$(git rev-parse -q --verify "refs/tags/$version^{commit}" || echo "$GITHUB_SHA")" >> "$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 + with: + ref: ${{ needs.check.outputs.ref }} + + - 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: 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/* + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist + path: dist/ + + # Tags the release commit before the upload. A rerun keeps the existing release. + 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: ${{ env.CHANGELOG }} - tag: ${{ env.VERSION }} + body: ${{ needs.build.outputs.changelog }} + tag: ${{ needs.check.outputs.version }} + 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 + # publisher requires the pypi environment, which only master can deploy to. + publish: + name: 📦 Publish to PyPI + needs: [build, release] + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: dist + path: dist/ + + # 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: + skip-existing: true