How to update job config files using the Python API?

Issue

I would like to be able to update job config files remotely using the Jenkins Python API.
 

Environment

  • CloudBees Jenkins Enterprise
  • Remote Access API
  • JenkinsAPI

Resolution

Here is an example that replaces occurrences of oldvalue with newvalue in job test.

#!/usr/bin/python

from jenkinsapi.jenkins import Jenkins

if __name__ == '__main__':
        jenkins = 'http://localhost:8080/'
        server = Jenkins(jenkins, username = 'admin', password = 'admin')
        job = server.get_job('test')
        config=job.get_config()
        new = config.replace('oldvalue', 'newvalue')
        job.update_config(new)

Obviously, replace:

  • username = 'admin', password = 'admin' with your username and password
  • http://localhost:8080/ with your Jenkins URL
  • test with your job name

See Remote Access API and JenkinsAPI for more.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.