Jenkins' JobDSL Queue with Parameters

2019-08-31 06:41发布

问题:

Does anyone know if you can run a Jenkins' job from JobDSL that has parameters?

I have used queue https://jenkinsci.github.io/job-dsl-plugin/#path/queue

But according to the docs, it only accepts a string or Job object. Maybe there is a way to do it with Job object, but its not clear. From JobDSL docs:

def example1 = job('example-1') {
   displayName('first example')
}

queue(example1)

job('example-2') {
    displayName('second example')
}

queue('example-2')

回答1:

Have same problem and couldn't find an answer in docs so I'm now looking at using system groovy script as per this example.

def job = Hudson.instance.getJob('MyJobName')
def anotherBuild
try {
    def params = [
      new StringParameterValue('FOO', foo)
    ]
    def future = job.scheduleBuild2(0, new Cause.UpstreamCause(build), new ParametersAction(params))
    println "Waiting for the completion of " + HyperlinkNote.encodeTo('/' + job.url, job.fullDisplayName)
    anotherBuild = future.get()
} catch (CancellationException x) {
    throw new AbortException("${job.fullDisplayName} aborted.")
}

I'm using Jenkins 2.116 and Groovy plugin 2.0