Skip to content

Add trusted publishing#791

Open
ericglau wants to merge 9 commits intomasterfrom
oidc
Open

Add trusted publishing#791
ericglau wants to merge 9 commits intomasterfrom
oidc

Conversation

@ericglau
Copy link
Copy Markdown
Member

@ericglau ericglau commented Apr 1, 2026

Summary

Add npm trusted publishing support to the publish workflow while keeping NPM_TOKEN fallback for first-time publishes of new packages.

Changes

  • Add registry-url: 'https://registry.npmjs.org' to the shared setup action so setup-node writes npm config for the npm registry and publish auth is wired correctly.
  • Add id-token: write to .github/workflows/publish.yml so GitHub Actions can mint the OIDC token required by npm trusted publishing.
  • Install npm@11.12.1 in the publish workflow because trusted publishing requires npm 11.5.1+, while the default npm on Node 22 is older.
  • Switch publish auth from NPM_TOKEN to NODE_AUTH_TOKEN because setup-node writes .npmrc using ${NODE_AUTH_TOKEN}, which is the env var npm will actually read.
  • Add NPM_CONFIG_PROVENANCE: true so provenance is generated consistently, including when the workflow falls back to token-based publish.

Notes

  • Keeps NPM_TOKEN fallback for new packages that do not yet have a trusted publisher configured.

@ericglau ericglau requested a review from tirumerla April 1, 2026 17:41
@ericglau ericglau requested a review from a team as a code owner April 1, 2026 17:41
Copy link
Copy Markdown
Collaborator

@tirumerla tirumerla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we still need the token, otherwise lgtm

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the TOKEN?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 2, 2026

Walkthrough

GitHub Actions configuration updates enable OIDC-based trusted publishing for npm packages. The setup action now configures the npm registry URL. The publish workflow installs npm@11.12.1, detects new packages in Yarn workspaces, and conditionally exports authentication tokens for publishing operations.

Changes

Cohort / File(s) Summary
Setup Action
.github/actions/setup/action.yml
Added registry-url input to actions/setup-node step, configured to use https://registry.npmjs.org.
Publish Workflow
.github/workflows/publish.yml
Added id-token: write permissions for OIDC support, installed npm@11.12.1, added workspace package detection logic, and implemented conditional NODE_AUTH_TOKEN export when new packages are detected. Changed changesets action to use NPM_CONFIG_PROVENANCE: true instead of passing NPM_TOKEN.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add trusted publishing' directly matches the main objective of the PR: implementing npm trusted publishing (OIDC-based) in the publish workflow.
Description check ✅ Passed The description comprehensively covers the changeset, explaining the key modifications to both the setup action and publish workflow, the rationale for each change, and the NPM_TOKEN fallback strategy.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch oidc

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
.github/workflows/publish.yml (1)

50-66: ⚠️ Potential issue | 🟠 Major

Upgrade changesets/action to v1.6.0+ to enable OIDC-aware authentication.

The workflow uses changesets/action@v1.5.3, which lacks OIDC-aware logic added in v1.6.0+. When has_new_packages is true, NODE_AUTH_TOKEN is set in the job environment, and v1.5.3 will use this token for all packages in the subsequent publish: npm run publish invocation—including existing packages that could use OIDC trusted publishing. Upgrading to v1.6.0 or later enables the action to skip token configuration when OIDC conditions are met, allowing existing packages to publish via OIDC while only new packages (or those requiring token fallback) use NPM_TOKEN.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.github/workflows/publish.yml around lines 50 - 66, The workflow step using
changesets/action pinned as
changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba (v1.5.3) should be
updated to v1.6.0 or later so the action can perform OIDC-aware authentication;
change the step with id "changesets" (the "Create Prepare Release PR or Publish"
step) to use a v1.6.0+ reference, and keep the existing
NODE_AUTH_TOKEN/NPM_TOKEN logic and has_new_packages conditional intact so the
action will skip injecting the token when OIDC is available and only fall back
to NPM_TOKEN when necessary.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.github/workflows/publish.yml:
- Around line 35-49: The script currently treats any failure of the npm view
command as a new-package signal; change the loop that runs npm view "$pkg" so it
captures stderr/exit status and only treats definite 404 responses (e.g., stderr
contains "404" / "Not Found" / "E404") as a new package to append to
"$RUNNER_TEMP/new_packages.txt"; for any other non-404 failure (timeouts, 5xx,
rate limits, network errors) have the step exit non-zero (fail) with an error
message so the workflow doesn’t silently switch to token publishing. Update the
logic around the npm view call and the determination of has_new_packages to
reflect this behavior while still writing to "$RUNNER_TEMP/new_packages.txt"
only for true 404s.

---

