如何使用Jenkins Declarative Pipeline同时运行多个代理(在运行时给定)?

bqf10yzr  于 2023-11-17  发布在  Jenkins
关注(0)|答案(1)|浏览(138)

我在运行时手动选择的多个平台上运行Jenkins作业。为相关节点选择多个名为“Platforms”的活动选择参数。
我在Jenkins脚本化管道中使用了以下代码:

def labels=Platforms.toString().split(",").collect{"\'" + it + "\'"}
    def builders=[:]
    
    for (label in labels) {
        builders[label] = {
                node(label) {
                    stage ('Stage 1') {
                        sh 'hostname'
                    }
                }
        }
    }
    parallel builders

字符串
如何在声明性管道中完成同样的事情?代理是由用户在运行时随机选择的。
谢谢.

cdmah0mi

cdmah0mi1#

好了,我在这里找到了答案:Running same Jenkins job on multiple agents in parallel in declarative pipeline感谢用户'np2807'。我的代码现在看起来像这样:

def labels=Platforms.toString().split(",").collect{"\'" + it + "\'"}

def generateStage(nodeLabel) {
    return {
        stage("Runs on ${nodeLabel}") {
            node(nodeLabel) {
                stage("Stage 1") {
                   script {
                        echo "Running on ${nodeLabel}"
                        sh 'hostname'
                      }
                 }
            }
        }
    }
}

def parallelStagesMap = labels.collectEntries {
    ["${it}" : generateStage(it)]
}

pipeline {
    agent none
    stages {
        stage('non-parallel stage') {
            steps {
                echo 'This stage will be executed first.'
            }
        }
        stage('parallel stage') {
            steps {
                script {
                    parallel parallelStagesMap
                }
            }
        }       
    }
}

字符串

相关问题