How to trigger Jenkins builds remotely and to pass parameters

156,278

Solution 1

See Jenkins documentation: Parameterized Build

Below is the line you are interested in:

http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value

Solution 2

In your Jenkins job configuration, tick the box named "This build is parameterized", click the "Add Parameter" button and select the "String Parameter" drop down value.

Now define your parameter - example:

Enter image description here

Now you can use your parameter in your job / build pipeline, example:

Enter image description here

Next to trigger the build with own/custom parameter, invoke the following URL (using either POST or GET):

http://JENKINS_SERVER_ADDRESS/job/YOUR_JOB_NAME/buildWithParameters?myparam=myparam_value

Solution 3

To add to this question, I found out that you don't have to use the /buildWithParameters endpoint.

In my scenario, I have a script that triggers Jenkins to run tests after a deployment. Some of these tests require extra info about the deployment to work correctly.

If I tried to use /buildWithParameters on a job that does not expect parameters, the job would not run. I don't want to go in and edit every job to require fake parameters just to get the jobs to run.

Instead, I found you can pass parameters like this:

curl -X POST --data-urlencode "token=${TOKEN}" --data-urlencode json='{"parameter": [{"name": "myParam", "value": "TEST"}]}' https://jenkins.corp/job/$JENKINS_JOB/build

With this json=... it will pass the param myParam with value TEST to the job whenever the call is made. However, the Jenkins job will still run even if it is not expecting the parameter myParam.

The only scenario this does not cover is if the job has a parameter that is NOT passed in the json. Even if the job has a default value set for the parameter, it will fail to run the job. In this scenario you will run into the following error message / stack trace when you call /build:

java.lang.IllegalArgumentException: No such parameter definition: myParam

I realize that this answer is several years late, but I hope this may be useful info for someone else!

Note: I am using Jenkins v2.163

Solution 4

You can simply try it with a jenkinsfile. Create a Jenkins job with following pipeline script.

pipeline {
    agent any

    parameters {
        booleanParam(defaultValue: true, description: '', name: 'userFlag')
    }

    stages {
        stage('Trigger') {
            steps {
                script {
                    println("triggering the pipeline from a rest call...")
                }
            }
        }
        stage("foo") {
            steps {
                echo "flag: ${params.userFlag}"
            }
        }

    }
}

Build the job once manually to get it configured & just create a http POST request to the Jenkins job as follows.

The format is http://server/job/myjob/buildWithParameters?PARAMETER=Value

curl http://admin:test123@localhost:30637/job/apd-test/buildWithParameters?userFlag=false --request POST

Solution 5

To pass/use the variables, first create parameters in the configure section of Jenkins. Parameters that you use can be of type text, String, file, etc.

After creating them, use the variable reference in the fields you want to.

For example: I have configured/created two variables for Email-subject and Email-recipentList, and I have used their reference in the EMail-ext plugin (attached screenshot).

Enter image description here

Share:
156,278
Exploring
Author by

Exploring

I enjoy making computer to work for me!

Updated on January 22, 2021

Comments

  • Exploring
    Exploring over 3 years

    I am invoking a Jenkins job remotely using:

    wget http://<ServerIP>:8080/job/Test-Jenkins/build?token=DOIT
    

    Here Test-Jenkins job is invoked and DOIT is the security token that I have used.

    Now I need to pass some parameters to the build.xml file of this job i.e. Test-Jenkins.

    I have not yet figured out how to pass the variables yet.

  • Nathan Basanese
    Nathan Basanese over 8 years
    // , Can you show us a way to do this using the API?
  • Hari Kiran Mutyala
    Hari Kiran Mutyala about 8 years
    I have used simple HTTP calls like POST or GET Ex: RESTCallsUtility.invokePostMethod(auth, "<host>/Services/job/Jira2BugDB/buildWithParameters?token=ji‌​ra2bugdb_remote&Oper‌​ation=create-single&‌​Argument="+projectKe‌​y, "data")
  • Roman
    Roman almost 7 years
    Latest Jenkins docs say that GET is depreciated for security reasons, so POST should be preferred.
  • Dylan Kapp
    Dylan Kapp over 6 years
    I was missing "job" in my url.
  • Learner
    Learner about 6 years
    What about for multi branch projects?
  • Kamil Dziedzic
    Kamil Dziedzic almost 6 years
    But it triggers the build. And you need to actually make a POST call. I would like to offer people link with default parameter values e.g. GET http://<ServerIP>:8080/job/Test-Jenkins/build?some_param=xyz should open a web page with some_param set to value xyz. Then use can press "Build".
  • Craig Ringer
    Craig Ringer over 5 years
    It's bewildering that this isn't supported by the stock version and you need a plugin.
  • Peter Kahn
    Peter Kahn over 5 years
    Is there a plugin that does handle the post? I want to have a link a user and click on to trigger a specific parameterized build job with one click
  • Luv33preet
    Luv33preet about 5 years
    how to add authentication token using the script here?
  • haridsv
    haridsv about 5 years
    Perhaps the POST should be done to buildWithParameters instead of build? According to the cloudbees article, the default values will be used if the former is used. Here is the article: support.cloudbees.com/hc/en-us/articles/…
  • Kyle Pittman
    Kyle Pittman about 5 years
    @haridsv If you are using this in a script that will potentially trigger both builds that do and do not take any parameters, buildWithParameters will fail on jobs that are not parameterized, whereas build will not fail on any jobs.
  • haridsv
    haridsv about 5 years
    Thanks for the clarification! I guess that matters if the script is very generic and is not written for this specific job.
  • jhagege
    jhagege almost 4 years
    @Learner did you find a solution for multibranch project ?
  • Kasun Siyambalapitiya
    Kasun Siyambalapitiya almost 4 years
    Is there anyway to get the scheduled build job id after triggering?
  • LoganMzz
    LoganMzz almost 3 years
    Not just about genericity but also if parameters are declared in pipeline job but not run yet. It works BUT there's one drawback (maybe major depending on your usage), in this case response location doesn't contain queue item URL which can be used to follow build or resolve build URL
  • fmdaboville
    fmdaboville almost 3 years