Neel Desai
Neel Desai

Reputation: 71

How to update Jenkins Job config.xml file using curl

How can I edit jenkins job's parameter by updating config.xml of jenkins job using curl?

Upvotes: 2

Views: 16245

Answers (3)

iocanel
iocanel

Reputation: 580

Here is a link of a script that I've been using in order to modify a job's pipeline for the shell: https://raw.githubusercontent.com/iocanel/presentations/382074b5012d6c3ed87042298114e688424eeaed/workspace/editor/jenkins-run-pipeline

Upvotes: 0

Majus Misiak
Majus Misiak

Reputation: 674

Just updating content of config.xml file is probably not enough to change in-memory state of Jenkins job. You still need to reload configuration from disk, which can be done either in GUI with jenkins/manage/, using groovy script or simply rebooting the server. After that your example should work.

This really goes down to the fact that Jenkins config.xml are XStream serialized java objects, not actual configuration files. So changing job parameters by manually editing xml files is likely not the best solution. Instead, you could change the job configuration using Jenkins script console. For example to change default parameter value for String parameter, you can run below script in Jenkins console (e.g. http://localhost:8080/jenkins/script):

import hudson.model.ParametersDefinitionProperty

def jobName = "job_name"
def paramName = "param_to_be_changed"
def newParamValue = "param_new_value"

def job = Jenkins.instance.getItem(jobName)
def params = job.getAction(ParametersDefinitionProperty)

def paramToModify = params.getParameterDefinitions().find { param -> param.getName() == paramName }    

paramToModify.setDefaultValue(newParamValue)

job.save()

If the job is inside the folder or organization, it is necessary to go one level further, i.e.:

def folderName = "folder_name"
def job = Jenkins.instance.getItem(folderName).getItem(jobName)

After that job state will be stored in config.xml file. After that you can execute the script remotely using curl. Assuming you saved above script to script.groovy file:

# Get breadcrumb from Jenkins
curl -u <username>:<password> 'http://localhost:8080/jenkins/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'
# Send script to Jenkins console
curl -X POST -u <username>:<password> -H 'Jenkins-Crumb: <crumb>' -H 'Content: text/plain' --data-urlencode "script=$(< script.groovy)"  http://localhost:8080/jenkins/scriptText

More details on Parameter API in javadoc

Upvotes: 1

Mikhail Naletov
Mikhail Naletov

Reputation: 285

You can use:

curl -X POST 'http://my-cool-jenkins.com:8080/createItem?name=mycooljob' -u username:password --data-binary @config.xml -H "Content-Type:text/xml"

Update:

That url for creating job, for updating use:

curl -X POST 'http://my-cool-jenkins.com:8080/job/mycooljob/config.xml' -u username:password --data-binary @config.xml -H "Content-Type:text/xml"

Upvotes: 5

Related Questions