簡體   English   中英

使用 Jenkins 管道將多個 git 存儲庫檢出到同一個作業中

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

我正在使用 Jenkins 多 SCM 插件將三個 git 存儲庫檢出到我的 Jenkins 作業的三個子目錄中。 然后我執行一組命令來構建一組工件,其中包含從所有三個存儲庫中提取的信息和代碼。

多 SCM 現在已貶值,文中建議移至管道。 我試過了,但我不知道如何使它工作。

這是我有興趣從 Jenkins 作業目錄的頂層看到的目錄結構:

$ ls
Combination
CombinationBuilder
CombinationResults

這三個子目錄中的每一個都有一個檢出的 git 存儲庫。 對於多 SCM,我使用了 git,然后添加了“結帳到子目錄”行為。 這是我對管道腳本的嘗試:

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 命令似乎在工作區的頂級目錄中執行(這有點道理),並且根據語法,似乎也沒有結帳到子目錄的行為。

您可以使用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')
}

您可以通過使用 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']]
        ])

}

這是我的

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

如果您的存儲庫有子模塊,請使用 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']]])


                }
            }
        }

    }

}
}

使用 Checkout git 代碼段生成器生成

您只需添加一個 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