Jenkinsfile 同時檢出多個 Git 倉庫
前置
通常,在 Jenkinsfile 中使用 Git 倉庫是這樣的:
stage('Checkout git repo') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repo-url'
]]
])
}
}
以上寫法的結果是遠程 Git 倉庫中所有內容都被檢出到當前工作目錄下,因此我們才能像下面這樣(不用切換目錄而)直接運行一些命令:
stage('generate version number') {
steps {
VERSION_NUBMER = sh(script: "git describe --always", returnStdout: true).trim()
}
}
將 Git 倉庫檢出到指定文件夾
如果要同時拉取多個 Git 倉庫,面臨的首要問題是如何將 Git 倉庫檢出到指定文件夾。對此,可以通過使用 $class: 'RelativeTargetDirectory'
解決,具體程式碼如下:
stage('Checkout git repo') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repo-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repo-folder'
]]
])
}
}
這樣一來,遠程 Git 倉庫中所有內容將被檢出到 ./repo-folder
文件夾下;後續我們若想在 Git 項目目錄下操作,就需要先 cd ./repo-folder
了,例如:
stage('generate version number') {
steps {
VERSION_NUBMER = sh(script: "cd ./repo-folder && git describe --always", returnStdout: true).trim()
}
}
檢出多 Git 倉庫
在解決了將 Git 倉庫檢出到指定文件夾這一問題後,檢出多 Git 倉庫這一需求就稍顯容易,下面以檢出repoA、repoB、repoC三個倉庫為例:
stage('Checkout multiple git repos') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoA-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoA-folder'
]]
])
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoB-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoB-folder'
]]
])
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoC-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoC-folder'
]]
])
}
}
上面👆程式碼的結果就是工作目錄下有了 repoA-folder
、repoB-folder
、repoC-folder
三個文件夾。
同時
截止現在,還沒有達到我們的最終目的。本文標題叫做「Jenkinsfile 同時檢出多個 Git 倉庫」,但現在我們僅僅做到了「多個」還沒有做到「同時」,為此我們需要使用Jenkins Pipeline語法中的 parallel
關鍵字。
stage('Checkout multiple git repos') {
parallel {
stage('repoA') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoA-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoA-folder'
]]
])
}
}
stage('repoB') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoB-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoB-folder'
]]
])
}
}
stage('repoC') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: 'develop']],
userRemoteConfigs: [[
credentialsId: 'ssh-private-key-id',
url: 'project-git-repoC-url'
]],
extensions: [[
$class: 'RelativeTargetDirectory',
relativeTargetDir: 'repoC-folder'
]]
])
}
}
}
}