How to trigger Jenkins builds remotely and to pass parameters
Asked Answered
C

10

101

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.

Cuxhaven answered 3/12, 2013 at 19:22 Comment(0)
M
111

See Jenkins documentation: Parameterized Build

Below is the line you are interested in:

http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
Misericord answered 3/12, 2013 at 20:14 Comment(5)
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".Jacquelynejacquelynn
It's bewildering that this isn't supported by the stock version and you need a plugin.Caitiff
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 clickBenevolent
Is there anyway to get the scheduled build job id after triggering?Lezley
Link updated : plugins.jenkins.io/parameterized-triggerAstrodynamics
U
82

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
Unsettle answered 17/3, 2016 at 11:56 Comment(4)
Latest Jenkins docs say that GET is depreciated for security reasons, so POST should be preferred.Kozloski
I was missing "job" in my url.Prole
What about for multi branch projects?Inactive
@Inactive did you find a solution for multibranch project ?Chaqueta
I
17

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

Incision answered 5/3, 2019 at 17:17 Comment(4)
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/…Protanopia
@Protanopia 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.Incision
Thanks for the clarification! I guess that matters if the script is very generic and is not written for this specific job.Protanopia
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 URLNeurogenic
S
3

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
Sox answered 21/9, 2018 at 7:58 Comment(2)
how to add authentication token using the script here?Heth
Generic Webhook trigger enables you to define this token.Strander
R
3

2023 Update

It has now changed how Jenkins Authenticates.
You need to pass 2 tokens to execute your job remotely.
You need:

  1. apiToken to authenticate your identity. This value is created from JENKINS_URL/me/configure . Also check here for documentation
  2. Another Job authentication token which you create when you enable 'Trigger builds remotely'.

Below is a sample you can tweak to get it done.

PARAM1_VALUE=<param1_value>
PARAM2_VALUE=<param2_vale>
USERNAME=dummy_user_name
JENKINS_URL="http://10.xxx.x.xxx:8080"
JOB_TOKEN="<value>" # you create this token when you enable Job>Configure>Build Triggers>Trigger builds remotely
LOGIN_API_TOKEN="<value>" #get this value from JENKINS_URL/me/configure 

curl -g -L --user $USERNAME:$LOGIN_API_TOKEN "$JENKINS_URL/job/JobName/buildWithParameters?token=$JOB_TOKEN&param1_name=$PARAM1_VALUE&param2_name=$PARAM2_VALUE"

-g or --globoff to avoid issues of passing parameters with "'" or brackets to curl

Reactionary answered 15/2, 2023 at 7:47 Comment(1)
many thanks! that's the trick. maybe you also will need a -X POST in your curl.Falster
B
2

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

Bulahbulawayo answered 2/3, 2015 at 17:53 Comment(2)
// , Can you show us a way to do this using the API?Stridulate
I have used simple HTTP calls like POST or GET Ex: RESTCallsUtility.invokePostMethod(auth, "<host>/Services/job/Jira2BugDB/buildWithParameters?token=jira2bugdb_remote&Operation=create-single&Argument="+projectKey, "data")Bulahbulawayo
M
1

When we have to send multiple trigger parameters to jenkins job, the following commands works.

curl -X POST -i -u "auto_user":"xxxauthentication_tokenxxx" "JENKINS_URL/view/tests/job/helloworld/buildWithParameters?param1=162&param2=store"
Mastitis answered 21/11, 2020 at 18:12 Comment(0)
P
0

You can trigger Jenkins builds remotely and to pass parameters by using the following query.

JENKINS_URL/job/job-name/buildWithParameters?token=TOKEN_NAME&param_name1=value&param_name1=value

JENKINS_URL (can be) = https://<your domain name or server address>

TOKE_NAME can be created using configure tab

Philippa answered 27/1, 2020 at 14:16 Comment(0)
L
0
curl -H "Jenkins-Crumb: <your_crumb_data>" -u "<username>:<password>" "http://<your_jenkins_url>?buildWithParameters?token=<your_remote_api_name>?<parameterA>=<val_parameter_A>&<parameterB>=<val_parameterB>"

You can change following parameters as you want:

<your_crumb_data>
<username>
<password>
<your_jenkins_url>
<your_remote_api_name>
<parameterA>
<parameterB>
<val_parameter_A>
<val_parameter_B>

Note: Placing double quotes may be critical. Pay attention, please.

Linzy answered 22/1, 2021 at 8:6 Comment(0)
K
0
curl -X POST -u Admin:<api_token> http://localhost:8080/job/<job_name>/buildWithParameters\?ColorValue\=sddddsd
Katmandu answered 18/1, 2023 at 13:52 Comment(1)
Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.Insecure

© 2022 - 2025 — McMap. All rights reserved.