jenkins pipeline : How to execute a function on a list of agents in parallel?

本小妞迷上赌 提交于 2021-01-29 05:22:48

问题


I have a bunch of nodes serving labels rhel6, rhel7.

How do I execute myFunc() on any 2 nodes of rhel6 and any 3 nodes rhel7 - in parallel?

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

Error shown: java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [archive, ...


回答1:


I haven't tested this yet, but it should work.

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


来源:https://stackoverflow.com/questions/55590295/jenkins-pipeline-how-to-execute-a-function-on-a-list-of-agents-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!