使用 Jenkins 管道检查同一个作业的多个 Git 回购

在我的 Jenkins 工作中,我使用 Jenkins 多 SCM 插件将三个 git 存储库签出到三个子目录中。然后,我执行一组命令来构建一组工件,其中包含来自所有三个存储库的信息和代码。

多个 SCM 现在被折旧,并且文本建议移动到管道。我试过了,但我不知道该怎么办。

下面是我感兴趣的 Jenkins 工作目录顶层的目录结构:

$ ls
Combination
CombinationBuilder
CombinationResults

这三个子目录中的每一个都有一个检出的 git 回购。对于多 SCM,我使用 git,然后添加“ checkout to a subdirectory”行为。下面是我对一个流水线脚本的尝试:

node('ATLAS && Linux') {
sh('[ -e CalibrationResults ] || mkdir CalibrationResults')
sh('cd CalibrationResults')
git url: 'https://github.com/AtlasBID/CalibrationResults.git'
sh('cd ..')
sh('[ -e Combination ] || mkdir Combination')
sh('cd Combination')
git url: 'https://github.com/AtlasBID/Combination.git'
sh('cd ..')
sh('[ -e CombinationBuilder ] || mkdir CombinationBuilder')
sh('cd CombinationBuilder')
git url: 'https://github.com/AtlasBID/CombinationBuilder.git'
sh 'cd ..'


sh('ls')
sh('. CombinationBuilder/build.sh')
}

然而,git 命令似乎是在工作区的顶级目录中执行的(这有一定的意义) ,而且根据语法,似乎不存在 checkout-to-sub 目录行为。

127332 次浏览

You can use the dir command to execute a pipeline step in a subdirectory:

node('ATLAS && Linux') {
dir('CalibrationResults') {
git url: 'https://github.com/AtlasBID/CalibrationResults.git'
}
dir('Combination') {
git url: 'https://github.com/AtlasBID/Combination.git'
}
dir('CombinationBuilder') {
git url: 'https://github.com/AtlasBID/CombinationBuilder.git'
}


sh('ls')
sh('. CombinationBuilder/build.sh')
}

If your repository has submodules use git checkout

pipeline {
agent {label 'master'}
stages{
stage("Demo"){
steps{


echo 'Hello World'
}
}
stage("Source"){
parallel{
stage('CalibrationResults'){
steps{
echo 'Checking out CalibrationResults'
checkout([$class: 'GitSCM', branches: [[name: '*/CI-CD-Demo']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 0, noTags: true, reference: '', shallow: false, timeout: 60], [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', timeout: 60, trackingSubmodules: true], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'server-core'],[$class: 'CheckoutOption', timeout: 60]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/AtlasBID/CalibrationResults.git']]])
}
}
stage('Combination'){


steps{
echo 'Checking out server spoke'
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'CloneOption', depth: 0, noTags: true, reference: '', shallow: false, timeout: 60], [$class: 'SubmoduleOption', disableSubmodules: false, parentCredentials: false, recursiveSubmodules: true, reference: '', timeout: 60, trackingSubmodules: true], [$class: 'RelativeTargetDirectory', relativeTargetDir: 'server-spoke'], [$class: 'CheckoutOption', timeout: 60]], submoduleCfg: [], userRemoteConfigs: [[url: 'https://github.com/AtlasBID/CombinationBuilder.git']]])




}
}
}


}


}
}

Generated using Checkout git snippet generator

You can checkout those three git repositories into three subdirectories by using the checkout SCM step three times like this:

stage('Checkout') {
// Get CalibrationResults from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'CalibrationResults']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/CalibrationResults.git']]
])
// Get Combination from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'Combination']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/Combination.git']]
])
// Get CombinationBuilder from GitHub
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'CombinationBuilder']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '6463627-ab54-4e42-bc29-123458', url: 'https://github.com/AtlasBID/CombinationBuilder.git']]
])


}

Here's Mine

    stage('CheckoutModule1') {
steps {
sh 'mkdir -p Module1'
dir("Module1")
{
git branch: "develop",
credentialsId: 'aaa',
url: 'git@a.com:b/module1.git'
}
}
}


stage('CheckoutModule2') {
steps {
sh 'mkdir -p Module2'
dir("Module2")
{
git branch: "develop",
credentialsId: 'aaa',
url: 'git@a.com:b/module2.git'
}
}
}

You can just add a shell step that does the cloning and moves it to a subdirectory of your choice.

git clone https://$bitbucketUsername:$bitbucketPassword@<yourbitbucketserver>/scm/projectname/reponame1.git
mv reponame1 new_subdir_name1


git clone https://$bitbucketUsername:$bitbucketPassword@<yourbitbucketserver>/scm/projectname/reponame2.git
mv reponame2 new_subdir_name2