0
votes

Hello Jenkins friends,

I created a Jenkins Freestyle project that contains the step "Execute Groovy Script". From the script, I want to trigger another parameterized job on the same Jenkins server.

Does anybody know how I can do this?

def triggerBuild(paramter1, paramter2) {
   // trigger job with name "foo" and set the paramters param1 and param2 
   //to the values of the variables parameter1 and parameter2
}
2

2 Answers

1
votes
def triggerBuild(parameter1, parameter2) {
  build job: 'foo', parameters: [
    string(name: 'param1', value: parameter1),
    string(name: 'param2', value: parameter2),
  ]
}

Be sure to invoke triggerBuild() inside script block.

0
votes

OK, I finally got a solution. My script looks as follows:

import hudson.model.*;

triggerBuild()

def triggerBuild(parameter1, parameter2) {
    def job = Hudson.instance.getJob('foo')

    def params = [
            new StringParameterValue('param1', 'value1'),
            new StringParameterValue('param1', 'value2')
    ]
    def future = job.scheduleBuild2(0,  new ParametersAction(params))
}