Skip to content

run npm publish after git push - #1387

Closed
bchenSyd wants to merge 1 commit into
lerna:2.xfrom
bchenSyd:issue#1385
Closed

bchenSyd wants to merge 1 commit into
lerna:2.xfrom
bchenSyd:issue#1385

Conversation

@bchenSyd

@bchenSyd bchenSyd commented Apr 21, 2018 •

Copy link
Copy Markdown
Contributor

this PR is to address 'double publish' issue. i.e. while one developer commits a change and is running lerna publish, another developer commits a new change and starts running lerna publish as well.
At the moment both lerna publish will fail but packages will be published, rendering the project into a chaotic state. An realistic example is illustrated in #1385

To solve this issue, ideally, we should wrap npm publish and git push with tags into an transaction and make it atomic. By putting git push ahead of npm publish, in the case where lerna publish is not running based on latest code, git push will fail and npm publish won't be run, and therefore atomicity is better reflected.

I believe this will have a huge benefit in production use of lerna

@jezzay

jezzay commented Apr 23, 2018

Copy link
Copy Markdown
Contributor

FYI @bochen2014 in lerna 3.x a check has been added to prevent publishing when behind the git remote: https://github.com/lerna/lerna/blob/master/commands/publish/index.js#L101

This sounds like it solves your issue in CI?

@evocateur

Copy link
Copy Markdown
Member

Yeah, I don't want to fundamentally change the order of operations in a maintenance branch. Probably gonna switch lerna 3.x to be latest in npm soon, anyway.

@bchenSyd

Copy link
Copy Markdown
Contributor Author

If you check before npm publish, there is still a chance that thing goes wrong. What if new commits come in after your check, but during you npm publish. I know npm publish is parallel,but it can still take a while and that give window for errors.

I still believe git push should happen before npm publish. Is there a reason why it’s not designed that way?

@evocateur

Copy link
Copy Markdown
Member

When an npm publish fails, now your public git history is at odds with what is present in the registry. Failed npm publish means you can go into the local directories and fix the problem. Once git is public, it's less tractable.

@bchenSyd

bchenSyd commented Apr 23, 2018 •

Copy link
Copy Markdown
Contributor Author

You are talking about all npm publish fail. What if 3 passed but 2 failed. Once published, you can’t unpublish them, git is not public,so next build would fail.

Also, what if new commits come in during npm publish? We have 45 packages in our project so I am a bit concerned

@evocateur

Copy link
Copy Markdown
Member

The answer is to only run one build at a time for branches that push commits back to origin. I don't know how else to contain the insanity, even pushing before npm publish is still liable to hit race conditions in such a chaotic situation.

@bchenSyd

bchenSyd commented Apr 23, 2018 •

Copy link
Copy Markdown
Contributor Author
  1. fair enough. I agree that the issue can be solved by having a git check latest plus one build at a time. The drawback is that we need extra configuration on build server, as by default Bamboo allows multi (4 by default I think). We need to document it somewhere so that developers would know, as most build servers allow multiple builds at a time by default

  2. I don't think put git push before npm publish has the same race condition. git push will make sure only the build that has latest code can proceed; all others get knocked back. git push is atomic by itself, meaning even 2 build try to git push at the same time, only one will succeed. I can't see a reason why we shouldn't do git push first

@bchenSyd bchenSyd closed this Apr 23, 2018
@bchenSyd bchenSyd reopened this Apr 23, 2018
@bchenSyd

Copy link
Copy Markdown
Contributor Author

Sorry, actually one build a time won’t solve the issue. If new commits in during npm publish, both build will fail

@bchenSyd

bchenSyd commented Apr 23, 2018 •

Copy link
Copy Markdown
Contributor Author

The first build will fail because it doesn’t have latest code(even it pass check before)
and therefor failed push. Public git history is at odds with npm anyway.

The second build would also fail because can’t publish the same npm package version again

It’s not an issue of how many build to run at a time, it’s an issue of when do your commit. I.e if the build server is publishing, you can’t commit as it has just passed latest check, and think it has got the latest code

@bchenSyd

Copy link
Copy Markdown
Contributor Author

semantic-release push right after commit/tag;
they recommend run --post-success "npm publish"

so, git push will happen before npm publish

>>> Your release branch is: master
>> No SemVer tag found. It seems like your first release? Initial release will be set to v1.0.0 as per npmjs specification.
>>> Bump type is minor
>>> update version on package.json...
v1.0.0
>>> About to write/append contents to CHANGELOG.md...
>>> About to add and commit package.json and CHANGELOG...
[master 1012a2d] chore(release): v1.0.0 [ci skip] ***NO_CI***
 2 files changed, 11 insertions(+), 1 deletion(-)
 create mode 100644 CHANGELOG.md
>> Time to create the Semantic Tag: v1.0.0
>>...and push to remote...
To github.com:bochen2014/semantic-release-test.git
   ac903c8..1012a2d  master -> master
