Subject is self-explanatory. I'd like to add post-build actions for many Jenkins jobs, instead of configuring one by one. I added Configuration Slicing plugin but if I'm not mistaken it doesn't modify post-build actions.
Any ideas?
Thanks in advance
If Configuration Slicing Plugin doesn't satisfy your requirments, then you should fall back to SED.
From Jenkins Issues:
You will need to write a script that will loop through your Jenkins
jobs and SED the value with a new one then use POST
https://support.cloudbees.com/hc/en-us/articles/218353308-How-to-update-job-config-files-using-the-REST-API-and-cURL
Get current config
curl -X GET
http://developer:developer#localhost:8080/job/test/config.xml -o
mylocalconfig.xml
Post updated config
curl -X POST http://developer:developer#localhost:8080/job/test/config.xml
--data-binary "#mymodifiedlocalconfig.xml"
For the Pos-build actions is the markups between <publishers>...</publishers> in the config.xml
Related
I have a requirement where i need to trigger the build jobs remotely using curl command. I am unable to pass the branch/tag name as a parameter to trigger the build.
I used the below command :
& $CURLEXE -k -X POST $dst_job_url --user username:token --data-urlencode json='{"parameters": [{"name":"branch","branch":"branches"}]}'
If i run the above command it was triggers the build for the trunk ( default ).
You omitted the URL, so it's hard to be certain. Jenkins has two urls for building: "build" and "buildWithParameters". If you're not using buildWithParameters, switching to it will probably help.
See:
How to trigger Jenkins builds remotely and to pass parameters
I have a jenkins Job that do two steps.
1) Get source code from GIT
2) Run the source code analisys using SONAR-RUNNER
The Job works fine and I'd like to copy/create a similar Job.
I tried to create a new Job using the command: curl -s --data-binary #config.xml -H "Content-Type: application/xml" -X POST http://localhost:8080/createItem?name=SONAR-NEW
It does create a new Job, however the SONAR-RUNNER part is empty and if I ran the build it doesn't make the SONAR part. The funny thing is that if I check the config.xml generated the SONAR-RUNNER configuration part is there.
The same thing happens if I try to copy the JOB, I tried it using:
1) jenkins-cli, or
2) curl --data "name=NEWJOBNAME&mode=copy&from=SONAR" -X POST http://localhost:8080/createItem
If I have a different build step, for example a "Execute SHell", I am able to copy/create the JOB.
So my question is how can I create or copy a JOB that as a build step using "Execute SonarQube Scanner"
Is there a way to have a script being run from an execute shell step access the build status as well as other information about the build? (e.g. build number, start/end time, etc)
I need to update a wiki page with a script with the information about the build.
Thanks in advance!
Build status information gets saved into simple XML files. Take a look at
{jenkins-directory}/jobs/{job-name}/builds/{build-number}/build.xml. You can use symbolic links like lastSuccessfulBuild instead of specifying a build number if you want.
You'll find all information about the build there: build status, start time, duration, etc. Parse the XML file or just use grep in a bash script.
If you don't want to use the filesystem you can use Jenkins API.
But in any case, once you got a script that can determine build parameters and update your wiki page, you can put it into another Jenkins job and trigger it automatically (for example, with the BuildResultTrigger Plugin).
I'm using jenkins own API to get the build status while the job is running, which works like a charm. Be aware that i'm using JQ To parse the json response.
To get this to work simply add a shell script and execute the following command: BUILD_STATUS=$(curl --silent ${BUILD_URL}api/json | jq -r '.result')
Which results in the following:
While executing a build, Jenkins set environment variables you can use in your script.
also refer to the detailes on how to use it -
%VAR% in batch files and $VAR from inside the Jenkins job configuration page
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.
See Jenkins documentation: Parameterized Build
Below is the line you are interested in:
http://server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
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:
Now you can use your parameter in your job / build pipeline, example:
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
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
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
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).
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¶m2=store"
You can trigger Jenkins builds remotely and to pass parameters by using the following query.
JENKINS_URL/job/job-name/buildWithParameters?token=TOKEN_NAME¶m_name1=value¶m_name1=value
JENKINS_URL (can be) = https://<your domain name or server address>
TOKE_NAME can be created using configure tab
curl -X POST -u Admin:<api_token> http://localhost:8080/job/<job_name>/buildWithParameters\?ColorValue\=sddddsd
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.
2023 Update
It has now changed how Jenkins Authenticates.
You need to pass 2 tokens to execute your job remotely.
You need:
apiToken to authenticate your identity. This value is created from JENKINS_URL/me/configure . Also check here for documentation
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 -L --user $USERNAME:$LOGIN_API_TOKEN "$JENKINS_URL/job/JobName/buildWithParameters?token=$JOB_TOKEN¶m1_name=$PARAM1_VALUE¶m2_name=$PARAM2_VALUE"
I am new to Jenkins, and I'm not sure if this is possible, but I would like to set up a web interface where somebody could click "Start Job" and this will tell Jenkins to start a particular build job.
Does Jenkins have a webservice that would allow such a thing? If so, what would be a simple example?
Here is a link to the documentation: Jenkins Remote Access API.
Check out the Submitting jobs section.
In your job configuration you setup a token and then create a POST request to JENKINS_URL/job/JOBNAME/build?token=TOKEN. That's probably the most basic usage.
Jenkins has support for parameterized build as well.
So, if you want to pass parameters for configurable build generation, you can pass them by posting it while invoking Jenkins build request with http://YOURHOST/jenkins/job/PROJECTNAME/buildWithParameters.
Aha, I found it in the documentation. So simple:
http://YOURHOST/jenkins/job/PROJECTNAME/build
I needed to add parameters and I wanted to do it over https. It took me a while but the following worked for me:
curl --request POST --url 'https://HOST_NAME/job/JOB_NAME/buildWithParameters?token=TOKEN' --header 'cache-control: no-cache' --header 'content-type: application/x-www-form-urlencoded' --data 'name1=value1&name2=value2'
Use:
http://some server/job/myjob/buildWithParameters?token=TOKEN&PARAMETER=Value
You can take a look at this documentation: Parameterized Build
curl -H POST http://USERNAME:PASSWORD#JENKINS_HOST:PORT/job/JOB_NAME/build?token=YOUR_TOKEN
Set YOUR_TOKEN at job configuration -> build triggers -> Trigger builds remotely.
There is a good sample of using the above API from Python. The project is called Python Jenkins, and you may find it here: link
Jenkins has a documented REST API. You can make your little web service invoke it.
With curl if you have multiple arguments to pass like a token and a parameter you might have to quote on Linux shell:
curl -H POST "http://USERNAME:PASSWORD#JENKINS_HOST:PORT/job/JOB_NAME/build?token=YOUR_TOKEN&PARAMETER=VALUE"
Install Generic Webhook Trigger plugin.
Select generic webhook trigger in build trigger actions. Generate a random string and paste in token.
Now your job can be triggered with a http request to the following url.
http://JENKINS_URL/generic-webhook-trigger/invoke?token=TOKEN_VALUE
replace your jenkins url and token value