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

1 Comments

  • 1
    Avatar
    Joyce Chidiadi

    Hi,

    Thanks for the explanation for updating job config files. I've tried this method but it doesn't work for me. Can you please let me know if this method currently works for you.

     

    Thank you.

Please sign in to leave a comment.