简体   繁体   English

使用 Jenkins 管道将多个 git 存储库检出到同一个作业中

[英]Using a Jenkins pipeline to checkout multiple git repos into same job

I'm using the Jenkins Multiple SCM plugin to check out three git repositories into three sub directories in my Jenkins job.我正在使用 Jenkins 多 SCM 插件将三个 git 存储库检出到我的 Jenkins 作业的三个子目录中。 I then execute one set of commands to build a single set of artifacts with information and code drawn from all three repositories.然后我执行一组命令来构建一组工件,其中包含从所有三个存储库中提取的信息和代码。

Multiple SCM is now depreciated, and the text recommends moving to pipelines.多 SCM 现在已贬值,文中建议移至管道。 I tried, but I can't figure out how to make it work.我试过了,但我不知道如何使它工作。

Here is the directory structure I'm interested in seeing from the top level of my Jenkins job directory:这是我有兴趣从 Jenkins 作业目录的顶层看到的目录结构:

$ ls
Combination
CombinationBuilder
CombinationResults

Each of those three sub-directories has a single git repo checked out.这三个子目录中的每一个都有一个检出的 git 存储库。 With the Multiple SCM, I used git, and then added the "checkout to a subdirectory" behavior.对于多 SCM,我使用了 git,然后添加了“结帐到子目录”行为。 Here was my attempt with a pipeline script:这是我对管道脚本的尝试:

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')
}

However, the git command seems to execute at the top level directory of the workspace (which makes some sense), and according to the syntax too, there doesn't seem to be the checkout-to-sub-directory behavior.但是, git 命令似乎在工作区的顶级目录中执行(这有点道理),并且根据语法,似乎也没有结帐到子目录的行为。

You can use the dir command to execute a pipeline step in a subdirectory:您可以使用dir命令在子目录中执行管道步骤:

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')
}

You can checkout those three git repositories into three subdirectories by using the checkout SCM step three times like this:您可以通过使用 checkout SCM 步骤三次,将这三个 git 存储库检出到三个子目录中,如下所示:

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'
            }
        }
    }

If your repository has submodules use git checkout如果您的存储库有子模块,请使用 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使用 Checkout git 代码段生成器生成

You can just add a shell step that does the cloning and moves it to a subdirectory of your choice.您只需添加一个 shell 步骤即可进行克隆并将其移动到您选择的子目录中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM