|
| 1 | +# ========================================== |
| 2 | +# 🚀 NPM Trusted Publishing Workflow |
| 3 | +# ========================================== |
| 4 | +# Automates publishing to npm using GitHub Actions |
| 5 | +# with "Trusted Publishing" — no NPM_TOKEN needed. |
| 6 | +# Uses GitHub’s OpenID Connect (OIDC) for secure identity verification. |
| 7 | +# |
| 8 | +# Docs: |
| 9 | +# - Trusted Publishers: https://docs.npmjs.com/trusted-publishers |
| 10 | +# - setup-node: https://github.com/actions/setup-node |
| 11 | +# - setup-pnpm: https://github.com/pnpm/action-setup |
| 12 | +# ========================================== |
| 13 | + |
| 14 | +name: Release |
| 15 | + |
| 16 | +on: |
| 17 | + push: |
| 18 | + branches: ['main'] # Trigger when code is pushed to main |
| 19 | + pull_request: |
| 20 | + types: [opened, synchronize] # Optional: validate PRs to main |
| 21 | + |
| 22 | +permissions: |
| 23 | + id-token: write # ✅ Required for Trusted Publishing (OIDC) |
| 24 | + contents: write # Required if creating git tags or GitHub releases |
| 25 | + |
| 26 | +jobs: |
| 27 | + build: |
| 28 | + name: 🚀 Release |
| 29 | + runs-on: ubuntu-latest |
| 30 | + timeout-minutes: 15 |
| 31 | + |
| 32 | + steps: |
| 33 | + # --------------------------------------------------------- |
| 34 | + # 🧩 1. Checkout source code |
| 35 | + # --------------------------------------------------------- |
| 36 | + - name: 📂 Check repository |
| 37 | + uses: actions/checkout@v4 |
| 38 | + with: |
| 39 | + fetch-depth: 2 # Fetch recent commits for changelog or versioning tools |
| 40 | + |
| 41 | + # --------------------------------------------------------- |
| 42 | + # 🧩 2. Setup pnpm |
| 43 | + # --------------------------------------------------------- |
| 44 | + - name: 📦 Setup pnpm # https://github.com/marketplace/actions/setup-pnpm |
| 45 | + uses: pnpm/action-setup@v4 |
| 46 | + |
| 47 | + # --------------------------------------------------------- |
| 48 | + # 🧩 3. Setup Node.js environment |
| 49 | + # --------------------------------------------------------- |
| 50 | + - name: 📦 Setup Node.js (v20+) # https://github.com/actions/setup-node |
| 51 | + uses: actions/setup-node@v4 # Reads Node.js version from package.json > engines.node |
| 52 | + with: |
| 53 | + node-version-file: './package.json' |
| 54 | + cache: 'pnpm' # Enable caching for pnpm dependencies |
| 55 | + registry-url: 'https://registry.npmjs.org/' # Set npm registry for publishing |
| 56 | + |
| 57 | + # --------------------------------------------------------- |
| 58 | + # 🧩 4. Update npm to latest (ensure OIDC-compatible version) |
| 59 | + # --------------------------------------------------------- |
| 60 | + - name: 🆙 Update npm |
| 61 | + run: | |
| 62 | + echo "Current npm version: $(npm -v)" |
| 63 | + npm install -g npm@latest |
| 64 | + echo "Updated npm version: $(npm -v)" |
| 65 | +
|
| 66 | + # --------------------------------------------------------- |
| 67 | + # 🧩 5. Install dependencies |
| 68 | + # --------------------------------------------------------- |
| 69 | + - name: 📦 Install dependencies |
| 70 | + run: pnpm install --frozen-lockfile # --frozen-lockfile ensures exact versions from pnpm-lock.yaml are used |
| 71 | + |
| 72 | + # --------------------------------------------------------- |
| 73 | + # 🧩 6. (Optional) Test or build project |
| 74 | + # --------------------------------------------------------- |
| 75 | + # - name: 🧪 Run tests |
| 76 | + # run: pnpm test |
| 77 | + |
| 78 | + # - name: 🛠️ Build project |
| 79 | + # run: pnpm build |
| 80 | + |
| 81 | + # --------------------------------------------------------- |
| 82 | + # 🧩 7. Release / Publish package |
| 83 | + # --------------------------------------------------------- |
| 84 | + - name: 🚀 Release (Trusted Publishing) |
| 85 | + # --access public ensures the package is published as a public npm package |
| 86 | + # pnpm release = pnpm build:release && changeset version && changeset publish |
| 87 | + run: pnpm release |
| 88 | + env: |
| 89 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + # GITHUB_TOKEN is used only for GitHub authentication (tags/releases). |
| 91 | + # npm authentication happens automatically via OIDC (Trusted Publishing). |
0 commit comments