Reputation: 608
I have two branches, master and turtles, with turtles being ahead of master by one commit: 'I like turtles'.
In GitLab I have the following .yml
file, which runs whenever a Merge Request is created, or updated by pushing the branch to merge:
update-doc:
stage: deploy
script:
- echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
- 'echo $(git log --abbrev-commit remotes/origin/master)'
- 'echo $(git log --abbrev-commit remotes/origin/master..remotes/origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})'
- 'echo $(git cherry -v remotes/origin/master remotes/origin/turtles --abbrev=1)'
only:
- merge_requests
Running git log --abbrev-commit remotes/origin/master..remotes/origin/turtles
or git cherry -v remotes/origin/master remotes/origin/turtles
in Git Bash on my Windows machine and on the Linux VM where we are hosting GitLab returns the commit message 'I like turtles', as expected. But when the .yml
file runs it cannot find the branch remotes/origin/turtles
and I get the following output:
$ echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
turtles
$ echo $(git log --abbrev-commit remotes/origin/master)
8406e4d Update .gitlab-ci.yml
$ echo $(git log --abbrev-commit remotes/origin/master..remotes/origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})
fatal: ambiguous argument 'remotes/origin/master..remotes/origin/turtles': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]'
$ echo $(git cherry -v remotes/origin/master remotes/origin/turtles --abbrev=1)
fatal: Unknown commit remotes/origin/turtles
So GitLab clearly knows that there is the turtles branch as it's in the $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
variable, but can't seem to resolve the remotes/origin/turtles. I've tried without the remotes/origin/ part too but still no luck.
How can I get the GitLab runner to recognise that remote path of the merge request branch? Or is there another git command I could try that shows just the commits on the turtles branch?
Upvotes: 3
Views: 1797
Reputation: 34426
In GitLab CI/CD, the default strategy for checking out code is to fetch the current branch with a shallow merge, e.g. git fetch --depth 50 origin $CI_COMMIT_BRANCH
. This explains why you see only the one branch.
You can fix this:
GIT_DEPTH: 0
in your .gitlab-ci.yml
to disable shallow clones, orgit fetch origin master
Refer to the docs on shallow cloning.
Upvotes: 7