Skip to content

Instantly share code, notes, and snippets.

@goblindegook
Created March 3, 2016 18:21
Show Gist options
  • Save goblindegook/f3506512183c0ee3442b to your computer and use it in GitHub Desktop.
Save goblindegook/f3506512183c0ee3442b to your computer and use it in GitHub Desktop.
Restart the last Codeship build for a project
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Usage:
#
# $ CODESHIP_API_KEY={your api key} CODESHIP_PROJECT_ID={project id} ./rebuild.py
import os
import json
import urllib2
API_KEY = os.environ.get('CODESHIP_API_KEY')
PROJECT_ID = os.environ.get('CODESHIP_PROJECT_ID')
BRANCH = os.environ.get('CODESHIP_BRANCH')
def fetch_builds(project_id, branch = 'master'):
url = 'https://codeship.com/api/v1/projects/{}.json?api_key={}&branch={}'.format(project_id, API_KEY, branch)
request = urllib2.Request(url)
response = urllib2.urlopen(request)
project = json.load(response)
return project['builds'] if 'builds' in project.keys() else []
def restart_build(build):
url = 'https://codeship.com/api/v1/builds/{}/restart.json?api_key={}'.format(build['id'], API_KEY)
request = urllib2.Request(url, {})
response = urllib2.urlopen(request)
build = json.load(response)
return build
if __name__ == '__main__':
builds = fetch_builds(PROJECT_ID, BRANCH or 'master')
if len(builds):
result = restart_build(builds[0])
print 'Rebuilding {}: "{}" on {}'.format(result['id'], result['message'].split('\n')[0], result['branch'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment