How to trigger Jenkins builds remotely and to pass parameters

JenkinsContinuous IntegrationContinuous DeploymentContinuous Delivery

Jenkins Problem Overview


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.

Jenkins Solutions


Solution 1 - Jenkins

See Jenkins documentation: Parameterized Build

Below is the line you are interested in:

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

Solution 2 - Jenkins

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 - Jenkins

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 - Jenkins

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 - Jenkins

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

Solution 6 - Jenkins

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"

Solution 7 - Jenkins

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

Solution 8 - Jenkins

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionExploringView Question on Stackoverflow
Solution 1 - JenkinsPeter SchuetzeView Answer on Stackoverflow
Solution 2 - JenkinsZacky PickholzView Answer on Stackoverflow
Solution 3 - JenkinsMonkpitView Answer on Stackoverflow
Solution 4 - JenkinsJanitha MadushanView Answer on Stackoverflow
Solution 5 - JenkinsHari Kiran MutyalaView Answer on Stackoverflow
Solution 6 - JenkinsNandhaView Answer on Stackoverflow
Solution 7 - JenkinsHimanshu MishraView Answer on Stackoverflow
Solution 8 - JenkinsmrtkprcView Answer on Stackoverflow