git describe --tags 不适用于 Jenkins 管道构建

git describe --tags does not work on Jenkins pipeline build

我看到 this question 的方向相似,但不完全相同。问题是标签没有正确推送。

我目前正在使用 Jenkins 使用 setuptools_scm 构建我的 python 项目,它基本上使用 git describe 来获得(或多或少)合理的版本号,即使不在一个标签。管道是使用 JobDSL 中的种子作业定义的,如下所示(缩写以获取要点):

import BuildProjects

BuildProjects.build_projects.each { def bproject ->
   multibranchPipelineJob("tool/${bproject}") {
      branchSources {
         branchSource {
            source {
               git {
                  id('tool_${bproject}')
                  remote("${BuildProjects.scmBaseLocation}/${bproject}.git")
                  credentialsId(BuildProjects.scmUser)
                  traits {
                     gitBranchDiscovery()
                     gitTagDiscovery()
                     cloneOptionTrait {
                        extension {
                           shallow(false)
                           noTags(false)
                           reference('grmblwrx')
                           timeout(1)
                        }
                     }
                  }
               }
            }
         }
      }

   }
}

一些配置值取自BuildProjects

当 运行 jenkins 时,我看到它获取标签:

 > git fetch --tags --progress -- ssh://git@git.my-company.net/tools.git +refs/heads/*:refs/remotes/origin/* # timeout=1

事实上,当我使用

在我的 Jenkins 文件中输出标签时,我可以看到它们
sh "git tag"

块。但是使用

sh "git describe --tags"

给予

fatal: No tags can describe '<Commit hash>'.
Try --always, or create some tags.

我在某处读到这可能是由于签出稀疏造成的:标记和当前 HEAD 之间的提交可能丢失。仔细检查后,我在日志中发现

> git config core.sparsecheckout # timeout=10
> git checkout -f <Commit hash> # timeout=10

紧接在上面显示的 git fetch 行之后。似乎不知何故我在 JobDSL 中的配置没有得到尊重。有什么想法吗?

如果为真,自 2018 git 插件 3.4.0 以来,Jenkins 管道默认使用窄引用规范克隆,并且没有标记。
allegro/axion-release-plugin issue 195, and documented in allegro/axion-release-plugin PR 198 中对此进行了说明。

raster-foundry/raster-foundry PR 4233, check your checkout scm step and its cloneOptions 所示:

node {
  try {
    // Checkout the proper revision into the workspace.
    stage('checkout') {
      checkout([
        $class: 'GitSCM',
        branches: scm.branches,
        extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
        userRemoteConfigs: scm.userRemoteConfigs
      ])
    }
    ...

尝试 noTags: false, reference: '', shallow: false,看看问题是否仍然存在。

自从我发布问题后,我们更新了 Jenkins 实例和一些插件,尤其是 git 插件。出于某种原因,现在 "simply works"。我无法不费吹灰之力地重现旧设置,因此我无法轻易找出导致 "fix".

的原因

将 Jenkins 从 2.176.2 和 git 插件 3.11.0 更新到 Jenkins 2.289.1 和 git 插件 4.7.2 后,我遇到了同样的问题。

在管理 Jenkins->配置系统->Git 插件中,启用选项“在结帐期间保留第二次提取”

这恢复了旧的 git 插件行为,并且“git describe”再次开始工作。