How can i update a jenkins job using the api
Solution 1
in case anyone else is also looking for the same answer,
It appears the solution is far easier, all you have to do is update the config.xml and post the updated config.xml back to jenkins and your job will be updated.
Solution 2
You could use python like this:
from jenkinsapi.jenkins import Jenkins
jenkinsSource = 'http://10.52.123.124:8080/'
server = Jenkins(jenkinsSource, username = 'XXXXX', password = 'YYYYY')
myJob=server.get_job("__test")
myConfig=myJob.get_config()
print myConfig
new = myConfig.replace('<string>clean</string>', '<string>string bean</string>')
myJob.update_config(new)
Solution 3
You can also POST an updated config.xml to the URL which can fetch config.xml
, to programmatically update the configuration of a job.
The fetch url pattern: $JENKINS_SERVER/job/$JOB_NAME/config.xml
detailed doc pattern: $JENKINS_SERVER/job/$JOB_NAME/api
example: https://ci.jenkins-ci.org/job/infra_atlassian-base/api/
Solution 4
http://asheepapart.blogspot.ca/2014/03/use-jenkins-rest-api-to-update-job.html
That little bit of scripting looks to be what you are looking for. Uses the REST API to get and set the config with some regex S&R in the middle.
Edit: Code below based on comment. It is copied directly from the blog so I take no credit for it.
# First, get the http://jenkins.example.com/job/folder-name/job/sample-job--template/configure looking like you want
read -s token
# type token from http://jenkins.example.com/user/$userName/configure
# Download the configuration XML for the template job (which will be our model template)
curl -v -u "bvanevery:$token" http://jenkins.example.com/job/folder-name/job/sample-job--template/config.xml > generic-config.xml
# My modules
declare modules=('module1' 'module2' 'module3')
# POST the updated configuration XML to Jenkins
for m in ${modules[@]}; do
echo "module $m";
sed "s/MODULE/$m/g" generic-config.xml > $m-config.xml;
curl -v -X POST --data-binary @$m-config.xml -u "bvanevery:$token" \
-H 'Content-Type: application/xml' \
"http://jenkins.example.com/job/folder-name/job/$m/config.xml" ;
done
Related videos on Youtube
dsymquen
A Rutgers Grad learning about programming, one stack at a time
Updated on November 28, 2020Comments
-
dsymquen over 3 years
I have to create/update a jenkins job using its api because all of my jobs are using parameters which are also used by other scripts and I am trying to centralize the scripts so when i change it in one place, the change reflects in all.
currently, if someone changes the script, they they also have to manually edit the parameters of the jenkins job as well.
I saw the example of the Remote API for creating jobs and was able to successfully create test jobs but how can i edit an existing job besides deleting it and creating it again(which isnt an option as i have to maintain the build history).
-
aarosil over 9 yearsWhy not answer with the code itself, blog links may go away eventually
-
Kaj Kandler over 9 yearsFound this really helpful recipe, how to update the config.xml benkiew.wordpress.com/2012/01/12/…
-
Amedee Van Gasse almost 8 yearsThe Job-DSL plugin can only be used to maintain jobs that have been created by that plugin before. See stackoverflow.com/a/37815498/766786
-
akostadinov over 7 yearshighly inconvenient it seems, not much useful to have to fiddle with xml
-
Christian Madsen over 7 yearsThanks. I've really been struggling with this.
-
gabbar0x over 5 years^ This link is perfect. However, if your jenkins gives you the error '403 crumb was not included' on POST requests, There is an option in the "Global Security Settings" that "Enables the Compatibilty Mode for proxies". Switch this off and your POST requests should work