Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

groovy - how to run a jenkins job parralley with different parameter value?

I have a job named READ_LOGS, which takes a serverName as parameter and ssh into the server and cat the log file.

parameter: serverName

Build step: execute shell script, the script contents below command

ssh -f user@serverName 'cat /tmp/test.log'

I have another job named START_POC, which does provide list of 6 servers as choice parameter. While running the job, user selects any number of servers to get logs from. I want to run the START_POC for x many time the servers selected by user and it should run x many time parallel.

Approach 1: I tried below approach but did not work for me.

def runparallellogs = []

for (server in servers){
    runparallellogs["run-${server }"] = {
        buildJob = build job: 'READ_LOGS', parameters: [string(name: 'serverName', value: "${server")]
    }
}
parallel runparallellogs

This approach gives me java.lang.IllegalArgumentException: argument type mismatch error

Approach 2: (ref)

parallel (
      { build("READ_LOGS", serverName: server1) },
      { build("READ_LOGS", serverName: server2) },
      { build("READ_LOGS", serverName: server3) },
      { build("READ_LOGS", serverName: server4) }
)

Approach 3:

def testJobs = [] 
for (server in servers) { 
  def jobParams = [serverName: server] 
  def testJob = { 
    // call build 
    build(jobParams, "READ_LOGS") 
  } 
  println jobParams
  testJobs.add(testJob) 
} 

parallel(testJobs)

I am getting the same error for both the approach.

java.lang.IllegalArgumentException: Expected named arguments but got [org.jenkinsci.plugins.workflow.cps.CpsClosure2@598ac18e, org.jenkinsci.plugins.workflow.cps.CpsClosure2@238eaf6c, org.jenkinsci.plugins.workflow.cps.CpsClosure2@200d849d, org.jenkinsci.plugins.workflow.cps.CpsClosure2@6ddfc97b]

I just want to run the READ_LOGS logs job from START_POC job for the x number of params selected by user. Any help would be appreciated in this scenario.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I assume they changed something in the implementation of parallel that does not accept lists, but only named-arguments, that is why you are getting the errors.

If you use maps instead of lists, it works just fine. For example in your first approach you should declare runparallellogs as map: def runparallellogs = [:]

In the second approach, you should also use maps, eg:

parallel ([
    "run-server1" : {
        build(
            job: "READ_LOGS",
            parameters: [ string(name: 'serverName', value: "server1")])
    },
    "run-server2" : {
        build(
            job: "READ_LOGS",
            parameters: [ string(name: 'serverName', value: "server2")])
    },
])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...