-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
49 lines (38 loc) · 1.58 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
import boto3
codebuild = boto3.client('codebuild')
def respond(body, err=None):
if not body:
return {'statusCode': '204', 'body': ''}
return {
'statusCode': '400' if err else '200',
'body': json.dumps(body),
'headers': {
'Content-Type': 'application/json'
}
}
def lambda_handler(event, context):
print("Received event:")
print(json.dumps(event))
#print(event['headers'])
if not event['headers'].get('X-Event-Key') == 'repo:push':
return respond({'message': 'not a repo:push'}, err=True)
if (event['queryStringParameters'] is None
or 'projectName' not in event['queryStringParameters']):
return respond({'message': 'projectName query parameter not specified'},
err=True)
project_name = event['queryStringParameters']['projectName']
body = json.loads(event['body'])
builds = []
for change in body['push']['changes']:
new_target = change['new']['target']['hash']
idempotency_token = event['headers'].get(
'X-Request-UUID',
event['requestContext']['requestId'])
print(f"triggering codebuild.start_build(projectName='{project_name}', sourceVersion='{new_target}', idempotencyToken='{idempotency_token}')")
response = codebuild.start_build(
projectName=project_name,
sourceVersion=new_target,
idempotencyToken=idempotency_token)
builds.append(response['build']['arn'])
return respond({'message': 'success', 'builds': builds})