Created
October 27, 2015 20:27
-
-
Save glombard/3f41a5d7b1c0cadffb5f to your computer and use it in GitHub Desktop.
Backup the config.xml of Jenkins jobs using the REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
logging.captureWarnings(True) | |
SERVER = 'my-jenkins' | |
JOB_FILTER = 'test' | |
API_TOKEN = '<my-jenkins-api-token>' | |
USER = 'glombard' | |
def get_jobs(): | |
url = 'https://%s/api/json?pretty=true' % SERVER | |
response = requests.get(url) | |
data = response.json() | |
return data['jobs'] | |
def backup_job(name, url): | |
url = url + 'config.xml' | |
response = requests.get(url, auth=(USER, API_TOKEN), stream=True) | |
if not response.ok: | |
print 'error %d getting job config: %s' % (response.status_code, url) | |
else: | |
print name | |
with open(name + '.xml', 'w') as output: | |
for block in response.iter_content(2048): | |
output.write(block) | |
jobs = get_jobs() | |
for j in jobs: | |
if j['name'].startswith(JOB_FILTER): | |
backup_job(j['name'], j['url']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment