Investigation and solving of CI build error (at `git merge-base`)

Digging

I got this error when doing github PR check (in npm run build process):

And asked in group, Herb said he met with this error every time when the PR branch not just being merged with master

That’s weird, merging does not require the branch syncing with the latest version of master

After looking at github workflow script, there is no git merge-base master HEAD code inside, and it’s happening inside npm run build procedure, so I search and found that nx build would execute this merge-base command

Trying

This command actually has not problem, I tired it on my computer, it works well

Is it because CI env had no master branch checked to local? I tried to add a git checkout master but it still failed, and I found master already checked out in actions/checkout@v2 action.

What if I do the merging before build?

I added git merge-base master HEAD before build, But it exited with code 254 without error messages

That’s not very helpful

Day 2 trying

What about normal merge?

I added git merge master before build, it exited with error message:

Weird, it’s in the same repo, but git said unrelated histories

This information is crucial, I search with it and got:

https://github.community/t/check-diff-when-using-actions-checkout/17765

When use Checkout@v2 12, only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth to fetch more history.

Oh, that’s helpful, maybe there is no commits history other than the latest one and git cannot find the common parent node of PR branch and master branch

And with the document of actions/checkout@v2:

I thought to try with 0 (all history) is a good idea, and it worked.

Resolve and conclusion

In .github/workflows/build.yml

last line solved this problem

Why? actions/checkout@v2 only fetched latest one commit info by default, so when merging, git can only check latest commit of master is the parent of PR branch or not, so if their common parent is a little earlier, it just fails with no more commits info of master. And now I fetched all commits on master, so git merge can check one by one from latest to very first commit, cause PR branch is from master, so it will find the common parent node eventually, and perform the merging successfully.

发表评论

电子邮件地址不会被公开。 必填项已用*标注