I just recently changed jobs: my new development team is larger, it’s a very dynamic environment with multiple branches being created in
git repositories all the time, and people are creating Jenkins build jobs in order to run automated builds and tests for these branches on a daily basis.
At some point the manual copy-pasting of Jenkins jobs that was the general practice was beginning to annoy me and I decided to give a try to Jenkins’ HTTP API. The goal was to be able to script the creation of jobs, from consistent updateable descriptions. The result has been the creation of the
autojenkins package written in Python, that will allow us to query the status of Jenkins jobs, trigger manual builds, create new ones, and cleanup by removing old jobs.
Autojenkins is published on PyPI, you install it simply with:
pip install autojenkins
Once installed, it provides an easy to use Python client for the Jenkins HTTP API. Here’s a taste of the API with some sample usage:
from autojenkins import Jenkins j = Jenkins('http://jenkins.pe.local') # trigger a manual build and check results j.build('my-job') j.last_result('my-job') # get only the result string (one of 'SUCCESS', 'UNSTABLE', 'FAILURE'): j.last_result('my-job')['result'] # get the configuration file for a job: j.get_config_xml('template') # Create a new job from a job named 'template', replacing variables j.create_copy('my-new-job', 'template', repo='my-repo', branch='my-branch') # build j.build('my-new-job') # check result and delete if successful: result = j.last_result('my-new-job')['result'] if result == 'SUCCESS': j.delete('my-new-job')