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.

https://your-server/job/your_job/config.xml

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}"
        else:
            api_url = f"{self.base_url}/{job_path}"

        try:
            urllib3.disable_warnings()
            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))
        finally:
            if r is not None:
                r.close()

Here is the test code.

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

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

        print(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'?>
<project>
  <description></description>
  <keepDependencies>false</keepDependencies>
  <properties/>
  <scm class="hudson.scm.NullSCM"/>
  <assignedNode>win</assignedNode>
  <canRoam>false</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders>
    <hudson.tasks.BatchFile>
      <command>dir</command>
    </hudson.tasks.BatchFile>
  </builders>
  <publishers/>
  <buildWrappers/>
</project>

Racap

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.

Author: admin

A software engineer in greater Seattle area

Leave a Reply

Your email address will not be published. Required fields are marked *