How to Get config.xml of a Jenkins Job with Python

Each Jenkins job is represented by XML. You can get the XML file using an URL like this.


Actually by saving it, you can back up your Jenkins job as XML file. Here is a function that can return you the XML content.

import requests
import urllib3
import ssl

class jenkins_server:
    def __init__(self, base_url, username, api_token):
        self.base_url = base_url
        self.username = username
        self.api_token = api_token
        self.crumb = None

    def get_job_xml(self, job_path):
        r = None

        if self.base_url.endswith("/"):
            api_url = f"{self.base_url}{job_path}"
            api_url = f"{self.base_url}/{job_path}"

            headers = {'Content-Type': 'application/xml'}
            r = requests.get(url=api_url, headers=headers, auth=(self.username, self.api_token), verify=ssl.CERT_NONE)
            return r.text

        except Exception as e:
            print("Error occurred: ", str(e))
            if r is not None:

Here is the test code.

    def test_get_job_xml(self):
        jenkins = jenkins_server(self.config['jenkins_server']['base_url'],

        xml = jenkins.get_job_xml("job/win-test/config.xml")


Notice that I implemented a configuration file with the test so that I can reuse the data in different test cases.

The XML content looks like this.

<?xml version='1.1' encoding='UTF-8'?>
  <scm class="hudson.scm.NullSCM"/>


This technique looks kind of useless but it has a huge potential if you have 500 Jenkins legacy jobs that needs to be managed without DSL. Jenkins API allows us to update jobs by us uploading the XML files. It means you could make mass changes to the XML files and upload them.

The ideal way to manage many Jenkins jobs is to use Job DSL and there is no doubt about it, but for legacy Jenkins jobs, this technique will lead us to something really advantageous.

Leave a Reply

Your email address will not be published.