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}"
    ]]
])
相关推荐
好学且牛逼的马1 小时前
【工具配置|docker】
运维·docker·容器
运维小贺2 小时前
Kubernetes之Deployment无状态控制器
云原生·容器·kubernetes
yeflx4 小时前
解决Ubuntu22.04宿主机docker容器中nvidia-smi偶发失效问题
运维·docker·容器
吉吉616 小时前
Docker拉取镜像解决办法
运维·docker·容器
宋情写9 小时前
docker-compose安装Redis
redis·docker·容器
lisanmengmeng9 小时前
cephfs 在k8s挂载文档
云原生·容器·kubernetes
装不满的克莱因瓶10 小时前
【2026最新 架构环境安装篇三】Docker安装RabbitMQ4.x详细教程
linux·运维·docker·容器·架构·rabbitmq
YongCheng_Liang14 小时前
Docker 核心概念与价值详解:从理论到实战安装指南
运维·docker·容器
会算数的⑨14 小时前
K8S 学习笔记——核心概念与工作机制(二)
笔记·学习·kubernetes
运维成长记14 小时前
关于k8s和harbor脚本安装的一系列操作
云原生·容器·kubernetes