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}"
    ]]
])
相关推荐
无聊的老谢2 分钟前
Spring Cloud Alibaba 应用的容器化部署与 K8s 编排
云原生·容器·kubernetes
liux35288 分钟前
Namespace 多租户隔离:K8s 资源管理的基石
docker·容器·kubernetes
程序员酥皮蛋10 小时前
docker基础
docker·容器·eureka
没有退路那我就不要散步13 小时前
kube-proxy优化
docker·容器·kubernetes
Java 码思客15 小时前
【ElasticSearch从入门到架构师】第3章:ES 核心基础概念(架构师必备底层认知)
大数据·elasticsearch·jenkins
丑过三八线16 小时前
Kubernetes 常用命令速查手册
云原生·容器·kubernetes
bloglin9999917 小时前
docker镜像构建及部署样例
运维·docker·容器
SLD_Allen17 小时前
基于docker搭建sub2api图文教程
运维·docker·容器
X1A0RAN17 小时前
Jenkins流水线日志彩色打印输出
运维·jenkins
木雷坞20 小时前
LiteLLM Docker 部署:config.yaml、Master Key 和 Postgres 配置
运维·docker·容器·litellm