Digging
I got this error when doing github PR check (in npm run build
process):
1 2 3 4 | > nx affected:build --parallel .../node_modules/annwl/workspace/node_modules/yargs/yargs.js:1109 Error: Command failed: git merge-base master HEAD |
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:
1 2 3 | Run git merge master fatal: refusing to merge unrelated histories ##[error] Process completed with exit code 128. |
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
:
1 2 3 4 | # Number of commits to fetch. 0 indicates all history for all branches and tags. # Default: 1 fetch-depth: '' |
I thought to try with 0 (all history) is a good idea, and it worked.
Resolve and conclusion
In .github/workflows/build.yml
1 2 3 4 5 6 | - name: 'Checkout' uses: actions/checkout@v2 with: ref: master fetch-depth: 0 |
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.