一般情況下,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']])