簡體   English   中英

聲明式Jenkins管道中多個節點上的並行簽出

[英]Parallel checkout in declarative Jenkins pipeline on multiple nodes

我正在為從Gitlab觸發的CI構建開發聲明式Jenkins管道。 我現在所擁有的:


// variable definitions

pipeline {
    agent none

    parameters {
            string(defaultValue: 'develop',
                description: 'Commit ID or branch name to build', 
                name: 'branch', 
                trim: false)
    }

    stages {
        stage('Checkout') {
            parallel {
                stage ('Windows') {
                    agent {
                        label 'build' && 'windows'
                    }

                    steps {
                        script {

                            def checkout_ext = [[$class: 'CleanCheckout'],
                                                [$class: 'CleanBeforeCheckout']] // calls git clean -fdx and git reset --hard

                            if (env.gitlabActionType == "MERGE"){   
                                  checkout_ext.add([$class: 'PreBuildMerge', 
                                                    options: [ mergeRemote: "origin",
                                                              mergeTarget: "${env.gitlabTargetBranch}"]])
                            }
                        }

                        checkout([
                                $class: 'GitSCM', 
                                branches: [[name: "${params.branch}"]],
                                userRemoteConfigs: [[ url: "${git_url}",                                          credentialsId: "${git_credentials_id}" ]],
                                extensions:   checkout_ext 
                        ])
                    }
                }

                stage('Linux') {
                    agent {
                        label 'build' && 'linux'
                    }
                    steps {
                        script {

                            def checkout_ext = [[$class: 'CleanCheckout'], 
                                                [$class: 'CleanBeforeCheckout']] // calls git clean -fdx and git reset --hard

                            if (env.gitlabActionType == "MERGE"){   
                               checkout_ext.add([$class: 'PreBuildMerge', 
                                                 options: [ mergeRemote: "origin",
                                                 mergeTarget: "${env.gitlabTargetBranch}"]])
                            }
                        }

                        checkout([
                                $class: 'GitSCM', 
                                branches: [[name: "${params.branch}"]],
                                userRemoteConfigs: [[ url: "${git_url}", credentialsId: "${git_credentials_id}"]],
                                extensions:   checkout_ext 
                        ])
                    }
                }
            }
        }
    }
}

結帳階段有些復雜。 如果gitlabActionTypeMERGE ,則首先嘗試合並到目標分支中,以確保合並請求不會破壞其中的任何內容。

這兩個操作系統的代碼相同。 我想避免代碼重復,但是無法找出正確的語法。

我嘗試將結帳步驟的定義移至全局變量,但出現語法錯誤。


def checkout_step = {
    script {
   ...
    }
    checkout (... )
}

pipeline {
...
   stages {
        stage('Checkout') {
            parallel {
                stage ('Windows') {
                    agent {
                        label 'build' && 'windows'
                    }

                    steps {
                        checkout_step
                    }
                }
                stage ('Linux') {
                    agent {
                        label 'build' && 'linux'
                    }

                    steps {
                        checkout_step
                    }
                }
            }
        }
    }
}

如果添加steps ,則還會出現錯誤:


def checkout_step = steps {
    script {
   ...
    }
    checkout (... )
}

pipeline {
...
   stages {
        stage('Checkout') {
            parallel {
                stage ('Windows') {
                    agent {
                        label 'build' && 'windows'
                    }

                    checkout_step

                }
                stage ('Linux') {
                    agent {
                        label 'build' && 'linux'
                    }

                    checkout_step

                }
            }
        }
    }
}

在這里找到解決方案


git_url = "git@gitserver.corp.com:group/repo.git"
git_credentials_id = 'aaaaaaa-bbbb-cccc-dddd-eefefefefef'


def checkout_tasks(os_labels) {
    tasks = [:]

    for (int i = 0; i < os_labels.size(); i++) {
        def os = os_labels[i]
        tasks["${os}"] = {
            node("build && ${os}"){

                def checkout_ext = [[$class: 'CleanCheckout'], [$class: 'CleanBeforeCheckout']] // calls git clean -fdx and git reset --hard

                if (env.gitlabActionType == "MERGE"){   
                    checkout_ext.add([
                            $class: 'PreBuildMerge', 
                            options: [ 
                            mergeRemote: "origin", 
                            mergeTarget: "${env.gitlabTargetBranch}" 
                            ]
                    ])
                         /* using this extension requires .gitconfig with section [user for Jenkins]
                            Example
                            [user]
                            email = jenkins@builder
                            name = Jenkins

                          */
                }

                checkout([
                        $class: 'GitSCM', 
                        branches: [[name: "${params.branch}"]],
                        userRemoteConfigs: [[
                            url: "${git_url}", 
                            credentialsId: "${git_credentials_id}"
                        ]],
                        extensions:  checkout_ext 
                ])

            }
        }
    }
    return tasks
}


pipeline {
    agent none

    parameters {
        string(defaultValue: 'develop',
               description: 'Commit ID or branch name to build', 
               name: 'branch', 
               trim: false)
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    def OSes = ["windows", "linux"]
                    parallel checkout_tasks(OSes)
                }
            }
        }
   }
}

聲明不帶def git_urlgit_credentials_id也很重要,以便函數可以讀取它們。

這個問題的更多細節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM