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}"
    ]]
])
相关推荐
IT古董8 小时前
Windows 11 专业版 安装与配置 Docker Desktop 保姆级手册(包成功永久免关注免VIP)
windows·docker·容器
Red丶哞10 小时前
Docker 安装部署Prometheus
linux·云原生·容器·kubernetes
紫神10 小时前
kubeedge安装并接入摄像头操作说明
云原生·kubernetes·edge
吃饭最爱11 小时前
Elasticsearch的用法
运维·jenkins
风无雨12 小时前
windows docker 配置镜像
运维·docker·容器
java_logo13 小时前
NGINX WEBUI Docker 容器化部署指南
运维·nginx·docker·容器·centos·rabbitmq·运维开发
运维 小白13 小时前
k8s 部署MySQL主从集群(一主两从)1.0
mysql·容器·kubernetes
ζั͡山 ั͡有扶苏 ั͡✾14 小时前
完善EKF可观测性体系:基于ElastAlert2构建k8s智能钉钉日志告警系统
容器·kubernetes·钉钉·kibana·filebeat·日志监控
i小杨14 小时前
Docker 相关使用收录
docker·容器·eureka
猪在黑魔纹里14 小时前
docker run hello-world失败、报错
linux·docker·容器