Jenkins下拉取大repo处理方法

发布时间 2024-01-08 15:26:55作者: Believer007

一般情况下,Jenkins使用pipeline中Checkout拉取代码最简单脚本如下:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: [[name: 'my-branch']],
                    userRemoteConfigs: [[url: 'https://github.com/user/repo.git']]
                ])
            }
        }
    }
}

默认情况下,会把repo上所有分支记录都会拉取下来。这时,很多人就会想到git 中depth参数,浅拉取,如下:

checkout scmGit(
    branches: [[name: '*/master']],
    extensions: [ cloneOption(shallow: true, depth: 1) ],
    userRemoteConfigs: [[url: 'https://github.com/user/repo.git']])

这个时候问题来了,发现在当前分支中,把大文件已经删除了。整个项目拉到下来后,文件还是这么大。查看之,大文件隐藏在 .git\objects\pack中

把这个.git去除,实际项目文件大小就是不包括大文件的大小。

如何解决呢。这时,看看jenkins consoleput 就明白了:

 这个过程会去fetch所有的记录,即使加了--depth=1. 这就导致把其它未删大文件的分支信息也会获取,这里就包括大文件。

如果解决?有两个方法:

方法一:彻底将大文件删除,这里网上有方法。https://www.jianshu.com/p/30ff7050bcea

方法二:在jenkins拉取代码时,指定refspec

如下:

checkout scmGit(
    branches: [[name: '*/master']],
    extensions: [ cloneOption(honorRefspec: true, shallow: true, depth:1) ],
    userRemoteConfigs: [[refspec: '+refs/heads/master:refs/remotes/origin/master',
        url: 'https://github.com/user/repo.git']])