|
| 1 | +# npm Publishing Workflow Setup Guide |
| 2 | + |
| 3 | +This guide explains how to set up automatic npm publishing when you create a GitHub Release. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🎯 Workflow Overview |
| 8 | + |
| 9 | +``` |
| 10 | +1. Update version in package.json |
| 11 | +2. Commit & push to main |
| 12 | +3. Create GitHub Release with tag |
| 13 | + └─> Triggers GitHub Action |
| 14 | + ├─ ✅ Run all 237 tests |
| 15 | + ├─ ✅ Build package |
| 16 | + ├─ ✅ Validate version matches tag |
| 17 | + ├─ ✅ Verify build output |
| 18 | + └─ 🚀 Publish to npm |
| 19 | +``` |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 🔐 One-Time Setup: npm Token |
| 24 | + |
| 25 | +### Step 1: Generate npm Token |
| 26 | + |
| 27 | +1. **Login to npm:** |
| 28 | + ```bash |
| 29 | + npm login |
| 30 | + ``` |
| 31 | + |
| 32 | +2. **Generate an Automation Token:** |
| 33 | + - Go to [npmjs.com](https://www.npmjs.com/) |
| 34 | + - Click your profile → "Access Tokens" |
| 35 | + - Click "Generate New Token" → "Classic Token" |
| 36 | + - Select **"Automation"** (required for CI/CD) |
| 37 | + - Give it a name: `BackpackFlow-GitHub-Actions` |
| 38 | + - Copy the token (starts with `npm_...`) |
| 39 | + |
| 40 | +### Step 2: Add Token to GitHub Secrets |
| 41 | + |
| 42 | +1. **Go to your GitHub repository:** |
| 43 | + - Navigate to: `https://github.com/pyrotank41/Backpackflow` |
| 44 | + |
| 45 | +2. **Add the secret:** |
| 46 | + - Go to **Settings** → **Secrets and variables** → **Actions** |
| 47 | + - Click **"New repository secret"** |
| 48 | + - Name: `NPM_TOKEN` |
| 49 | + - Value: Paste your npm token |
| 50 | + - Click **"Add secret"** |
| 51 | + |
| 52 | +✅ **Setup complete!** The workflow will now be able to publish to npm. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## 📦 How to Release a New Version |
| 57 | + |
| 58 | +### The Simple 3-Step Process |
| 59 | + |
| 60 | +```bash |
| 61 | +# Step 1: Bump version (updates package.json, creates commit & tag) |
| 62 | +npm version major # or minor, or patch |
| 63 | + |
| 64 | +# Step 2: Push to GitHub (including tags) |
| 65 | +git push origin main --follow-tags |
| 66 | + |
| 67 | +# Step 3: Create GitHub Release from tag (triggers workflow) |
| 68 | +gh release create v2.0.0 --generate-notes |
| 69 | +# OR use GitHub UI: https://github.com/pyrotank41/Backpackflow/releases/new |
| 70 | +``` |
| 71 | + |
| 72 | +That's it! The GitHub Action will automatically test, build, and publish to npm. |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### Detailed Step-by-Step Guide |
| 77 | + |
| 78 | +#### Step 1: Use `npm version` to Bump Version |
| 79 | + |
| 80 | +The `npm version` command handles everything automatically: |
| 81 | + |
| 82 | +```bash |
| 83 | +# For breaking changes (1.3.0 → 2.0.0) |
| 84 | +npm version major -m "chore: release v%s" |
| 85 | + |
| 86 | +# For new features (2.0.0 → 2.1.0) |
| 87 | +npm version minor -m "chore: release v%s" |
| 88 | + |
| 89 | +# For bug fixes (2.0.0 → 2.0.1) |
| 90 | +npm version patch -m "chore: release v%s" |
| 91 | +``` |
| 92 | + |
| 93 | +**What this does:** |
| 94 | +- ✅ Updates `package.json` version |
| 95 | +- ✅ Creates a git commit with message `"chore: release vX.Y.Z"` |
| 96 | +- ✅ Creates a git tag (e.g., `v2.0.0`) |
| 97 | +- ✅ All in one command! |
| 98 | + |
| 99 | +#### Step 2: Push to GitHub |
| 100 | + |
| 101 | +```bash |
| 102 | +# Push commits AND tags to GitHub |
| 103 | +git push origin main --follow-tags |
| 104 | +``` |
| 105 | + |
| 106 | +**Important:** The `--follow-tags` flag pushes the version tag to GitHub. |
| 107 | + |
| 108 | +#### Step 3: Create GitHub Release |
| 109 | + |
| 110 | +**Option A: Using GitHub CLI (Fastest)** |
| 111 | + |
| 112 | +```bash |
| 113 | +# Install gh CLI if you haven't |
| 114 | +brew install gh # macOS |
| 115 | + |
| 116 | +# Login once |
| 117 | +gh auth login |
| 118 | + |
| 119 | +# Create release (auto-generates notes from commits) |
| 120 | +gh release create v2.0.0 --generate-notes |
| 121 | + |
| 122 | +# OR with custom notes |
| 123 | +gh release create v2.0.0 \ |
| 124 | + --title "v2.0.0 - Backpack Architecture + Telemetry + Serialization" \ |
| 125 | + --notes-file CHANGELOG.md |
| 126 | +``` |
| 127 | + |
| 128 | +**Option B: Using GitHub UI** |
| 129 | + |
| 130 | +1. Go to: `https://github.com/pyrotank41/Backpackflow/releases/new` |
| 131 | +2. **Choose a tag:** Select `v2.0.0` (created by `npm version`) |
| 132 | +3. **Release title:** `v2.0.0 - [Release Name]` |
| 133 | +4. **Description:** Click "Generate release notes" or write custom notes |
| 134 | +5. Click **"Publish release"** |
| 135 | + |
| 136 | +#### Step 4: Watch the Magic ✨ |
| 137 | + |
| 138 | +Once you create the GitHub Release: |
| 139 | +1. Go to **Actions** tab: `https://github.com/pyrotank41/Backpackflow/actions` |
| 140 | +2. Watch "Publish to npm" workflow run |
| 141 | +3. All 237 tests will run |
| 142 | +4. Package will build and publish to npm |
| 143 | +5. Done! 🎉 |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +### Quick Reference |
| 148 | + |
| 149 | +```bash |
| 150 | +# Complete release in 3 commands: |
| 151 | +npm version major -m "chore: release v%s" |
| 152 | +git push origin main --follow-tags |
| 153 | +gh release create v2.0.0 --generate-notes |
| 154 | + |
| 155 | +# Package is now live on npm! 🚀 |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## ✅ Pre-Release Checklist |
| 161 | + |
| 162 | +Before creating a release, make sure: |
| 163 | + |
| 164 | +- [ ] All tests passing locally (`npm test`) |
| 165 | +- [ ] Build succeeds (`npm run build`) |
| 166 | +- [ ] Version in `package.json` updated |
| 167 | +- [ ] CHANGELOG.md updated with changes |
| 168 | +- [ ] Documentation updated (if needed) |
| 169 | +- [ ] All PRs merged to main |
| 170 | +- [ ] CI passing on main branch |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## 🛡️ Safety Features |
| 175 | + |
| 176 | +The workflow includes multiple safety checks: |
| 177 | + |
| 178 | +### 1. **Tests Must Pass** |
| 179 | +- All 237 tests run before publishing |
| 180 | +- If any test fails, publish is aborted |
| 181 | + |
| 182 | +### 2. **Version Validation** |
| 183 | +- Workflow verifies `package.json` version matches release tag |
| 184 | +- Example: `package.json` has `2.0.0` → Release tag must be `v2.0.0` |
| 185 | +- Prevents accidental version mismatches |
| 186 | + |
| 187 | +### 3. **Build Verification** |
| 188 | +- Ensures TypeScript compilation succeeds |
| 189 | +- Verifies `dist/index.js` and `dist/index.d.ts` exist |
| 190 | +- Prevents publishing broken builds |
| 191 | + |
| 192 | +### 4. **Duplicate Detection** |
| 193 | +- Checks if version already exists on npm |
| 194 | +- Warns but doesn't block (allows re-releases if needed) |
| 195 | + |
| 196 | +### 5. **Provenance** |
| 197 | +- Uses `--provenance` flag for npm publish |
| 198 | +- Provides cryptographic proof of package origin |
| 199 | +- Increases trust and security |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +## 🚨 Troubleshooting |
| 204 | + |
| 205 | +### "Error: 401 Unauthorized" |
| 206 | +- **Cause:** npm token is invalid or expired |
| 207 | +- **Fix:** Regenerate npm token and update GitHub secret |
| 208 | + |
| 209 | +### "Version mismatch" |
| 210 | +- **Cause:** `package.json` version doesn't match release tag |
| 211 | +- **Fix:** Update `package.json` or delete and recreate the release with correct tag |
| 212 | + |
| 213 | +### "Tests failed" |
| 214 | +- **Cause:** One or more tests are failing |
| 215 | +- **Fix:** Run `npm test` locally, fix failing tests, commit, and push |
| 216 | + |
| 217 | +### "dist/index.js not found" |
| 218 | +- **Cause:** Build failed or `dist/` is in `.gitignore` |
| 219 | +- **Fix:** Ensure `npm run build` works locally and `dist/` is generated |
| 220 | + |
| 221 | +### "Already published" |
| 222 | +- **Cause:** Version already exists on npm |
| 223 | +- **Fix:** Bump version in `package.json` or unpublish old version (within 72h) |
| 224 | + |
| 225 | +--- |
| 226 | + |
| 227 | +## 📊 Workflow Logs |
| 228 | + |
| 229 | +To view detailed logs: |
| 230 | + |
| 231 | +1. Go to **Actions** tab in GitHub |
| 232 | +2. Click on the "Publish to npm" workflow run |
| 233 | +3. Click on the "publish" job |
| 234 | +4. Expand each step to see detailed output |
| 235 | + |
| 236 | +--- |
| 237 | + |
| 238 | +## 🎯 Version Numbering (Semantic Versioning) |
| 239 | + |
| 240 | +Follow [semver](https://semver.org/) for version numbers: |
| 241 | + |
| 242 | +- **Major (X.0.0):** Breaking changes |
| 243 | + - Example: `1.3.0` → `2.0.0` (v2.0 with Backpack Architecture) |
| 244 | +- **Minor (x.Y.0):** New features (backward compatible) |
| 245 | + - Example: `2.0.0` → `2.1.0` (new observability dashboard) |
| 246 | +- **Patch (x.y.Z):** Bug fixes |
| 247 | + - Example: `2.0.0` → `2.0.1` (fix memory leak) |
| 248 | + |
| 249 | +--- |
| 250 | + |
| 251 | +## 🔄 CI Workflow (Automatic Testing) |
| 252 | + |
| 253 | +The repository also has a CI workflow that runs on every PR and push to main: |
| 254 | + |
| 255 | +- **Triggers:** PRs, pushes to main |
| 256 | +- **Tests:** Runs on Node 18 and Node 20 |
| 257 | +- **Steps:** |
| 258 | + 1. Install dependencies |
| 259 | + 2. Run all tests |
| 260 | + 3. Build package |
| 261 | + 4. Verify build output |
| 262 | + 5. Check TypeScript compilation |
| 263 | + |
| 264 | +This ensures code quality before merging PRs! |
| 265 | + |
| 266 | +--- |
| 267 | + |
| 268 | +## 📚 Additional Resources |
| 269 | + |
| 270 | +- [npm Automation Tokens](https://docs.npmjs.com/about-access-tokens#automation-tokens) |
| 271 | +- [GitHub Actions: Publishing Node packages](https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages) |
| 272 | +- [npm Provenance](https://docs.npmjs.com/generating-provenance-statements) |
| 273 | +- [Semantic Versioning](https://semver.org/) |
| 274 | + |
| 275 | +--- |
| 276 | + |
| 277 | +## 🎉 Example: v2.0.0 Release |
| 278 | + |
| 279 | +Here's a complete example of releasing v2.0.0: |
| 280 | + |
| 281 | +```bash |
| 282 | +# 1. Make sure you're on main and up to date |
| 283 | +git checkout main |
| 284 | +git pull origin main |
| 285 | + |
| 286 | +# 2. Run tests locally (optional but recommended) |
| 287 | +npm test # All 237 tests should pass! |
| 288 | + |
| 289 | +# 3. Update CHANGELOG.md (optional but recommended) |
| 290 | +# Add your release notes to CHANGELOG.md |
| 291 | + |
| 292 | +# 4. Bump version (creates commit + tag automatically) |
| 293 | +npm version major -m "chore: release v%s" |
| 294 | +# Output: v2.0.0 |
| 295 | + |
| 296 | +# 5. Push to GitHub (including the new tag) |
| 297 | +git push origin main --follow-tags |
| 298 | + |
| 299 | +# 6. Create GitHub Release |
| 300 | +gh release create v2.0.0 \ |
| 301 | + --title "v2.0.0 - BackpackFlow Major Release" \ |
| 302 | + --notes " |
| 303 | +## 🎉 Major Release: BackpackFlow v2.0 |
| 304 | +
|
| 305 | +### New Features |
| 306 | +- ✅ **PRD-001:** Backpack Architecture (Git-like state management) |
| 307 | +- ✅ **PRD-002:** Telemetry System (Complete observability) |
| 308 | +- ✅ **PRD-003:** Serialization Bridge (Config-driven flows) |
| 309 | +
|
| 310 | +### Breaking Changes |
| 311 | +- Replaced SharedStore with Backpack |
| 312 | +- New BackpackNode base class required |
| 313 | +- Config schema updated to 2.0.0 |
| 314 | +
|
| 315 | +### Stats |
| 316 | +- 237 tests passing |
| 317 | +- ~4,550 lines of production code |
| 318 | +- Complete TypeScript rewrite |
| 319 | +
|
| 320 | +See [CHANGELOG.md](./CHANGELOG.md) for full details. |
| 321 | +" |
| 322 | + |
| 323 | +# 7. Watch the workflow run: |
| 324 | +# https://github.com/pyrotank41/Backpackflow/actions |
| 325 | + |
| 326 | +# 8. Verify publication: |
| 327 | +# https://www.npmjs.com/package/backpackflow |
| 328 | +``` |
| 329 | + |
| 330 | +**Timeline:** |
| 331 | +- Step 1-6: ~2 minutes (manual) |
| 332 | +- Step 7: ~3-5 minutes (automated: tests, build, publish) |
| 333 | +- **Total:** ~5-7 minutes from start to npm! 🚀 |
| 334 | + |
| 335 | +--- |
| 336 | + |
| 337 | +**Questions?** Open an issue or reach out to @pyrotank41 on GitHub! |
| 338 | + |
0 commit comments