matandked
matandked

Reputation: 1565

Is it possible to execute Jenkins jobs from Powershell or Bash or Groovy scripts?

I have separated quite small Jenkins jobs.

Now I need to configure another job which depending on the selected by the user parameters (selected probably through checkboxes?) will execute some of them.

I would like to start them from Powershell or Bash or Groovy script. Is it possible?

Upvotes: 1

Views: 2085

Answers (2)

qxo
qxo

Reputation: 1694

sample:

import hudson.model.*
def actions=[]
  def plist=[ ];
  ["ok":"ok","label":"master"].each {k,v->    
    plist << new hudson.model.StringParameterValue(k,"$v","");
  }
actions.add(new hudson.model.ParametersAction(plist));
def future = Jenkins.instance.getItemByFullName("samples/testPipeline").scheduleBuild2(0,actions as hudson.model.Action[] );
future.get().getLog()

Upvotes: 0

Daniel Omoto
Daniel Omoto

Reputation: 2481

If you are using Groovy in a Postbuild/pipeline step, you can start the job via the Jenkins API.

For example, something like this for parameterless builds:

import hudson.model.*
Jenkins.instance.getItem("My Job").scheduleBuild(5)

And something like this for parameterized builds:

import hudson.model.*
Jenkins.instance.getItem("My Job").scheduleBuild( 5, new Cause.UpstreamCause( currentBuild ), new ParametersAction([ new StringParameterValue( "My Parameter Name", "My Parameter Value" ) ]));

You can also use the Jenkins Rest API for the rest. For example, by hitting the following url:

Parameterless:

curl -X POST JENKINS_URL/job/JOB_NAME/build

Parameterized:

curl -X POST JENKINS_URL/job/JOB_NAME/buildWithParameters?MyParameterName=MyParameterValue

Upvotes: 2

Related Questions