【问题标题】:Why does this "complicated" git clone/checkout cause gitversion to fail?为什么这个“复杂”的 git clone/checkout 会导致 gitversion 失败?
【发布时间】:2022-02-01 22:35:15
【问题描述】:

我在 Jenkinsfile 中有以下管道语法代码来克隆 git repo:

      checkout([$class: \'GitSCM\',
                branches: [[name: \"${branch}\"]],
                doGenerateSubmoduleConfigurations: false,
                extensions: [[$class: \'SubmoduleOption\',
                              disableSubmodules: false,
                              parentCredentials: false,
                              recursiveSubmodules: true,
                              reference: \'\',
                              trackingSubmodules: false],
                             [$class: \'LocalBranch\',
                              localBranch: \"\"],
                             [$class: \'RelativeTargetDirectory\',
                              relativeTargetDir: relative_dir]],
                submoduleCfg: [],
                userRemoteConfigs: [[credentialsId: cred_id,
                                     url: git_url]]])

以下是此 Jenkins 作业的一个实例的输出,它揭示了用于克隆 repo 的 git 命令:

 > git init /home/user/workspace/CICD/PRJ/repo/PRJ/repo/121/src # timeout=10
Fetching upstream changes from ssh://git@bitbucket.company.com:7999/PRJ/repo.git
 > git --version # timeout=10
using GIT_SSH to set credentials user - ssh username with private key. 
 > git fetch --tags --progress -- ssh://git@bitbucket.company.com:7999/PRJ/repo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url ssh://git@bitbucket.company.com:7999/PRJ/repo.git # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url ssh://git@bitbucket.company.com:7999/PRJ/repo.git # timeout=10
Fetching upstream changes from ssh://git@bitbucket.company.com:7999/PRJ/repo.git
using GIT_SSH to set credentials user - ssh username with private key. 
 > git fetch --tags --progress -- ssh://git@bitbucket.company.com:7999/PRJ/repo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/feature/hcf^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/refs/heads/feature/hcf^{commit} # timeout=10
Checking out Revision 37b9492373951ab0b4c70fa64a3320be58133e0e (refs/remotes/origin/feature/hcf)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 37b9492373951ab0b4c70fa64a3320be58133e0e # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git checkout -b feature/hcf 37b9492373951ab0b4c70fa64a3320be58133e0e # timeout=10
Commit message: \"Uncomment set_fault\"
 > git rev-list --no-walk 59ce67929be6c901975596db509de7a72b3c557a # timeout=10

同样的 Jenkinsfile 使用 gitversion 自动应用 git 标签,如下所示:

docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v \"$(pwd):/repo\" artifactory.company.com/gittools/gitversion:5.7.1 /repo

上述 git 克隆和使用 gitversion 生成新的 git 标签的方法已经运行了几个月,没有发生任何事故。

问题
今天,gitversion 在运行此 Jenkins 作业以构建一个我看不到任何独特之处的 git 分支时意外失败(与许多成功构建的分支相比)。
下面的错误似乎表明无法识别开发或发布分支,在我们的例子中,它们分别是 devmaster

+ docker run -u 1172002866:1172000513 --rm -v /home/user/workspace/CICD/PRJ/repo/PRJ/repo/121/src:/repo artifactory.company.com/gittools/gitversion:5.7.1 /repo
INFO [01/26/22 23:30:14:02] Working directory: /repo
INFO [01/26/22 23:30:14:26] Project root is: /repo/
INFO [01/26/22 23:30:14:26] DotGit directory is: /repo/.git
INFO [01/26/22 23:30:14:59] Begin: Loading version variables from disk cache
  INFO [01/26/22 23:30:14:59] Cache file /repo/.git/gitversion_cache/AF7963E4F79300A465EFC56DBA14B2B679307C08.yml not found.
  INFO [01/26/22 23:30:14:59] End: Loading version variables from disk cache (Took: 1.50ms)
  INFO [01/26/22 23:30:14:61] Using latest commit on specified branch
  INFO [01/26/22 23:30:14:62] Begin: Attempting to inherit branch configuration from parent branch
    INFO [01/26/22 23:30:14:65] Begin: Finding branch source of \'feature/hcf\'
    INFO [01/26/22 23:30:14:66] End: Finding branch source of \'feature/hcf\' (Took: 15.87ms)
    INFO [01/26/22 23:30:14:67] Begin: Getting branches containing the commit \'37b9492\'.
      INFO [01/26/22 23:30:14:67] Trying to find direct branches.
      INFO [01/26/22 23:30:14:67] No direct branches found, searching through all branches.
      INFO [01/26/22 23:30:14:67] Searching for commits reachable from \'origin/dev\'.
      INFO [01/26/22 23:30:14:69] The branch \'origin/dev\' has no matching commits.
      INFO [01/26/22 23:30:14:69] Searching for commits reachable from \'origin/master\'.
      INFO [01/26/22 23:30:14:70] The branch \'origin/master\' has no matching commits.
      INFO [01/26/22 23:30:14:70] Searching for commits reachable from \'origin/release-npm2\'.
      INFO [01/26/22 23:30:14:70] The branch \'origin/release-npm2\' has no matching commits.
      INFO [01/26/22 23:30:14:70] Searching for commits reachable from \'origin/release-npm3\'.
      INFO [01/26/22 23:30:14:71] The branch \'origin/release-npm3\' has no matching commits.
      INFO [01/26/22 23:30:14:71] Searching for commits reachable from \'origin/release-5944.0\'.
      INFO [01/26/22 23:30:14:72] The branch \'origin/release-5944.0\' has no matching commits.
      INFO [01/26/22 23:30:14:72] Searching for commits reachable from \'origin/release-5944.1\'.
      INFO [01/26/22 23:30:14:73] The branch \'origin/release-5944.1\' has no matching commits.
    INFO [01/26/22 23:30:14:73] End: Getting branches containing the commit \'37b9492\'. (Took: 63.52ms)
    INFO [01/26/22 23:30:14:73] Found possible parent branches: 
  INFO [01/26/22 23:30:14:74] End: Attempting to inherit branch configuration from parent branch (Took: 116.47ms)
  ERROR [01/26/22 23:30:14:82] An unexpected error occurred:
System.InvalidOperationException: Gitversion could not determine which branch to treat as the development branch (default is \'develop\') nor releaseable branch (default is \'main\' or \'master\'), either locally or remotely. Ensure the local clone and checkout match the requirements or considering using \'GitVersion Dynamic Repositories\'
   at GitVersion.Configuration.BranchConfigurationCalculator.InheritBranchConfiguration(IBranch targetBranch, BranchConfig branchConfiguration, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\Configuration\\BranchConfigurationCalculator.cs:line 137
   at GitVersion.Configuration.BranchConfigurationCalculator.GetBranchConfiguration(IBranch targetBranch, ICommit currentCommit, Config configuration, IList`1 excludedInheritBranches) in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\Configuration\\BranchConfigurationCalculator.cs:line 47
   at GitVersion.GitVersionContextFactory.Create(GitVersionOptions gitVersionOptions) in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\Core\\GitVersionContextFactory.cs:line 40
   at GitVersion.GitVersionCoreModule.<>c__DisplayClass0_0.<RegisterTypes>b__1() in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\GitVersionCoreModule.cs:line 38
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at GitVersion.VersionCalculation.NextVersionCalculator.get_context() in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\VersionCalculation\\NextVersionCalculator.cs:line 17
   at GitVersion.VersionCalculation.NextVersionCalculator.FindVersion() in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\VersionCalculation\\NextVersionCalculator.cs:line 32
   at GitVersion.GitVersionCalculateTool.CalculateVersionVariables() in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.Core\\Core\\GitVersionCalculateTool.cs:line 52
   at GitVersion.GitVersionExecutor.RunGitVersionTool(GitVersionOptions gitVersionOptions) in D:\\a\\GitVersion\\GitVersion\\src\\GitVersion.App\\GitVersionExecutor.cs:line 70
  INFO [01/26/22 23:30:14:82] Attempting to show the current git graph (please include in issue): 
  INFO [01/26/22 23:30:14:82] Showing max of 100 commits
  INFO [01/26/22 23:30:14:93] * 37b9492 3 minutes ago  (HEAD -> feature/hcf, origin/feature/hcf)

我试过的
当我手动克隆 repo 并使用上述 git 命令检查分支时,我可以重现 gitversion 失败。

当我“简化”我的 git 克隆和分支结帐时,只需:

$ git clone ssh://git@bitbucket.company.com:7999/PRJ/repo.git
$ git checkout feature/hcf
$ docker run -u $(id -u ${USER}):$(id -g ${USER}) --rm -v \"$(pwd):/repo\" artifactory.company.com/gittools/gitversion:5.7.1 /repo

...然后gitversion 成功并返回预期值。

问题
可以修改 Jenkinsfile checkout() 语句,使其在功能上与“简单”git clonegit checkout 语句相同吗?从 git 克隆和检查与gitversion 相关的分支这两种“方法”之间显然存在一些显着差异。
如果可能的话,我更喜欢使用 Jenkins 管道 checkout() 函数的解决方案,而不是像 sh(script:\"git clone...\") 这样的 shell 脚本调用——但也许有经验的回答者可以说服我这种偏好是不合理的。

有人能解释一下我如何确定这个特定的 git repo 或分支的“独特”是什么吗?对于其他存储库/分支,我们 Jenkinsfile 中的上述 checkout() 语句继续与随后的 gitversion 调用一起工作。因此,我不知道如何确定这个失败案例似乎有一些独特之处。

  • gitversion 是否要求分支在本地存在?如果是这样,您的 Jenkins 输出表明 masterdevelopment 都不作为本地分支存在(仅作为远程跟踪)

标签: jenkins jenkins-pipeline git-clone git-checkout gitversion


【解决方案1】:

我自己不使用 Jenkins,但它的存储库克隆中似乎缺少 develop 分支,这可能表明 Jenkins 正在执行 sparseshallow 克隆。如GitVersion's requirements 中所述,它需要完整的存储库克隆才能进行计算。

git clone &amp;&amp; git checkout 语句起作用的原因是,默认情况下,Git 会执行存储库的完整克隆。您需要做的是指示 Jenkins 对存储库进行非浅层克隆。 GitVersion's Jenkins documentation 列出了一些您可以并且应该做的事情,以使 GitVersion 正常工作:

  • 高级克隆行为
    • 启用Fetch tags
    • 启用Honor refspec on intial clone
  • 签出到匹配的本地分支
  • 修剪陈旧的远程跟踪分支
  • 指定参考规范
    • 参考规格:+refs/heads/*:refs/remotes/@{remote}/*

正确调整上述 Jenkins 设置后,它应该可以工作。

【讨论】:

    猜你喜欢
    • 2010-11-03
    • 2015-09-06
    • 2011-11-10
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    相关资源
    最近更新 更多