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}"
    ]]
])
相关推荐
别骂我h26 分钟前
Kubernetes服务发布基础
云原生·容器·kubernetes
weixin_399380692 小时前
k8s一键部署tongweb企业版7049m6(by why+lqw)
java·linux·运维·服务器·云原生·容器·kubernetes
IT成长日记2 小时前
【Docker基础】Docker数据卷管理:docker volume ls及其参数详解
运维·docker·容器·volume ls
偷萧逸苦茶2 小时前
docker常见命令
docker·容器·eureka
绝不偷吃6 小时前
ELK日志分析系统
运维·elk·jenkins
高山莫衣16 小时前
Docker Desktop导致存储空间不足时的解决方案
docker·容器·eureka
鹏大师运维16 小时前
在银河麒麟V10 SP1上手动安装与配置高版本Docker的完整指南
linux·运维·docker·容器·麒麟·统信uos·中科方德
Ahlson16 小时前
【fnNAS】docker的nginx配置html
nginx·docker·容器·fnnas
LuckyLay16 小时前
Compose 常用命令详解——AI教你学Docker
docker·容器·eureka
moppol17 小时前
容器化 vs 虚拟机:什么时候该用 Docker?什么时候必须用 VM?
运维·docker·容器