How to update job config files using the Python API?

Article ID:218353568
1 minute readKnowledge base

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 = 'https://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

  • https://localhost:8080/ with your Jenkins URL

  • test with your job name

See Remote Access API and JenkinsAPI for more.

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.