To github.com:bochen2014/semantic-release-test.git
 * [new tag]         v1.0.0 -> v1.0.0
Done in 15.30s.

@evocateur

Copy link
Copy Markdown
Member

I'm not sure what is relevant about your semantic-release example? It just skipped publishing? I'm not an expert on the tool, but I was under the impression semantic-release only pushes tags, not a generated commit, so it's not an apples-to-apples comparison.

@bchenSyd

bchenSyd commented Apr 24, 2018 •

Copy link
Copy Markdown
Contributor Author

thanks for your reply.
the point I was trying to make by semantic-release 's example is that, semantic-release does git commit and git push together; it then runs your post-success script, which in many cases is npm publish

so in all, semantic-release does git push before npm publish

@bchenSyd

Copy link
Copy Markdown
Contributor Author

think about this, what if there is a commit merged in ( just merge in, not build ), right after lerna doing this line
https://github.com/lerna/lerna/blob/master/commands/publish/index.js#L101

what will happen?
and we don't have control over when developer will merge in their code

@bchenSyd

Copy link
Copy Markdown
Contributor Author

this problem is critical to us as it has happened twice so far (in 2 weeks time), the result is chaotic as we can't unpublished npm packages - I had to force publish twice, but the version info won't be liner.

if this issue won't be fixed in lerna@3, i'm afraid that we will have to host our own version of lerna.

@evocateur

evocateur commented Apr 24, 2018 •

Copy link
Copy Markdown
Member

We always have control over what commits are generated in CI. You can always do lerna publish --canary to avoid generating conflicting commits. Not ideal, I'll grant you, but it's what we have right now.

By the way, semantic-release seems to run its npm plugin before its github plugin during the publish phase, so it's not clear to me that your assertion is correct?

@bchenSyd

Copy link
Copy Markdown
Contributor Author

"We always have control over what commits are generated in CI."

I don't think we are talking about the same thing.
I'm talking about in a team develop environment, where multiple developers can check in or merge into master at anytime. If right after lerna runs this line

if (isBehindUpstream(this.gitRemote, this.currentBranch, this.execOpts)) {

another developer push some changes into master. what will happen?

I don't think this issue is related to lerna's commit message

@evocateur

evocateur commented Apr 24, 2018 via email

Copy link
Copy Markdown
Member

@bchenSyd

Copy link
Copy Markdown
Contributor Author

Thanks for your advice, really appreciate it.

Are you talking about lerna 2.x or 3.x?

@bchenSyd

bchenSyd commented Apr 25, 2018 •

Copy link
Copy Markdown
Contributor Author

She’ll I create a PR in 3.x swapping npm publish and git push?

I think I have explained enough, and it’s worth putting git push before npm publish

@evocateur

evocateur commented Apr 25, 2018 via email

Copy link
Copy Markdown
Member

@bchenSyd

Copy link
Copy Markdown
Contributor Author

thanks you so much, really appreciate your help!

I have already bumped lerna to 3.x in our code base, as we recently noticed lerna publish in 2.x returns success (exit code 0) even when it fails to push. This caused a CI build marked as passed, but really should be failed. This is a defect that we can't afford in production.

So, thanks for your offer but since we don't use lerna 2.x anymore, there is no need to create a new dist-tag.

@evocateur

Copy link
Copy Markdown
Member

Thanks again for your patience @bochen2014, but I don't think we're going to land this on 2.x, it's a breaking change that I can't afford to make on what is in reality a maintenance branch. Let me know if lerna 3 needs changes to make this happen (I'm still working on the split of publish into version and publish phases...).

Can you open a new issue about the incorrect exit code after a push failure, please? We should really figure that out, it's not ideal.

@evocateur evocateur closed this May 1, 2018
@bchenSyd

bchenSyd commented May 2, 2018

Copy link
Copy Markdown
Contributor Author

thanks for your reply

  1. git push should happen before npm publish , i thought we have reached agreement on this? in 3.x there is a isBehindUpstream check but that is insufficient because new commits could be pushed right after that check. So, yes , we do need this in 3.x as an important change
  2. in our repository setup, we disallow direct commit to master. Every commit into master much has a PR first, but we forgot to create an exception for CI. As a result, once CI bumps the versions and tries to push commits & tags back, it failed. however that build was marked as green. After this I immediately bumped to 3.x and can confirm this has been fixed in 3.x

@evocateur

Copy link
Copy Markdown
Member

Yes, we agreed that git push should happen before npm publish. That would be part of the new lerna version command I'm working on, extracted from the current lerna publish.

In the near future, calling lerna publish will automatically call lerna version when necessary, which effectively reorders git push before npm publish.

@lock

lock Bot commented Dec 27, 2018

Copy link
Copy Markdown

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.

@lock lock Bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants