Last active
May 30, 2016 03:12
-
-
Save josefdlange/8b5d63d1a77bb339ac24edad2fb18f0a to your computer and use it in GitHub Desktop.
Simple script to pull Jenkins build status
This file contains hidden or 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
| #!/usr/bin/env python | |
| """ | |
| This script is pretty specific to my use case but perhaps someone else might find it useful. | |
| I have two Jenkins jobs, and that's what this script supports displaying: | |
| 1) Build on every commit to my `development` branch of my project, | |
| 2) Build on every PR to the `development` branch (using https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin) | |
| Requirements: | |
| pip install jenkinsapi terminaltables | |
| Usage: | |
| ./jenkins-check.py <username> <api-token> <server_url> | |
| Output: | |
| ┌──────────────────────────────────┬──────────────────────────────────────────────────┬─────────┐ | |
| │ Job Name │ Cause │ Status │ | |
| ├──────────────────────────────────┼──────────────────────────────────────────────────┼─────────┤ | |
| │ Cool AutoBuild on `development` │ SHA1: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx │ RUNNING │ | |
| │ Pull Requests to `development` │ PR #24: development <-- feature/cool-feature-bro │ SUCCESS │ | |
| └──────────────────────────────────┴──────────────────────────────────────────────────┴─────────┘ | |
| Caveats: | |
| If you want to access this inside a virtualenv, you'll want to be either: | |
| 1) Allowing access to global site-packages, or, | |
| 2) Installing the dependencies in the local virtualenv | |
| """ | |
| import sys | |
| from jenkinsapi.jenkins import Jenkins | |
| from terminaltables import SingleTable | |
| _, user, token, server = sys.argv | |
| jenkins = Jenkins(server, user, token) | |
| jobs = [] | |
| for j in jenkins.get_jobs(): | |
| job = jenkins.get_job(j[0]) | |
| b = job.get_last_build() | |
| changeset = b.get_changeset_items() | |
| if len(changeset) > 0: | |
| cause = 'SHA1: ' + changeset[-1]['commitId'] | |
| else: | |
| actions = {p['name']: p['value'] for p in b.get_actions()['parameters']} | |
| cause = cause = 'PR #{}: {} <-- {}'.format(actions['ghprbPullId'], actions['ghprbTargetBranch'], actions['ghprbSourceBranch']) | |
| jobs.append({ | |
| 'name': job.name, | |
| 'running': job.is_running(), | |
| 'queued': job.is_queued(), | |
| 'status': b.get_status(), | |
| 'cause': cause | |
| }) | |
| table_data = [ | |
| ['Job Name', 'Cause', 'Status'] | |
| ] | |
| for j in jobs: | |
| table_data.append([ | |
| j['name'], | |
| j['cause'], | |
| 'QUEUED' if j['queued'] else 'RUNNING' if j['running'] else j['status'] | |
| ]) | |
| print SingleTable(table_data).table |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment