kubernetes jenkins pipeline优化拉取大仓库性能指定分支+深度

有时候我们历史的git仓库,提交了某个比较大的文件如果不限制 depth ,就会拉取所有的历史提交记录,这样在历史仓库比较大的时候 clone 非常之慢,而实际上我们只需要最新的代码来构建就行了,为了优化性能,我们可以配置指定拉取:

关键配置:

复制代码
        stage('Check code') {
            echo("gitCommitId value is: ${gitCommitId}")
            // sh "sleep 10000"
            def scmVars = checkout([
                    $class: 'GitSCM',
                    branches: [[name: "${gitCommitId}"]],
                    extensions: [
                            [$class: 'CloneOption', depth: 1, shallow: true, noTags: true, honorRefspec: true, timeout: 3000]
                    ],
                    userRemoteConfigs: [[
                                                credentialsId: "${gitlabCredential}",
                                                url: "${gitlabURL}",
                                                refspec: "+refs/heads/${gitCommitId}:refs/remotes/origin/${gitCommitId}"
                                        ]]
            ])
            commitHash = scmVars.GIT_COMMIT.take(8)
            echo("${commitHash}")
        }

上面的拉取方式只适合拉取分支的,如果还要兼容commitId,需要下面的配置:

复制代码
// 判断是分支名还是 commit hash
def isCommitHash = gitCommitId ==~ /^[a-f0-9]{7,40}$/

def scmVars = checkout([
    $class: 'GitSCM',
    branches: [[name: isCommitHash ? "${gitCommitId}" : "origin/${gitCommitId}"]],
    extensions: [
        [$class: 'CloneOption', depth: 1, shallow: true, noTags: true, honorRefspec: true, timeout: 3000]
    ],
    userRemoteConfigs: [[
        credentialsId: "${gitlabCredential}",
        url: "${gitlabURL}",
        refspec: isCommitHash ? 
            "+${gitCommitId}:refs/remotes/origin/target-commit" : 
            "+refs/heads/${gitCommitId}:refs/remotes/origin/${gitCommitId}"
    ]]
])
相关推荐
落日漫游4 小时前
K8s资源管理:高效管控CPU与内存
java·开发语言·kubernetes
小薛博客6 小时前
22、Jenkins容器化部署Java应用
java·运维·jenkins
ZLRRLZ6 小时前
【Docker】Docker基础
运维·docker·容器
ggaofeng7 小时前
k8s网络原理
网络·容器·kubernetes
观测云21 小时前
Jenkins 可观测最佳实践
jenkins
衍余未了21 小时前
centos9 docker启动不起来,docker启动发生堵塞问题!
运维·docker·容器
一念一花一世界1 天前
DevOps实战(2) - 使用Arbess+GitPuk+Docker实现Java项目自动化部署
jenkins·tiklab·arbess·开源cicd工具·docker构建部署
我真的是大笨蛋1 天前
K8S-基础架构
笔记·云原生·容器·kubernetes
tianyuanwo1 天前
Docker Registry 实现原理、适用场景、常用操作及搭建详解
运维·docker·容器·registry
木二_1 天前
附050.Kubernetes Karmada Helm部署联邦及使用
容器·kubernetes·karmada·kubernetes联邦·集群联邦