Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sanrai
Copy link
Collaborator

@sanrai sanrai commented Sep 4, 2025

Fix Webpack Bundle Version Stamping in CI/CD Pipeline

Background & Root Cause

Currently, our Webpack builds (using BannerPlugin) read the version straight from package.json at build time. However, our CI/CD release pipeline uses release-it, which runs Webpack before it bumps and tags package.json. As a result, every published bundle is stamped with the previous version rather than the newly released version—an off‐by‐one version mismatch in the banner.

Root Cause:

  1. BannerPlugin pulls package.json.version when Webpack runs
  2. release-it (via npm run release) does:
    • Determine next version (e.g. 0.35.4 → 0.35.5)
    • Update package.json
    • Commit and tag that change
    • Push and create GitHub Release
  3. But our pipeline did Webpack before the version bump/tag, so banners carried the old version

Solution Overview

Modify BannerPlugin to read from git describe --tags (with an optional RELEASE_TAG override).

  • Locally: git describe --tags --abbrev=0 always returns the latest annotated tag (e.g. v0.35.4)
  • In CI: After release-it tags v0.35.5, BannerPlugin's git describe call sees exactly that new tag

Hook into release-it so that Webpack builds after the new tag is created. We use the after:git:tag hook to run:

npm run build && git add dist && git commit --amend --no-edit

This means:

  1. release-it bumps & tags (e.g. 0.35.5)
  2. Our hook then runs npm run build (BannerPlugin sees 0.35.5)
  3. We stage the freshly built dist/ and amend the same "chore: release v0.35.5" commit to include dist/
  4. Finally, release-it pushes that amended commit and tag to GitHub and creates the Release—so dist/ is always stamped with the correct version

What Changed

webpack.config.js

Before: BannerPlugin read from packageJson.version

After:

const { execSync } = require('child_process');
// …other imports…

const date = new Date().toISOString().slice(0, 10);
// Try RELEASE_TAG env var first; fallback to latest Git tag.
const version =
  process.env.RELEASE_TAG ||
  execSync('git describe --tags --abbrev=0').toString().trim();

module.exports = {
  // …existing config…
  plugins: [
    new webpack.BannerPlugin({
      banner: `Chimera UI Libraries - Build ${version} (${date})`,
      entryOnly: true,
    }),
    // …other plugins…
  ],
};

Rationale: By using git describe --tags, Webpack always picks up the latest Git tag (e.g. v0.35.5), so banners cannot be off by one.

package.json

Before: release-it ran Webpack in CI before tagging

After: Under "release-it""hooks", we added:

"after:git:tag": "npm run build && git add dist && git commit --amend --no-edit"

Rationale:

  • Once release-it has created the new tag (e.g. 0.35.5), our hook triggers a rebuild
  • Now BannerPlugin's git describe --tags returns the just‐created tag, guaranteeing the new version is baked into dist/
  • We git commit --amend so that package.json, CHANGELOG.md, and the rebuilt dist/ all live in a single commit (chore: release v0.35.5). release-it then pushes and publishes normally

How to Test

1. Local BannerPlugin Check (without running npm run release)

Ensure you have at least one Git tag. If none exist, create a lightweight tag:

git tag v0.35.4
git push --tags

Run a plain build:

npm run build

Verify the top lines of dist/main.js show:

/*! Chimera UI Libraries - Build 0.35.4 (YYYY-MM-DD) */

Override via RELEASE_TAG to simulate an upcoming release:

RELEASE_TAG=0.35.5 npm run build

Check dist/main.js again—banner should read "Build 0.35.5 …".

2. Dry‐run release-it Locally

Create a dummy commit so there's something new to release:

touch DUMMY.md
git add DUMMY.md
git commit -m "chore: test release-it integration"

Run:

npx release-it --ci --no-push --no-git.requireUpstream

Expected output sequence:

  1. Bump from 0.35.4 → 0.35.5
  2. Create tag v0.35.5
  3. Run npm run build (BannerPlugin now sees v0.35.5)
  4. git add dist
  5. git commit --amend --no-edit (amend "chore: release v0.35.5" commit to include dist/)

Final local state: one commit tagged 0.35.5 containing both the bumped package.json and built dist/.

Validate:

git log --oneline -n 2
git tag | grep 0.35.5
git show 0.35.5:dist/main.js | head -n 5

Confirm banner reads 0.35.5 in dist/main.js.

3. CI Verification

  1. Merge this branch into main
  2. Let the GitHub Action (on push: main) run npm run release. With GITHUB_TOKEN set in Secrets:
    • release-it will bump → tag → run our hook → rebuild → amend → push → publish
  3. After CI finishes, inspect the GitHub Release (e.g. v0.35.6). Download or view the published dist/main.js from that release—banner must say "Build 0.35.6" (or whichever version CI picked)

Why We Chose This Approach

Simplicity

  • We continue using release-it (no major tooling changes)
  • Hooks are a built‐in release-it feature; no external shell scripts or manual steps

Reliability

  • git describe --tags always returns the exact tag that was just created, so no off‐by‐one issues
  • Amending the single "chore: release vX.Y.Z" commit ensures package.json, CHANGELOG.md, and dist/ stay in sync

Consistency (Local vs. CI)

  • Local developers can still run npm run build and see the latest Git tag in the banner
  • CI will stamp the bundles with the new version automatically as part of the release flow

Checklist

  • BannerPlugin now reads version from Git (with RELEASE_TAG override)
  • release-it configured to build/commit dist/ after creating the release tag
  • Manual local test (npm run build, git show 0.35.5:dist/main.js) passed
  • Dry-run of npx release-it --ci --no-push showed correct behavior (bump, tag, rebuild, amend)
  • CI run on main confirms new release is published with correctly stamped dist assets

Please let me know if you need any further tweaks or clarifications!

@github-actions
Copy link

github-actions bot commented Sep 4, 2025

Core Web Vitals Metrics

Metric Value
LCP N/A s
FID N/A ms
CLS N/A

Recorded at: 2025-09-04T18:36:03.354Z
PR: #319

@sanrai sanrai changed the title fix(mwpw-123): stamp builds with correct version tag and release-it build(mwpw-179848): stamp builds with correct version tag and release-it Sep 4, 2025
@sanrai sanrai merged commit 0c9e03a into main Sep 4, 2025
19 checks passed
@github-actions
Copy link

github-actions bot commented Sep 4, 2025

Core Web Vitals Metrics

Metric Value
LCP N/A s
FID N/A ms
CLS N/A

Recorded at: 2025-09-04T18:46:30.841Z
PR: #319

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