前置
通常,在 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'
]]
])
}
}
上面
标签:git,url,Git,检出,folder,stage,class,Jenkinsfile From: https://www.cnblogs.com/astrofeyx/p/jenkins-parallel-checkout-multiple-git-repositories.ht