Outside diff comments:
In @.github/workflows/publish.yml:
- Around line 50-66: The workflow step using changesets/action pinned as
changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba (v1.5.3) should be
updated to v1.6.0 or later so the action can perform OIDC-aware authentication;
change the step with id "changesets" (the "Create Prepare Release PR or Publish"
step) to use a v1.6.0+ reference, and keep the existing
NODE_AUTH_TOKEN/NPM_TOKEN logic and has_new_packages conditional intact so the
action will skip injecting the token when OIDC is available and only fall back
to NPM_TOKEN when necessary.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 464b403d-d857-4a0a-b7c9-2f87dec0ef89

📥 Commits

Reviewing files that changed from the base of the PR and between b907a53 and 85aec59.

📒 Files selected for processing (2)
  • .github/actions/setup/action.yml
  • .github/workflows/publish.yml

Comment on lines +35 to +49
" | while IFS=: read -r pkg dir; do
if ! npm view "$pkg" version > /dev/null 2>&1; then
echo "New package detected: $pkg ($dir)"
echo "$dir" >> "$RUNNER_TEMP/new_packages.txt"
else
echo "Existing package: $pkg"
fi
done
if [ -s "$RUNNER_TEMP/new_packages.txt" ]; then
echo "::notice::New packages detected — will use NPM token for publish"
echo "has_new_packages=true" >> "$GITHUB_OUTPUT"
else
echo "All packages exist on npm — using OIDC trusted publishing"
echo "has_new_packages=false" >> "$GITHUB_OUTPUT"
fi
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify that the current branch condition treats every non-zero `npm view` exit as "new package".
sed -n '35,49p' .github/workflows/publish.yml
rg -n -C2 'npm view "\$pkg" version > /dev/null 2>&1|New package detected|has_new_packages=true' .github/workflows/publish.yml

Repository: OpenZeppelin/contracts-wizard

Length of output: 1347


Differentiate 404s from transient npm view failures to avoid silent auth-path changes.

Line 36 treats every npm view error identically—timing out, returning 5xx, or hitting rate limits will silently flip the job into token mode instead of surfacing the actual registry failure. Only treat definite 404 responses as "new package"; fail the step on other errors.

Suggested fix
-          " | while IFS=: read -r pkg dir; do
-            if ! npm view "$pkg" version > /dev/null 2>&1; then
+          " | while IFS=: read -r pkg dir; do
+            if output="$(npm view "$pkg" version 2>&1)"; then
+              echo "Existing package: $pkg"
+            elif printf '%s' "$output" | grep -qE 'E404|404 Not Found'; then
               echo "New package detected: $pkg ($dir)"
               echo "$dir" >> "$RUNNER_TEMP/new_packages.txt"
             else
-              echo "Existing package: $pkg"
+              printf '%s\n' "$output" >&2
+              exit 1
             fi
           done
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
" | while IFS=: read -r pkg dir; do
if ! npm view "$pkg" version > /dev/null 2>&1; then
echo "New package detected: $pkg ($dir)"
echo "$dir" >> "$RUNNER_TEMP/new_packages.txt"
else
echo "Existing package: $pkg"
fi
done
if [ -s "$RUNNER_TEMP/new_packages.txt" ]; then
echo "::notice::New packages detected — will use NPM token for publish"
echo "has_new_packages=true" >> "$GITHUB_OUTPUT"
else
echo "All packages exist on npm — using OIDC trusted publishing"
echo "has_new_packages=false" >> "$GITHUB_OUTPUT"
fi
" | while IFS=: read -r pkg dir; do
if output="$(npm view "$pkg" version 2>&1)"; then
echo "Existing package: $pkg"
elif printf '%s' "$output" | grep -qE 'E404|404 Not Found'; then
echo "New package detected: $pkg ($dir)"
echo "$dir" >> "$RUNNER_TEMP/new_packages.txt"
else
printf '%s\n' "$output" >&2
exit 1
fi
done
if [ -s "$RUNNER_TEMP/new_packages.txt" ]; then
echo "::notice::New packages detected — will use NPM token for publish"
echo "has_new_packages=true" >> "$GITHUB_OUTPUT"
else
echo "All packages exist on npm — using OIDC trusted publishing"
echo "has_new_packages=false" >> "$GITHUB_OUTPUT"
fi
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.github/workflows/publish.yml around lines 35 - 49, The script currently
treats any failure of the npm view command as a new-package signal; change the
loop that runs npm view "$pkg" so it captures stderr/exit status and only treats
definite 404 responses (e.g., stderr contains "404" / "Not Found" / "E404") as a
new package to append to "$RUNNER_TEMP/new_packages.txt"; for any other non-404
failure (timeouts, 5xx, rate limits, network errors) have the step exit non-zero
(fail) with an error message so the workflow doesn’t silently switch to token
publishing. Update the logic around the npm view call and the determination of
has_new_packages to reflect this behavior while still writing to
"$RUNNER_TEMP/new_packages.txt" only for true 404s.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants