Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Publish npm from verified release artifacts

Sina Atalaya168c9ca5de5
diff --git a/.github/workflows/publish-npm.yaml b/.github/workflows/publish-npm.yaml
new file mode 100644
index 00000000..88fb15d2
--- /dev/null
+++ b/.github/workflows/publish-npm.yaml
@@ -0,0 +1,67 @@
+# Publication consumes the signed GitHub release, so it can be retried with current
+# publishing tools without rebuilding the package or moving an already published tag.
+name: Publish npm
+run-name: Publish npm ${{ inputs.tag }}
+
+on:
+ workflow_call:
+ inputs:
+ tag:
+ required: true
+ type: string
+ workflow_dispatch:
+ inputs:
+ tag:
+ description: Published GitHub release tag
+ required: true
+ type: string
+
+permissions: {}
+
+concurrency:
+ group: publish-npm-${{ inputs.tag }}
+ cancel-in-progress: false
+
+jobs:
+ publish:
+ if: github.event_name != 'workflow_dispatch' || github.ref == 'refs/heads/main'
+ runs-on: ubuntu-24.04
+ timeout-minutes: 10
+ environment:
+ name: npm
+ url: https://www.npmjs.com/package/manimgx
+ permissions:
+ contents: read
+ attestations: read
+ id-token: write
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ sparse-checkout: scripts/release
+
+ - name: Acquire the signed release and confirm its Python dependency is public
+ run: |
+ test "$(gh release view --repo "$GITHUB_REPOSITORY" --json isDraft --jq .isDraft -- "$TAG")" = false
+ gh release download --repo "$GITHUB_REPOSITORY" --dir dist --pattern "manimgx-${TAG#v}.tgz" -- "$TAG"
+ commit="$(gh api "repos/$GITHUB_REPOSITORY/commits/$TAG" --jq .sha)"
+ gh attestation verify dist/*.tgz --repo "$GITHUB_REPOSITORY" \
+ --signer-workflow "$GITHUB_REPOSITORY/.github/workflows/release.yaml" \
+ --source-ref "refs/tags/$TAG" --source-digest "$commit"
+ curl --fail --silent --show-error "https://pypi.org/pypi/manimgx/${TAG#v}/json" \
+ | jq -e '.urls | any(.filename | endswith("_wasm32.whl"))' > /dev/null
+ env:
+ GH_TOKEN: ${{ github.token }}
+ TAG: ${{ inputs.tag }}
+
+ - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
+ with:
+ node-version: 24
+ registry-url: https://registry.npmjs.org
+ package-manager-cache: false
+
+ - name: Publish and verify the exact built tarball
+ run: python3 scripts/release/publish_npm.py dist/*.tgz
+ env:
+ # Only needed for the first release, before npm allows a trusted publisher.
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml
index db3ddace..be947234 100644
--- a/.github/workflows/release.yaml
+++ b/.github/workflows/release.yaml
@@ -18,6 +18,7 @@ on:
- .github/workflows/release.yaml
- .github/workflows/create-wheels.yaml
- .github/workflows/create-executables.yaml
+ - .github/workflows/publish-npm.yaml
- scripts/release/**
- pyproject.toml
- fonts/*/pyproject.toml
@@ -431,35 +432,14 @@ jobs:
npm:
name: Publish to npm
# after PyPI: the package installs this release of ManimGX from there
- needs: [version, pypi]
- runs-on: ubuntu-24.04
- timeout-minutes: 10
- environment:
- name: npm
- url: https://www.npmjs.com/package/manimgx/v/${{ needs.version.outputs.version }}
+ needs: [pypi]
+ uses: $/.github/workflows/publish-npm.yaml
+ with:
+ tag: ${{ github.ref_name }}
permissions:
contents: read
+ attestations: read
id-token: write # trusted publishing, and the provenance's signature
- steps:
- - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- with:
- persist-credentials: false
- sparse-checkout: scripts/release
- - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
- with:
- name: npm
- path: dist
- # trusted publishing needs npm 11.5.1 or later
- - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
- with:
- node-version: 24
- registry-url: https://registry.npmjs.org
- package-manager-cache: false
- - name: Publish and verify the exact built tarball
- run: python3 scripts/release/publish_npm.py dist/*.tgz
- env:
- # Only needed for the first release, before npm allows a trusted publisher.
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
publish:
name: Verify release completion
diff --git a/docs/content/developer-guide/github-workflows.md b/docs/content/developer-guide/github-workflows.md
index 146feb87..d4a7d563 100644
--- a/docs/content/developer-guide/github-workflows.md
+++ b/docs/content/developer-guide/github-workflows.md
@@ -166,15 +166,19 @@ See [Documentation](documentation.md#deployment) for how the site is served.
GitHub environments with release tags (`v*`) allowed. Pending publishers need distinct
workflow/environment combinations, even when their project names differ. Publishing
uses the workflow's OIDC token: no PyPI token is stored.
-6. **Publish to npm:** the already-built tarball for the browser, which installs this
- release of ManimGX from PyPI, so it comes after PyPI; by trusted publishing too, with
+6. **Publish to npm:** `publish-npm.yaml` acquires the browser's tarball from the public
+ GitHub release and verifies its attestation against the release workflow, tag and commit.
+ It confirms the matching Python browser wheel is public on PyPI before publishing with
provenance. A retry accepts an existing version only when its SHA512 integrity matches
- the exact tarball. For the first release, before the package exists on npm, create a
+ the exact tarball. The same workflow can run by hand from `main`, with a published `tag`,
+ to retry publication with current tools without rebuilding or moving that tag.
+ For the first release, before the package exists on npm, create a
short-lived granular token with read/write access to all packages and bypass 2FA,
- and store it as `NPM_TOKEN` in the GitHub `npm` environment (allow only `v*` tags).
+ and store it as `NPM_TOKEN` in the GitHub `npm` environment (allow `v*` tags and `main`).
The token is passed only to the publishing step. Once the package exists, configure
- its npm trusted publisher with owner `academa-labs`, repository `manimgx`, workflow
- `release.yaml`, and environment `npm`; enable direct publishing (`npm publish`),
+ its npm trusted publishers with owner `academa-labs`, repository `manimgx`, workflows
+ `release.yaml` and `publish-npm.yaml`, and environment `npm`; npm checks the caller's
+ workflow identity. Enable direct publishing (`npm publish`),
since the workflow does not stage releases. Then revoke the token and delete the
GitHub secret: subsequent releases use OIDC.
7. **Publish the Docker image:** copy the tested OCI archives without changing their digests,
diff --git a/scripts/release/publish_npm.py b/scripts/release/publish_npm.py
index b13d8b7b..28ae8a53 100644
--- a/scripts/release/publish_npm.py
+++ b/scripts/release/publish_npm.py
@@ -10,6 +10,8 @@ from pathlib import Path
def publish(archive: Path) -> None:
+ # npm interprets relative names such as dist/package.tgz as GitHub package specs.
+ archive = archive.resolve()
with tarfile.open(archive) as package:
metadata = package.extractfile("package/package.json")
if metadata is None:
diff --git a/tests/test_release.py b/tests/test_release.py
index bae157a2..f61851f0 100644
--- a/tests/test_release.py
+++ b/tests/test_release.py
@@ -24,6 +24,10 @@ ROOT = Path(__file__).parents[1]
JOBS = yaml.safe_load(
(ROOT / ".github/workflows/release.yaml").read_text(encoding="utf-8")
)["jobs"]
+NPM = yaml.load(
+ (ROOT / ".github/workflows/publish-npm.yaml").read_text(encoding="utf-8"),
+ Loader=yaml.BaseLoader,
+)
@pytest.mark.parametrize("autocrlf", ["false", "true", "input"])
@@ -281,9 +285,11 @@ def test_every_artifact_is_built_before_public_source_and_registry_uploads() ->
def test_registry_uploads_use_built_artifacts_without_rebuilding() -> None:
- npm = JOBS["npm"]["steps"]
- assert any(step.get("with", {}).get("name") == "npm" for step in npm)
- assert not any("build-npm" in step.get("run", "") for step in npm)
+ npm = NPM["jobs"]["publish"]["steps"]
+ command = "\n".join(step.get("run", "") for step in npm)
+ assert 'gh release download --repo "$GITHUB_REPOSITORY"' in command
+ assert '--pattern "manimgx-${TAG#v}.tgz" -- "$TAG"' in command
+ assert not any(build in command for build in ["build-npm", "npm pack", "bun run"])
docker = JOBS["docker"]["steps"]
assert any(step.get("with", {}).get("pattern") == "docker-*" for step in docker)
command = "\n".join(step.get("run", "") for step in docker)
@@ -297,11 +303,65 @@ def test_registry_uploads_use_built_artifacts_without_rebuilding() -> None:
assert "--no-deps /wheels/*.whl" in release
+def test_npm_release_and_manual_recovery_share_the_publisher() -> None:
+ assert JOBS["npm"]["uses"] == "$/.github/workflows/publish-npm.yaml"
+ assert JOBS["npm"]["with"] == {"tag": "${{ github.ref_name }}"}
+ for event in ["workflow_call", "workflow_dispatch"]:
+ tag = NPM["on"][event]["inputs"]["tag"]
+ assert tag["required"] == "true"
+ assert tag["type"] == "string"
+ publisher = NPM["jobs"]["publish"]
+ assert publisher["if"] == (
+ "github.event_name != 'workflow_dispatch' || github.ref == 'refs/heads/main'"
+ )
+ assert publisher["environment"]["name"] == "npm"
+
+
+def test_npm_publication_authenticates_the_release_before_uploading() -> None:
+ steps = NPM["jobs"]["publish"]["steps"]
+ acquire = next(
+ step for step in steps if "gh attestation verify" in step.get("run", "")
+ )
+ assert acquire["env"]["TAG"] == "${{ inputs.tag }}"
+ source = acquire["run"]
+ assert '--json isDraft --jq .isDraft -- "$TAG")" = false' in source
+ assert (
+ 'commit="$(gh api "repos/$GITHUB_REPOSITORY/commits/$TAG" --jq .sha)"' in source
+ )
+ assert (
+ '--signer-workflow "$GITHUB_REPOSITORY/.github/workflows/release.yaml"'
+ in source
+ )
+ assert '--source-ref "refs/tags/$TAG" --source-digest "$commit"' in source
+ assert (
+ 'curl --fail --silent --show-error "https://pypi.org/pypi/manimgx/${TAG#v}/json"'
+ in source
+ )
+ assert "jq -e '.urls | any(.filename | endswith(\"_wasm32.whl\"))'" in source
+ command = "\n".join(step.get("run", "") for step in steps)
+ positions = [
+ command.index(gate)
+ for gate in [
+ "gh release view",
+ "gh release download",
+ "commit=",
+ "gh attestation verify",
+ "https://pypi.org/pypi/manimgx/",
+ "python3 scripts/release/publish_npm.py dist/*.tgz",
+ ]
+ ]
+ assert positions == sorted(positions)
+
+
@pytest.mark.parametrize("state", ["new", "identical", "different", "unauthorized"])
[email protected]("relative", [False, True], ids=["absolute", "relative"])
def test_npm_retry_requires_the_exact_tarball(
- tmp_path: Path, monkeypatch: pytest.MonkeyPatch, state: str
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, state: str, relative: bool
) -> None:
- archive = tmp_path / "package.tgz"
+ archive = tmp_path / "dist" / "package.tgz"
+ archive.parent.mkdir()
+ monkeypatch.chdir(tmp_path)
+ argument = archive.relative_to(tmp_path) if relative else archive
with tarfile.open(archive, "w:gz") as package:
body = json.dumps({"name": "manimgx", "version": "0.1.0"}).encode()
member = tarfile.TarInfo("package/package.json")
@@ -315,6 +375,7 @@ def test_npm_retry_requires_the_exact_tarball(
def run(command: list[str], **_options: object) -> subprocess.CompletedProcess[str]:
if command[1] == "publish":
+ assert Path(command[2]).is_absolute(), "npm must receive a local file path"
uploaded.append(Path(command[2]).read_bytes())
return subprocess.CompletedProcess(command, 0, "", "")
assert command == ["npm", "view", "[email protected]", "dist.integrity", "--json"]
@@ -333,12 +394,12 @@ def test_npm_retry_requires_the_exact_tarball(
monkeypatch.setattr(subprocess, "run", run)
if state == "different":
with pytest.raises(ValueError, match="different artifact bytes"):
- publish_npm.publish(archive)
+ publish_npm.publish(argument)
elif state == "unauthorized":
with pytest.raises(RuntimeError, match="cannot inspect"):
- publish_npm.publish(archive)
+ publish_npm.publish(argument)
else:
- publish_npm.publish(archive)
+ publish_npm.publish(argument)
assert uploaded == ([archive.read_bytes()] if state == "new" else [])