Skip to content

Instantly share code, notes, and snippets.

AWS Lambda functions that you can set to be triggered by Codecommit push triggers and it will trigger builds on any CodeBuild Project that has this repository as source.
import boto3
codecommit = boto3.client('codecommit')
codebuild = boto3.client('codebuild')
def trigger_build(project, commit, branch):
codebuild.start_build(projectName=project, sourceVersion=commit,
environmentVariablesOverride=[
{ 'name': 'ENVIRONMENT', 'value': branch, 'type': 'PLAINTEXT' }
])
def handler(event, context):
record = event['Records'][0]
repo_name = record['eventSourceARN'].split(':')[5]
repo = codecommit.get_repository(repositoryName=repo_name)
repo_url = repo['repositoryMetadata']['cloneUrlHttp']
paginator = codebuild.get_paginator('list_projects')
page_iterator = paginator.paginate()
for page in page_iterator:
projects = codebuild.batch_get_projects(names=page['projects'])
for project in projects['projects']:
src = project['source']
if (src['type'] == 'CODECOMMIT' and src['location'] == repo_url):
for ref in record['codecommit']['references']:
_, ref_type, ref_name = ref['ref'].split('/', 2)
if (ref_type == 'heads'):
branch = codecommit.get_branch(
repositoryName=project['name'], branchName=ref_name)
commit = branch['branch']['commitId']
trigger_build(project['name'], commit, ref_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment