jenkins 如何在脚本管道中指定检出深度(SCM)

klsxnrf1  于 2023-06-28  发布在  Jenkins
关注(0)|答案(3)|浏览(160)

我们的项目存储库非常大(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
    }    
}

如果克隆次数减少,那将是非常有益的。在执行行 checkout (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可能会解决这个问题。但我不知道脚本管道中的语法。

pxq42qpu

pxq42qpu1#

你可以在 checkout 之前在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.

1.在Jenkins中创建一个新作业,定期更新镜像存储库。更新镜像时仅使用fetch命令:

sh "git fetch --all --prune"

1.告诉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
])
mbyulnm0

mbyulnm02#

checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/branchname'], [$class: 'CloneOption', depth: 1, noTags: true, reference: '', shallow: true]], userRemoteConfigs: [[credentialsId: 'credentialId', url: 'git@repourl']]]

credentialId可以通过点击http://yourjenkinsinstall/credentials找到

irtuqstp

irtuqstp3#

根据towel的回答,你也可以通过在扩展名CloneOption中添加参数noTags来避免git仓库中的所有标签,这里是checkout对象的外观:

checkout([
    $class: 'GitSCM', 
    branches: [[name: '*/master']], 
    extensions: [[$class: 'CloneOption', noTags: true, shallow: true, depth: 1, timeout: 30]],
    userRemoteConfigs: [[credentialsId: 'your-credentials', url: 'your-repository']]
])

以下是文档:
noTags:boolean取消选择此选项将执行不带标记的克隆,当您只想访问refspec指定的内容时,可以节省时间和磁盘空间。
友情链接:https://www.jenkins.io/doc/pipeline/steps/params/gitscm/

相关问题