diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 00000000..2cec745e --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,164 @@ +# Copyright (c) 2025-2026 Datalayer, Inc. +# Distributed under the terms of the MIT License. + +# Publishes the release a tag names, to PyPI (lexical-loro) and npm +# (@datalayer/lexical-loro), with no stored token: both registries trust +# this workflow file (OIDC trusted publishing, through the `pypi` and `npm` +# environments). The two packages share one version — pyproject.toml reads +# it straight from package.json (hatch-nodejs-version) — which the tag must +# name. A package already published at that version is skipped, so a re-run +# after a partial upload publishes the rest. See RELEASE.md. + +name: Release + +on: + push: + tags: + - 'v*' + +concurrency: + group: release-${{ github.ref }} + cancel-in-progress: false + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + outputs: + npm: ${{ steps.plan.outputs.npm }} + python: ${{ steps.plan.outputs.python }} + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: '22' + + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Check the tag names the version both packages carry + run: | + set -euo pipefail + tag="${GITHUB_REF_NAME#v}" + version="$(node -p "require('./package.json').version")" + echo "tag=$tag version=$version" + test "$tag" = "$version" + + - name: Find what is not published yet + id: plan + run: | + set -euo pipefail + version="${GITHUB_REF_NAME#v}" + if npm view "@datalayer/lexical-loro@$version" version >/dev/null 2>&1; then + echo "npm: @datalayer/lexical-loro@$version is already published, skipping" + echo "npm=" >> "$GITHUB_OUTPUT" + else + echo "npm: @datalayer/lexical-loro@$version will be published" + echo "npm=yes" >> "$GITHUB_OUTPUT" + fi + status="$(curl -sL -o /dev/null -w '%{http_code}' "https://pypi.org/pypi/lexical-loro/$version/json")" + if [ "$status" = "200" ]; then + echo "PyPI: lexical-loro $version is already published, skipping" + echo "python=" >> "$GITHUB_OUTPUT" + else + echo "PyPI: lexical-loro $version will be published" + echo "python=yes" >> "$GITHUB_OUTPUT" + fi + + - name: Build and pack the npm package + if: steps.plan.outputs.npm != '' + run: | + set -euo pipefail + npm install --no-audit --no-fund + npm run build:lib + mkdir -p dist-npm + npm pack --pack-destination dist-npm + ls -la dist-npm + + - name: Build the Python package + if: steps.plan.outputs.python != '' + run: | + set -euo pipefail + python -m pip install --upgrade pip build + python -m build --sdist --wheel --outdir dist-py . + ls -la dist-py + + - uses: actions/upload-artifact@v4 + if: steps.plan.outputs.npm != '' + with: + name: npm + path: dist-npm/*.tgz + + - uses: actions/upload-artifact@v4 + if: steps.plan.outputs.python != '' + with: + name: python + path: dist-py/* + + pypi: + needs: build + if: needs.build.outputs.python != '' + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + name: python + path: dist + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + skip-existing: true + + npm: + needs: build + if: needs.build.outputs.npm != '' + runs-on: ubuntu-latest + environment: npm + permissions: + id-token: write + steps: + - uses: actions/setup-node@v4 + with: + node-version: '22' + registry-url: 'https://registry.npmjs.org' + # Trusted publishing needs npm 11.5.1 or later. + - run: npm install -g npm@^11.5.1 && npm --version + - uses: actions/download-artifact@v4 + with: + name: npm + path: dist + - name: Publish + run: | + set -euo pipefail + for tarball in dist/*.tgz; do + # "./": npm reads a bare "dist/x.tgz" as the GitHub shorthand + # github:dist/x.tgz and refuses to fetch it (EALLOWGIT). + npm publish "./$tarball" --provenance --access public + done + + release: + name: GitHub release + needs: [build, pypi, npm] + if: always() && needs.build.result == 'success' && needs.pypi.result != 'failure' && needs.npm.result != 'failure' + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Create the release with generated notes + run: | + version="${GITHUB_REF_NAME#v}" + gh release view "$GITHUB_REF_NAME" >/dev/null 2>&1 && exit 0 + gh release create "$GITHUB_REF_NAME" --title "Release $version" --generate-notes \ + --notes "- [PyPI lexical-loro](https://pypi.org/project/lexical-loro/$version/) + - [npm @datalayer/lexical-loro](https://www.npmjs.com/package/@datalayer/lexical-loro/v/$version)" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 00000000..def890d5 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,46 @@ + + +# Making a release + +One tag releases both packages, with no stored token: PyPI and npm trust +`.github/workflows/release.yaml` through OIDC (trusted publishing). + +| Package | Registry | Version from | GitHub environment | +| -------------------------- | -------- | -------------------------------------- | ------------------- | +| `lexical-loro` | PyPI | `package.json` (via hatch-nodejs-version) | `pypi` | +| `@datalayer/lexical-loro` | npm | `package.json` | `npm` | + +Both packages carry **one version**: `package.json`'s. `pyproject.toml` +reads it straight from there (`[tool.hatch.version] source = "nodejs"`), so +there is nothing to keep in sync by hand. + +## Steps + +1. Bump `version` in `package.json`, on a branch, and open a pull request. +2. Merge, then tag the merge commit and push the tag: + + ```bash + git checkout main && git pull + git tag vX.Y.Z + git push origin vX.Y.Z + ``` + +3. The `Release` workflow checks that the tag names the version, builds the + wheel, the sdist and the npm tarball, publishes what is not on the + registries yet, and creates a GitHub release with generated notes. + +## Trusted publishing + +- PyPI project `lexical-loro`: owner `datalayer`, repository `lexical-loro`, + workflow `release.yaml`, environment `pypi`. +- npm package `@datalayer/lexical-loro`: GitHub Actions, organization + `datalayer`, repository `lexical-loro`, workflow filename `release.yaml`, + environment `npm`. npm provenance also checks `package.json`'s + `repository.url`, which already names this repository. + +The registry matches the repository, the workflow filename and the +environment exactly; renaming any of them means re-registering. diff --git a/package.json b/package.json index d6705b2c..51072af4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@datalayer/lexical-loro", - "version": "1.0.6", + "version": "1.0.7", "main": "lib/index.js", "files": [ "lib/**/*.{css,d.ts,eot,gif,html,jpg,js,js.map,json,png,svg,woff2,ttf}",