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}"
    ]]
])
相关推荐
tb_first1 小时前
k8sday09
linux·云原生·容器·kubernetes
是小崔啊2 小时前
【Jenkins】01 - Jenkins安装
运维·jenkins
稚辉君.MCA_P8_Java2 小时前
豆包 Java的23种设计模式
java·linux·jvm·设计模式·kubernetes
天上掉下来个程小白4 小时前
Docker-14.项目部署-DockerCompose
运维·docker·微服务·容器
星霜笔记7 小时前
Docker 部署 MariaDB+phpMyAdmin+Nextcloud 完整教程
运维·数据库·docker·容器·mariadb
数据知道10 小时前
容器化部署:用Docker封装机器翻译模型与服务详解
docker·容器·机器翻译
40kuai16 小时前
kubernetes中数据存储etcd
容器·kubernetes·etcd
Harvey_D18 小时前
【部署K8S集群】 1、安装前环境准备配置
云原生·容器·kubernetes
伊成18 小时前
Docker 部署 Nginx 完整指南
nginx·docker·容器
江湖有缘1 天前
【Docker项目实战】使用Docker部署Notepad轻量级记事本
docker·容器·notepad++