name: Publish Packages on: workflow_dispatch: inputs: dry-run: description: 'Dry run (unchecked will publish to npm, push git tag, and create GitHub release)' required: true default: true type: boolean dist-tag: description: "npm dist-tag. 'auto' derives it from the version (prerelease identifier, or 'latest' for stable)." required: true default: auto type: choice options: - auto - latest - next - rc - beta permissions: contents: write id-token: write # Prevent concurrent publishes concurrency: group: publish cancel-in-progress: false env: # Directory names under packages/, in dependency order. Every package publishes # as @solana/ and all packages share one version. Add new packages here. PUBLISH_PACKAGES: | web3.js wallet-adapter jobs: guard-branch: name: Guard allowed source branch for publish runs-on: ubuntu-latest steps: - name: Fail if workflow_dispatch is not on main run: | if [ "${{ github.ref }}" != "refs/heads/main" ]; then echo "::error::Publish must be run from main. Current ref: ${{ github.ref }}" exit 1 fi preview-release: name: Preview release notes runs-on: ubuntu-latest needs: guard-branch steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - name: Write release preview to job summary env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} DIST_TAG: ${{ github.event.inputs.dist-tag }} DRY_RUN: ${{ github.event.inputs.dry-run }} run: | VERSION=$(node -p "require('./packages/web3.js/package.json').version") TAG="v${VERSION}" PREV_TAG=$(git tag -l 'v*' --sort=-version:refname | grep -v "^${TAG}$" | head -1 || true) NOTES_ARGS=(-f tag_name="$TAG" -f target_commitish="$GITHUB_SHA") if [ -n "$PREV_TAG" ]; then NOTES_ARGS+=(-f previous_tag_name="$PREV_TAG") fi NOTES=$(gh api "repos/${GITHUB_REPOSITORY}/releases/generate-notes" "${NOTES_ARGS[@]}" --jq .body) COMPARE_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/compare/${PREV_TAG}...${GITHUB_SHA}" echo "::notice title=Release preview::${TAG} from ${PREV_TAG:-} (dist-tag: ${DIST_TAG}, dry run: ${DRY_RUN})" { echo "## Release preview: ${TAG}" echo "" echo "- **Commit**: \`${GITHUB_SHA}\`" echo "- **Previous tag**: \`${PREV_TAG:-}\`" echo "- **dist-tag input**: \`${DIST_TAG}\`" echo "- **Dry run**: \`${DRY_RUN}\`" if [ -n "$PREV_TAG" ]; then echo "- **Diff**: [${PREV_TAG}...${GITHUB_SHA::10}](${COMPARE_URL})" fi echo "" echo "$NOTES" } >> $GITHUB_STEP_SUMMARY publish: name: Publish Packages runs-on: ubuntu-latest needs: preview-release environment: release env: NPM_CONFIG_PROVENANCE: true steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: fetch-depth: 0 - name: Setup Git user run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - name: Setup pnpm uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 with: run_install: false - name: Setup Node.js uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: '26' - name: Install dependencies run: pnpm install --frozen-lockfile - name: Resolve target packages id: targets run: | PACKAGES=$(echo $PUBLISH_PACKAGES | tr '\n' ' ') echo "packages=$PACKAGES" >> $GITHUB_OUTPUT echo "Target packages: $PACKAGES" - name: Get version and check consistency id: version run: | VERSION="" for pkg in ${{ steps.targets.outputs.packages }}; do PKG_VERSION=$(node -p "require('./packages/${pkg}/package.json').version") if [ -z "$VERSION" ]; then VERSION="$PKG_VERSION" elif [ "$PKG_VERSION" != "$VERSION" ]; then echo "::error::packages/${pkg} has version ${PKG_VERSION}, expected ${VERSION}. All packages must share a version." exit 1 fi echo " ✓ packages/${pkg}: ${PKG_VERSION}" done echo "version=$VERSION" >> $GITHUB_OUTPUT echo "Publishing version: $VERSION" - name: Resolve npm dist-tag id: prerelease env: VERSION: ${{ steps.version.outputs.version }} REQUESTED_TAG: ${{ github.event.inputs.dist-tag }} run: | # SemVer: MAJOR.MINOR.PATCH[-IDENTIFIER[.N]]. With dist-tag 'auto', the prerelease # identifier becomes the npm dist-tag (e.g. 3.0.0-rc.1 -> rc) and stable versions # publish under 'latest'. Any other choice overrides that derivation, e.g. 'next' # ships a stable version without moving 'latest'. if [[ "$VERSION" == *"-"* ]]; then IS_PRERELEASE=true DERIVED_TAG="${VERSION#*-}" DERIVED_TAG="${DERIVED_TAG%%.*}" else IS_PRERELEASE=false DERIVED_TAG=latest fi if [ "$REQUESTED_TAG" == "auto" ]; then NPM_TAG="$DERIVED_TAG" else NPM_TAG="$REQUESTED_TAG" fi if [ -z "$NPM_TAG" ]; then echo "::error::Could not derive npm dist-tag from version '$VERSION'" exit 1 fi if [ "$NPM_TAG" == "latest" ] && [ "$IS_PRERELEASE" == "true" ]; then echo "::error::Refusing to publish prerelease '$VERSION' under the 'latest' dist-tag" exit 1 fi echo "npm_tag=$NPM_TAG" >> $GITHUB_OUTPUT echo "is_prerelease=$IS_PRERELEASE" >> $GITHUB_OUTPUT echo "Version: $VERSION | requested: $REQUESTED_TAG | npm tag: $NPM_TAG | prerelease: $IS_PRERELEASE" - name: Pre-flight - check target packages exist on npm run: | # A brand-new package must be published manually once so the org owns the # name before OIDC trusted publishing can take over. MISSING="" for pkg in ${{ steps.targets.outputs.packages }}; do NPM_NAME="@solana/${pkg}" NPM_RC=0 NPM_OUT=$(npm view "$NPM_NAME" version 2>&1) || NPM_RC=$? if [ "$NPM_RC" -eq 0 ] && [ -n "$NPM_OUT" ]; then echo " ✓ $NPM_NAME (latest: $NPM_OUT)" elif echo "$NPM_OUT" | grep -q "E404"; then echo " ✗ $NPM_NAME does not exist on npm" MISSING="$MISSING $NPM_NAME" else echo " ✗ $NPM_NAME: registry error (not 404):" echo "$NPM_OUT" | sed 's/^/ /' MISSING="$MISSING $NPM_NAME(registry-error)" fi done if [ -n "$MISSING" ]; then echo "::error::The following packages cannot be published by CI:$MISSING" echo "If a package is brand new, an org maintainer must run a manual first-publish:" echo " cd packages/ && pnpm pack && npm publish --access public" echo "Then verify with 'npm view @solana/' and re-run this workflow." exit 1 fi echo "✓ all target packages exist on npm" - name: Enforce code formatting run: pnpm run test:format - name: Compile types run: pnpm run compile:typedefs - name: Compile JavaScript run: pnpm run compile:js - name: Lint run: pnpm run test:lint - name: Typecheck run: pnpm run test:typecheck - name: Run unit tests run: pnpm run test:unit - name: Verify install as a consumer run: pnpm --filter @solana/wallet-adapter run test:install - name: Pack packages run: | mkdir -p .npm-pack # pnpm pack (not npm pack) rewrites workspace:* to real versions. devDependencies # are dropped from the published manifest, mirroring web3.js's prepublishOnly, # since we publish from the tarball and pnpm pack does not run that hook. for pkg in ${{ steps.targets.outputs.packages }}; do echo " packages/${pkg}" pushd "packages/${pkg}" > /dev/null pnpm pkg delete devDependencies pnpm pack --pack-destination ../../.npm-pack git checkout -- package.json popd > /dev/null done echo "" echo "Packed tarballs:" ls -la .npm-pack/ - name: Guard - tarball manifests are clean run: | FAILED=0 for tarball in .npm-pack/*.tgz; do tar -xOzf "$tarball" package/package.json > /tmp/pkg.json if grep -q '"workspace:' /tmp/pkg.json; then echo " ✗ ${tarball} contains workspace: dependencies" grep '"workspace:' /tmp/pkg.json || true FAILED=1 elif ! node -e "const p=require('/tmp/pkg.json'); process.exit(p.devDependencies && Object.keys(p.devDependencies).length ? 1 : 0)"; then echo " ✗ ${tarball} contains devDependencies" FAILED=1 else echo " ✓ ${tarball}" fi done if [ "$FAILED" -eq 1 ]; then exit 1 fi - name: Guard - tarballs contain every declared entry point run: | # Every file referenced by main/module/types/react-native/browser/exports in the # packed manifest must be present in the tarball, so a broken build cannot ship. FAILED=0 for tarball in .npm-pack/*.tgz; do tar -tzf "$tarball" > /tmp/contents.txt tar -xOzf "$tarball" package/package.json > /tmp/pkg.json MISSING=$(node -e ' const pkg = require("/tmp/pkg.json"); const contents = new Set(require("fs").readFileSync("/tmp/contents.txt", "utf8").split("\n")); const files = new Set(); const collect = v => { if (typeof v === "string") { if (!v.includes("*") && /^\.?\/?(lib|dist|src)\/|\.(js|cjs|mjs|d\.ts|css)$/.test(v)) files.add(v.replace(/^\.\//, "")); } else if (v && typeof v === "object") Object.values(v).forEach(collect); }; ["main", "module", "types", "react-native", "browser", "exports"].forEach(k => collect(pkg[k])); const missing = [...files].filter(f => !contents.has(`package/${f}`)); if (files.size === 0) missing.push(""); console.log(missing.join("\n")); ') if [ -n "$MISSING" ]; then echo " ✗ ${tarball} missing:" echo "$MISSING" | sed 's/^/ /' FAILED=1 else echo " ✓ ${tarball}" fi done if [ "$FAILED" -eq 1 ]; then echo "" echo "Make sure compile:js and compile:typedefs ran successfully before packing." exit 1 fi - name: Pre-flight - npm publish dry-run for all targets run: | VERSION="${{ steps.version.outputs.version }}" NPM_TAG="${{ steps.prerelease.outputs.npm_tag }}" FAILED=0 FAILED_PKGS="" for pkg in ${{ steps.targets.outputs.packages }}; do NPM_NAME="@solana/${pkg}" TARBALL=".npm-pack/solana-${pkg}-${VERSION}.tgz" if [ ! -f "$TARBALL" ]; then echo " ✗ ${pkg}: tarball not found at ${TARBALL}" FAILED=1 FAILED_PKGS="${FAILED_PKGS} ${pkg}(missing-tarball)" continue fi # Refuse to overwrite a published version. Distinguish 404 (not published) # from real registry or auth errors. NPM_RC=0 NPM_OUT=$(npm view "${NPM_NAME}@${VERSION}" version 2>&1) || NPM_RC=$? if [ "$NPM_RC" -ne 0 ] && ! echo "$NPM_OUT" | grep -q "E404"; then echo " ✗ ${NPM_NAME}: registry error (not 404):" echo "$NPM_OUT" | sed 's/^/ /' FAILED=1 FAILED_PKGS="${FAILED_PKGS} ${pkg}(registry-error)" continue fi if [ "$NPM_RC" -eq 0 ] && [ -n "$NPM_OUT" ]; then echo " ✗ ${NPM_NAME}@${VERSION} already published. Bump the version in package.json." FAILED=1 FAILED_PKGS="${FAILED_PKGS} ${pkg}(already-published)" continue fi if ! npm publish "$TARBALL" --access public --tag "$NPM_TAG" --dry-run > "/tmp/dryrun-${pkg}.log" 2>&1; then echo " ✗ ${NPM_NAME}: dry-run failed" sed 's/^/ /' "/tmp/dryrun-${pkg}.log" FAILED=1 FAILED_PKGS="${FAILED_PKGS} ${pkg}(dryrun-failed)" continue fi echo " ✓ ${NPM_NAME}@${VERSION}" done echo "" if [ "$FAILED" -eq 1 ]; then echo "::error::Pre-flight failed for:${FAILED_PKGS}. Aborting before any real publish." exit 1 fi echo "✓ pre-flight passed for all targets" - name: Publish to npm if: ${{ github.event.inputs.dry-run != 'true' }} run: | VERSION="${{ steps.version.outputs.version }}" NPM_TAG="${{ steps.prerelease.outputs.npm_tag }}" PUBLISHED=0 for pkg in ${{ steps.targets.outputs.packages }}; do TARBALL=".npm-pack/solana-${pkg}-${VERSION}.tgz" echo "" echo " -> ${TARBALL} (tag '${NPM_TAG}')" npm publish "$TARBALL" --access public --tag "$NPM_TAG" PUBLISHED=$((PUBLISHED + 1)) done echo "" echo "✓ published ${PUBLISHED} packages" - name: Create and push tag if: ${{ github.event.inputs.dry-run != 'true' }} run: | TAG="v${{ steps.version.outputs.version }}" if git rev-parse "$TAG" >/dev/null 2>&1; then echo "Tag $TAG already exists, skipping" else git tag "$TAG" git push origin "$TAG" echo "Created tag $TAG" fi - name: Resolve previous tag id: prev_tag if: ${{ github.event.inputs.dry-run != 'true' }} run: | CURRENT_TAG="v${{ steps.version.outputs.version }}" PREV_TAG=$(git tag -l 'v*' --sort=-version:refname | grep -v "^${CURRENT_TAG}$" | head -1 || true) echo "previous_tag=${PREV_TAG}" >> $GITHUB_OUTPUT echo "Previous tag: ${PREV_TAG:-}" - name: Create GitHub Release if: ${{ github.event.inputs.dry-run != 'true' }} uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 env: PACKAGES: ${{ steps.targets.outputs.packages }} VERSION: ${{ steps.version.outputs.version }} PREVIOUS_TAG: ${{ steps.prev_tag.outputs.previous_tag }} IS_PRERELEASE: ${{ steps.prerelease.outputs.is_prerelease }} NPM_TAG: ${{ steps.prerelease.outputs.npm_tag }} with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const version = process.env.VERSION; const tagName = `v${version}`; const previousTag = process.env.PREVIOUS_TAG; const isPrerelease = process.env.IS_PRERELEASE === 'true'; const isNpmLatest = process.env.NPM_TAG === 'latest'; const repoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}`; const packages = process.env.PACKAGES.trim().split(/\s+/).map(p => `@solana/${p}`); const packageList = packages .map(p => `- [\`${p}@${version}\`](https://www.npmjs.com/package/${p}/v/${version})`) .join('\n'); const changelogLine = previousTag ? `**Full changelog**: [${previousTag}...${tagName}](${repoUrl}/compare/${previousTag}...${tagName})` : `**Full changelog**: [Initial release](${repoUrl}/commits/${tagName})`; const body = [ `## v${version}`, '', changelogLine, '', '### Published Packages', '', packageList, '', '### Installation', '', '```bash', `pnpm add ${packages.map(p => `${p}@${version}`).join(' ')}`, '```', ].join('\n'); try { await github.rest.repos.getReleaseByTag({ owner: context.repo.owner, repo: context.repo.repo, tag: tagName, }); console.log(`Release ${tagName} already exists, skipping`); return; } catch (e) { if (e.status !== 404) throw e; } await github.rest.repos.createRelease({ owner: context.repo.owner, repo: context.repo.repo, tag_name: tagName, name: tagName, body: body.trim(), draft: false, prerelease: isPrerelease, make_latest: isNpmLatest ? 'true' : 'false', }); console.log(`Created release: ${tagName}`); - name: Publish summary env: VERSION: ${{ steps.version.outputs.version }} NPM_TAG: ${{ steps.prerelease.outputs.npm_tag }} PREV_TAG: ${{ steps.prev_tag.outputs.previous_tag }} DRY_RUN: ${{ github.event.inputs.dry-run }} run: | { echo "## v${VERSION}" echo "" echo "**Version**: \`${VERSION}\`" echo "**npm tag**: \`${NPM_TAG}\`" echo "" echo "| Package | npm |" echo "|---------|-----|" for pkg in ${{ steps.targets.outputs.packages }}; do echo "| \`@solana/${pkg}\` | [npm](https://www.npmjs.com/package/@solana/${pkg}/v/${VERSION}) |" done echo "" } >> $GITHUB_STEP_SUMMARY if [ "$DRY_RUN" == "true" ]; then echo "Mode: **dry run** (OIDC + tarballs validated, nothing published)" >> $GITHUB_STEP_SUMMARY else { echo "Published to npm." echo "" echo "GitHub release: https://github.com/${{ github.repository }}/releases/tag/v${VERSION}" if [ -n "$PREV_TAG" ]; then echo "Full changelog: https://github.com/${{ github.repository }}/compare/${PREV_TAG}...v${VERSION}" fi } >> $GITHUB_STEP_SUMMARY fi