23

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).

Hitesh
  • 3,449
  • 8
  • 39
  • 57
dsymquen
  • 584
  • 1
  • 5
  • 23

6 Answers6

20

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)
maxshuty
  • 9,708
  • 13
  • 64
  • 77
Dave
  • 201
  • 2
  • 2
16

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.

dsymquen
  • 584
  • 1
  • 5
  • 23
  • 2
    Found this really helpful recipe, how to update the config.xml http://benkiew.wordpress.com/2012/01/12/automating-hudsonjenkins-via-rest-and-curl-a-very-small-cookbook/ – Kaj Kandler Oct 31 '14 at 14:01
  • ^ 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 – gabbar0x Nov 21 '18 at 05:55
10

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/

CSchulz
  • 10,882
  • 11
  • 60
  • 114
Frank Chen
  • 171
  • 1
  • 5
3

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
CSchulz
  • 10,882
  • 11
  • 60
  • 114
0

For those using RestSharp, I found that I needed to make sure that:

  1. The user ID performing the update had permission to do so under Manage > Global Security > Authorization Matrix
  2. I had a current Jenkins Crumb token, required once CSRF (also under Manage > Security) is enabled.
  3. Send the updated XML using a parameter of the Request object with the value of [ParameterType.RequestBody] (link)1 for the type argument.

    private XmlDocument JobConfigGet()
    {
        Uri JobConfigURI = GetJenkinsURI("job/" + _args.JobName + "/config.xml", null);
        RestClient restClient = new RestClient(JobConfigURI);
        RestRequest restRequest = new RestRequest(Method.GET);
    
        byte[] ua = Encoding.ASCII.GetBytes(Properties.Settings.Default.UserID + ":" + Properties.Settings.Default.UserPassword);
        restRequest.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua));
        IRestResponse restResponse = restClient.Execute(restRequest);
    
        if (restResponse.ResponseStatus != ResponseStatus.Completed || restResponse.StatusCode != HttpStatusCode.OK)
            throw new Exception(string.Format("Unable to retrieve job config: {0}. Wrong ResponseStatus ({1}) or StatusCode ({2}) returned.\nURL: {3}", _args.JobName, restResponse.ResponseStatus.ToString(), restResponse.StatusCode.ToString(), restClient.BaseUrl.AbsoluteUri));
        if (restResponse.ContentType != "application/xml")
            throw new Exception("Unexpected data type returned for job config: " + _args.JobName + ".  Expected 'application/xml'. Got: " + restResponse.ContentType + ".\nURL: " + restClient.BaseUrl.AbsoluteUri);
    
        XmlDocument jobConfig = new XmlDocument();
        jobConfig.LoadXml(restResponse.Content);
        return jobConfig;
    }
    
    private void JobConfigUpdate(XmlDocument JobConfig, string JenkinCrumb)
    {
        // Update JobConfig XML as needed here.
        Uri JobConfigURI = GetJenkinsURI("job/" + _args.JobName + "/config.xml", null);
        RestClient restClient = new RestClient(JobConfigURI);
    
        RestRequest restRequest = new RestRequest(Method.POST);
        byte[] ua = Encoding.ASCII.GetBytes(Properties.Settings.Default.UserID + ":" + Properties.Settings.Default.UserPassword);
        restRequest.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua));
    
        string[] crumbSplit = JenkinCrumb.Split(':');
        restRequest.AddHeader(crumbSplit[0], crumbSplit[1]);
        restRequest.AddParameter("text/xml", JobConfig.InnerXml, ParameterType.RequestBody);
    
        IRestResponse restResponse = restClient.Execute(restRequest);
        string resp = restResponse.Content;
    }
    
Dave Michener
  • 1,058
  • 11
  • 30
0
curl -v -X POST https://jenkinsurl.fr:8443/job/jobname/config.xml  --data-binary "@config.xml" -u "jenkinsusername:yourjenkinstoken" -H "Content-Type: application/xml"