Repository navigation
Description
introduction
We have a monorepo project comprised of 5 packages. We are using lerna to manage package versions, with independent strategy. Overall, apart from one particular big surprise, the experience with lerna is good.
our pipeline setup
5 developers work on different features, once their work is finished, code is merged into master, which will trigger a build in Bamboo. The bamboo build server will build the code, bump the version of changed packages and publish them into a private npm registry.
the problem
the problem happens when people merge their code into master while there is an active build in progress.
For example, lets say my project has [email protected], [email protected] and [email protected] to begin with. Here is what happened (in time order)
- developer A merged his code into master which contains
chore(A)andfeat(B). Bamboo picked it up and starts build#1; - while build#1 is under way, developer B merged his code which contains
feat(C)andchore(A). This triggers another build#2. Now we have both build#1 and build#2 running at the same time; - build#1 bumped packages to
[email protected]and[email protected]; and successfully published them into npm registry. But when it tries togit pushall thosepackage.jsonfiles andchangelog.mds back, it failed because it doesn't contains latest change(due to developer B 's merge). So build#1 failed with[email protected]and[email protected]published into npm registry. No commits or tags were pushed to origin. - build#2 bumped packages to
[email protected],[email protected]and[email protected](note here thatBwill be picked up as well because build#1 didn't push the new tags. even if it did, build#2 may not be able to see it due to timing condition). It successfully published[email protected]but failed on publishing[email protected]because that package already exists. So build#2 also failed with[email protected]published. No commits or tags were pushed to origin.
Now, imagine you sit in front a dev machine and do a git pull. All your local package.json files are still stating [email protected], [email protected] and [email protected], no new git tags introduced, but all 3 npm packages have been published. What do you do? 😵 😵 😱 😱
possible solutions : disable concurrent builds in bamboo
you may think that by making bambo build sequential the problem may be solved. But it wont' help because build#1 still fails due to not having developer B 's merge. build#2 also will fail because build#1 didn't push its local commits&tags. so disabling concurrent build literally has no effect at all.
your solution
I do have a solution but it's not ideal and I would like to hear from others before I share it here.
I'm pretty sure i'm not alone, as it seems to be the standard use case of lerna. What is the official way of using lerna in a CI/CD pipeline??
Activity
[-]lerna in production[/-][+]lerna with CI/CD pipeline[/+]bchenSyd commentedon Apr 20, 2018
@evocateur
bchenSyd commentedon Apr 20, 2018
ok. I guess
canary releaseis the way to go.bchenSyd commentedon Apr 20, 2018
it's hard to understand the purpose of
canaryflag. in source code , it treatcanarysame as--skip-gitthis.gitEnabled = !(this.options.canary || this.options.skipGit);so it seems to be a step designed only for npm publish , which is fair enough.
but why do you bump the npm packages version (instead of just suffix with commit short sha1 ), and even default to a minor?
how am I supposed to link this npm package back to my git tag? what is purpose of this design?
right. I got it. it basically says "I'm a release candidate for the next minor version" and hence the bump
I'm not still not sure how I can link npm package version with repository version/tag; I suppose I can use npm tag; but I need more investigation
plus, the description is really misleading, as there is no tag created; the entire git is disabled for a
canarypublish; but the description still says it creates the new version tag by ..... bla bla.no. there is no new tags created. there is no new commit created. it's purely for
npm publishevocateur commentedon Apr 20, 2018
--npm-tagcontrols the npm packagedist-tag, not the annotated git tag (which is indeed skipped with--canary). It's an unfortunately-named option.--canarywas originally intended to be the "nightly" release, run in CI.lerna publishwas not originally designed to be run in CI, but it has gained various features over the years. It's certainly the direction that the industry wants to go.Instead of rewriting
lerna publishto be more CI-friendly, I would rather figure out a way to integrate lerna-like versioning decisions intosemantic-release, to be honest. It's got a very robust architecture, it's just missing a few of the git calculations and whatnot.bchenSyd commentedon Apr 21, 2018
thanks @evocateur for your reply. Here is my understanding
semantic-releaseis not able to manage monorepos and therefore won't be consideredsemantic-releaseis able to handle concurrent publish eitherthe fundamental difference between
lernaandsemantic-releasewhen speaking to version bump is lerna usesgit tagpluspackages.jsonto calculate new version whilesemantic-releaseusenpm info; its really just a matter of where to read version info from. If we usesemantic-release, build#1 will still fail due to not able to push commit back, build#2 is highly likely (not guaranteed though) succeed, but the end result will be missing changelog from build#1.I think the issue can be easily fixed by putting
git pushahead ofnpm publishin lerna. I created #1387 for it, can you please have a look? thank again.bchenSyd commentedon Apr 21, 2018
we were quite shocked to know that lerna factor in work tree changes when doing comparison, and that might not be intended.
#1388 solve the issue. please take a look as well
bdwain commentedon Jul 13, 2018
@bochen2014 we solved this issue by only allowing people to merge to master using a jenkins job, and the jenkins job also auto-rebases the branch onto master before merging it (failing if there is a conflict)
feat: Split `lerna version` from of `lerna publish` (#1522)
feat: Split `lerna version` from of `lerna publish` (lerna#1522)
lock commentedon Dec 27, 2018
This thread has been automatically locked because there has not been any recent activity after it was closed. Please open a new issue for related bugs.