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}"
    ]]
])
相关推荐
架构师老Y9 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
handsomestWei12 小时前
Docker引擎API接入配置
运维·http·docker·容器·api
键盘鼓手苏苏15 小时前
Kubernetes与GitOps高级实践
云原生·kubernetes·k8
不是书本的小明15 小时前
K8S应用优化方向
网络·容器·kubernetes
andeyeluguo17 小时前
docker总结
运维·docker·容器
SuAluvfy17 小时前
从 0 到 1:在 Windows + Docker 环境下搭建 NextChat 并接入多模型 API(踩坑实录)
docker·容器
九英里路17 小时前
cpp容器——string模拟实现
java·前端·数据结构·c++·算法·容器·字符串
尘世壹俗人18 小时前
知识点8---虚拟化编排工具Kubernetes
容器·kubernetes
冷色系里的一抹暖调19 小时前
OpenClaw Docker 部署避坑指南:服务启动成功但网页打不开?
人工智能·windows·docker·ai·容器·opencode
@土豆20 小时前
acme.sh 自动解析、申请并续签免费泛域名证书(Docker容器部署+腾讯云DNSPod)
docker·容器·腾讯云