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}"
    ]]
])
相关推荐
AAA小肥杨1 天前
基于k8s的Python的分布式深度学习训练平台搭建简单实践
人工智能·分布式·python·ai·kubernetes·gpu
xiaogg36781 天前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
2503_930123931 天前
Kubernetes (四)网络插件详解:Flannel 与 Calico 的原理、数据流向与实战对比
网络·容器·kubernetes
K_i1341 天前
K8s 运维三大核心难题:DNS 故障、有状态存储、AI 赋能 SRE 解决方案
kubernetes
远向光1 天前
k8s中的控制器
linux·容器·kubernetes
半梦半醒*1 天前
搭建Jenkins
linux·运维·centos·tomcat·jenkins·运维开发
安卓开发者1 天前
Docker常用镜像使用指南:从入门到实战
运维·docker·容器
霖.241 天前
Docker常见问题
服务器·docker·云原生·容器
荣光波比1 天前
K8S(十七)—— Kubernetes集群可视化工具Kuboard部署与实践指南
云原生·容器·kubernetes
深蓝电商API1 天前
爬虫+Docker:让你的爬虫项目一键部署、可移植
爬虫·docker·容器