如何在脚本化管道中指定结帐(scm)的深度

时间:2019-10-03 06:12:24

标签: jenkins jenkins-pipeline jenkins-groovy

我们的项目存储库很大(2.5GB)。因此,在脚本化管道中的checkout(scm)步骤中,从GIT克隆代码需要更长的时间。并且由于GIT试图获取整个历史记录,我们面临以下错误。

到目前为止,我已经尝试使用checkout(scm),我读了https://jenkins.io/doc/pipeline/steps/workflow-scm-step/ 有一个名为depth的选项,通过它我们可以仅下载最近的提交。

但是我不知道它的语法。

node(nodeName) {
    try {
    deleteDir()
    checkout(scm)
        ....
        ....
    }
    catch(Exception ex) {
        throw ex
    }    
}

如果克隆时间减少了,那将是非常有益的。 执行行结帐(scm)后,

我们有时会出现以下错误。

using credential Servicejenkins_build
Cloning the remote Git repository
Cloning with configured refspecs honoured and without tags
Cloning repository 
hudson.plugins.git.GitException: Command "C:\Git\cmd\git fetch --no-tags --progress https://gitlab.com/../../supportforpc.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: remote: Enumerating objects: 1           
remote: Enumerating objects: 24671, done.        
remote: Counting objects:   0% (1/24671)           
remote: Counting objects:   1% (247/24671)           
remote: Counting objects:   2% (494/24671)           
remote: Counting objects:   3% (741/24671)           
remote: Counting objects:   4% (987/24671)           
remote: Counting objects:   5% (1234/24671)           
remote: Counting objects:   6% (1481/24671)  
.....
....
Counting objects:   100% (24671/24671) 
remote: Compressing objects:   0% (1/10279)           
remote: Compressing objects:   1% (103/10279)           
remote: Compressing objects:   2% (206/10279)           
remote: Compressing objects:   3% (309/10279)           
remote: Compressing objects:   4% (412/10279)           
remote: Compressing objects:   5% (514/10279)           
remote: Compressing objects:   6% (617/10279)           
remote: Compressing objects:   7% (720/10279)           
remote: Compressing objects:   8% (823/10279)           
remote: Compressing objects:   9% (926/10279)           
remote: Compressing objects:  10% (1028/10279) 
....
....
remote: Compressing objects:  100% (10279/10279) 
Receiving objects:   0% (1/24671)   
Receiving objects:   1% (247/24671)   
Receiving objects:   2% (494/24671)   
Receiving objects:   3% (741/24671)   
Receiving objects:   4% (987/24671)   
Receiving objects:   5% (1234/24671)   
Receiving objects:   6% (1481/24671)
....
....
Receiving objects:   43%

    fatal: index-pack failed
    error: RPC failed; curl 56 SSL read: 
    error:00000000:lib(0):func(0):reason(0), errno 10054

因此,我认为在checkout(scm)中使用深度1可能会解决此问题。但是我不知道脚本化管道中的语法。

1 个答案:

答案 0 :(得分:2)

您可以在结帐之前在scm对象中启用浅表克隆(无历史记录,并且仅提取最新的提交):

node(nodeName) {
    try {
    deleteDir()
    scm.extensions << [$class: 'CloneOption', shallow: true]
    checkout(scm)
        ....
        ....
    }
    catch(Exception ex) {
        throw ex
    }    
}

但是,我建议您建立并维护一个参考存储库,该存储库的数量级更快:

  1. 在终端窗口中,将存储库克隆到镜像中。该存储库将仅包含git对象:

    $ git clone --mirror https://gitlab.com/../../supportforpc.git
    Cloning into bare repository 'supportforpc.git'...
    remote: Enumerating objects: 6578, done.
    remote: Counting objects: 100% (6578/6578), done.
    remote: Compressing objects: 100% (1561/1561), done.
    remote: Total 739260 (delta 5791), reused 5046 (delta 5013), pack-reused 732682
    Receiving objects: 100% (739260/739260), 3.49 GiB | 3.78 MiB/s, done.
    Resolving deltas: 100% (562236/562236), done.
    
  2. 在Jenkins中创建一个新作业,以定期更新镜像存储库。更新镜像时,仅使用fetch命令:

    sh "git fetch --all --prune"
    
  3. 告诉scm对象以使用镜像存储库作为参考。读取引用后,将查询远程存储库是否有新提交,因此您不必担心总是将镜像保持最新:

    node(nodeName) {
        try {
        deleteDir()
        scm.extensions << [$class: 'CloneOption', reference: "<your-server>:git/<where-you-put-the-mirror-repo>"]
        checkout(scm)
            ....
            ....
        }
        catch(Exception ex) {
            throw ex
        }    
    }
    

完成该设置后,您将看到在短短几秒钟内克隆了整个存储库,并且保留了所有存储库的历史记录。如果您是从步骤1中创建的镜像存储库中手动克隆,则可以自己测试克隆。

$ git clone <mirror-repository-directory> <some-dir>`
Cloning into '<some-dir>'...
done.
Checking out files: 100% (15055/15055), done.

有关修改默认scm对象的说明:如果您不想更改它,则可以为结帐创建一个新对象,如this answer所示:

checkout([
    $class: 'GitSCM',
    branches: scm.branches,
    doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
    extensions: scm.extensions + [$class: 'CloneOption', reference: "<your-server>:git/<where-you-put-the-mirror-repo>"],
    userRemoteConfigs: scm.userRemoteConfigs
])