如何在 Jenkins 中 git fetch --unshallow ?

Ala*_*Dea 5 git github jenkins jenkins-pipeline

我正在将 SonarQube 集成到 Jenkins 中。我们目前使用浅克隆,因为考虑到历史上有大量二进制文件,我们的存储库完全克隆的速度很慢。SonarQube 需要运行git blame,这显然不适用于浅克隆。我需要一个解决方法,但我无法更改全局 Jenkins 配置(这会减慢所有构建速度)。

情况:

checkout scm无需参数即可工作。

sh 'git fetch --unshallow收到错误,因为它没有凭据。

我尝试过这个怪物来自定义结帐 scm 的参数:

checkout scm: [
    $class: 'GitSCM', userRemoteConfigs: [
        [url: env.repoURL, credentialsId: 'GitHubEnterprise']
    ], extensions: [
        [$class: 'CheckoutOption', timeout: 60],
        [$class: 'CloneOption', noTags: true,
            reference: '/var/lib/gitchcache/reference.git',
            shallow: false, timeout: 60]
    ], branches: [
        [name: branch]
    ]
], remoteName: "origin", poll: false, clearWorkspace: true
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

> git rev-parse PR-42^{commit} # timeout=10

Couldn't find any revision to build. Verify the repository
and branch configuration for this job.
Run Code Online (Sandbox Code Playgroud)

报告的问题似乎集中在未能提供“存储库名称”:

我试图构建一个拉取请求,但它没有更改的参考规范。我不知道为什么它不能推断出这一点,但事实并非如此。

以下是解决第一部分的方法:

checkout scm: [$class: 'GitSCM',
               userRemoteConfigs: [
                       [url: env.GIT_URL,
                        refspec: "+refs/pull/${prNumber}/head:refs/remotes/origin/${branch}",
                        credentialsId: 'GitHubEnterprise']
               ],
               extensions: [
                       [$class: 'CloneOption',
                        shallow: false,
                        timeout: 60]
               ],
               branches: [
                       [name: branch]
               ]
]
Run Code Online (Sandbox Code Playgroud)

结帐有效,构建有效,但我仍然得到浅结帐......

INFO: SCM provider for this project is: git
INFO: 1 files to be analyzed
WARN: Shallow clone detected, no blame information will be provided. You can convert to non-shallow with 'git fetch --unshallow'.
INFO: 0/1 files analyzed
WARN: Missing blame information for the following files:
WARN:   * src/main/java/com/example/Example.java
Run Code Online (Sandbox Code Playgroud)

这是我在 Jenkins 配置中的项目 - GitHub 组织下的内容: 高级克隆行为

这是我想要的特定构建步骤:checkout scm,但代码中的浅层“未经检查”。

如何在 Jenkins 中 git fetch --unshallow ?

Mar*_*tin 3

从版本 git-4.0.0-beta3 开始,这是不可能的 git jenkins 插件。源代码中没有任何支持或提及 的参数unshallowgit fetch

您可以做的就是保留 git jenkins 插件选项,如下所示:浅层首次获取,因此速度更快。

然后你有一些选择:

  • 在构建的执行 shell 中,您可以git直接使用而不是通过插件来运行非浅层过程。
  • 如果这会减慢太多构建速度,请在 SonarQube 进入工作区并运行 unshallow 过程之前执行预运行步骤。这可以通过 Groovy 脚本等来完成。