jenkins管道:如何在代理列表上并行执行功能?

时间:2019-04-09 10:09:48

标签: jenkins jenkins-pipeline jenkins-declarative-pipeline

我有一堆服务于标签rhel6rhel7的节点。

如何在myFunc()的任意2个节点和rhel6的任意3个节点上执行rhel7-并行

def slaveList = ['rhel6', 'rhel6', 'rhel7', 'rhel7', 'rhel7']

def stageFunc (String slaveLabel) {
  return {
        // Run this stage on any available node serving slaveLabel
        agent { label "${slaveLabel}" } // Error shown here.
        stage {
            myFunc()
        }
    } 
}

pipeline {
    agent any

    stages {
        stage('Start') {
            steps {
                script {
                    def stageMap = [:]
                    def i = 0
                    slaveList.each { s ->
                        stageMap[i] = stageFunc(s)
                        i++
                    }
                    parallel stageMap
                }
            }
        }        
    }
}

显示的错误: java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [archive, ...

1 个答案:

答案 0 :(得分:0)

我还没有测试过,但是应该可以。

def slaveList = ['rhel6', 'rhel6', 'rhel7', 'rhel7', 'rhel7']

def stageFunc (stage_name, slaveLabel) {
  return {
        // Run this stage on any available node serving slaveLabel
        stage(stage_name){
            node(slaveLabel) {   
                myFunc()
            }
        }

    } 
}

pipeline {
    agent any

    stages {
        stage('Start') {
            steps {
                script {
                    def stageMap = [:]
                    def i = 0
                    slaveList.each { s ->
                        stageMap[i] = stageFunc("Stage-${i}", s)
                        i++
                    }
                    parallel stageMap
                }
            }
        }        
    }
}