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}"
    ]]
])
相关推荐
惜.己19 小时前
安装windows版本docker(配置镜像源+拉取运行docker)
运维·docker·容器
极客小云20 小时前
【2026年Docker国内镜像源最新可用清单与配置指南】
运维·docker·容器
忍冬行者21 小时前
kubernetes安装traefik Gateway API,应对Ingress NGINX停止维护
云原生·kubernetes·云计算
java_logo21 小时前
Caddy Docker 容器化部署指南
运维·docker·容器·caddy部署·caddy部署文档·caddy部署教程·docker部署caddy
-指短琴长-1 天前
Docker-Desktop修改WSL文件系统到D盘
docker·容器·eureka
ydswin1 天前
Sidecar不就是在Pod里多跑一个容器吗!
linux·kubernetes
回忆是昨天里的海1 天前
docker网络-自定义网络
运维·docker·容器
飞Link1 天前
【开发工具】Docker常用操作
运维·docker·容器
运维行者_1 天前
网络流量分析入门:从流量监控与 netflow 看懂核心作用
运维·开发语言·网络·云原生·容器·kubernetes·php
hanyi_qwe1 天前
Docker 容器操作 【docker (二)】
docker·容器·eureka