Skip to content

Instantly share code, notes, and snippets.

@thehar
Forked from rosstimson/codeship-ami-builder.py
Created December 5, 2017 18:10
Show Gist options
  • Save thehar/e1da9283ce0273210bbbd5777a12a84d to your computer and use it in GitHub Desktop.
Save thehar/e1da9283ce0273210bbbd5777a12a84d to your computer and use it in GitHub Desktop.
Scheduled Lambda that uses the Codeship API in order to trigger a build.
import boto3
import http.client
import json
import logging
import os
from base64 import b64decode
conn = http.client.HTTPSConnection("api.codeship.com")
payload = "{}"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
auth_encrypted = os.environ['AUTH']
auth = boto3.client('kms').decrypt(CiphertextBlob=b64decode(auth_encrypted))['Plaintext']
auth = auth.decode("utf-8")
print(auth)
# Set UUIDs of Codeship build to be restarted.
org_uuid = os.environ['ORG_UUID']
project_uuid = os.environ['PROJECT_UUID']
build_uuid = os.environ['BUILD_UUID']
def authenticate():
payload = "{}"
headers = {
'content-type': "application/json",
'authorization': "Basic " + auth
}
conn.request("POST", "/v2/auth", payload, headers)
res = conn.getresponse()
data = res.read()
decoded_data = data.decode("utf-8")
parsed_json = json.loads(decoded_data)
return parsed_json['access_token']
def restart_build(org_uuid, project_uuid, build_uuid):
auth_token = authenticate()
headers = {
'content-type': "application/json",
'authorization': auth_token
}
conn.request("POST", "/v2/organizations/" + org_uuid + "/projects/" + project_uuid + "/builds/" + build_uuid + "/restart", payload, headers)
res = conn.getresponse()
data = res.read()
logger.info(data.decode("utf-8"))
def lambda_handler(event, context):
# Restart a build on Codeship
restart_build(org_uuid, project_uuid, build_uuid)
@dclawson
Copy link

dclawson commented Aug 1, 2018

Hi, what would the AUTH env consist of? Is this a CodeShip auth token?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment