首页 > 解决方案 > Git 仅在 GitLab 中记录模棱两可的参数

问题描述

我有两个分支,master 和 turtles,其中海龟领先于 master 一个提交:“我喜欢海龟”。
在 GitLab 中,我有以下.yml文件,它在创建合并请求时运行,或者通过推送分支合并来更新:

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

正如预期的那样,在我的 Windows 机器和托管 GitLab 的 Linux VM 上运行git log --abbrev-commit remotes/origin/master..remotes/origin/turtles或在 Git Bash 中返回提交消息“我喜欢海龟”。git cherry -v remotes/origin/master remotes/origin/turtles但是当.yml文件运行时它找不到分支remotes/origin/turtles,我得到以下输出:

$ echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
海龟
$ echo $(git log --abbrev-commit remotes/origin/master)
8406e4d 更新 .gitlab-ci.yml
$ echo $(git log --abbrev-commit remotes/origin/master..remotes/ origin/${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME})
致命:不明确的参数 'remotes/origin/master..remotes/origin/turtles':未知修订版或路径不在工作树中。使用 '--' 将路径与修订分开,如下所示: 'git [...] -- [...]'
$ echo $(git cherry -v remotes/origin/master remotes/origin/turtles --abbrev =1)
致命:未知的提交远程/来源/海龟

所以 GitLab 清楚地知道$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME变量中有 turtles 分支,但似乎无法解析 remotes/origin/turtles。我也尝试过不使用 remotes/origin/ 部分,但仍然没有运气。

如何让 GitLab 运行器识别合并请求分支的远程路径?或者是否有另一个 git 命令我可以尝试只显示海龟分支上的提交?

标签: gitgitlabgitlab-ci

解决方案


在 GitLab CI/CD 中,检出代码的默认策略是使用浅合并获取当前分支,例如git fetch --depth 50 origin $CI_COMMIT_BRANCH. 这解释了为什么您只看到一个分支。

你可以解决这个问题:

  • 通过在 Web UI 中将“Git shallow clone”设置为 0,或者
  • 通过设置GIT_DEPTH: 0.gitlab-ci.yml禁用浅克隆,或
  • 通过拉/取您需要的任何其他分支,例如git fetch origin master

请参阅有关浅层克隆的文档


推荐阅读