AWS CodeCommit PR build

Ngchiwa Ng
2 min readMar 26, 2018

--

in AWS, it provides AWS CodeCommit, CodeBuild, CodePipeline, but it seems not support build if PR(pull request) create in CodePipeline,

this article, it help you to trigger PR build if PR create

it is our idea to trigger PR build

Main component:

  • AWS CloudWatch: monitor AWSCodeBuild event and trigger lambda
  • AWS Lambda: start AWS Codebuild

Setup AWS CodeBuild

first you need setup CodeBuild Project for your source code

Setup CloudWatch

CloudWatch > Events > Rules (Create Rule)

setup listen CodeCommit Event and trigger Lambda

Setup Lambda

Lambda >Functions > Create function

function code

const aws = require(‘aws-sdk’);
const codecommit = new aws.CodeCommit({ apiVersion: ‘2015–04–13’});
const codebuild = new aws.CodeBuild({ apiVersion: ‘2016–10–06’});
exports.handler = function(event, context) {

if (event.detail.event == “pullRequestCreated” ||
event.detail.event == “pullRequestSourceBranchUpdated”) {
//start build

codebuild.startBuild({projectName: YOUR_BUILD_PROJECT, sourceVersion:CODE_COMMIT})
}
}

PR events:

pullRequestCreated,

pullRequestSourceBranchUpdated,

pullRequestStatusChanged,

pullRequestMergeStatusUpdated

We can do somethings by different events

if you have better solutions CI/CD solution in AWS, please leave you comments :)

--

--

Ngchiwa Ng
Ngchiwa Ng

Written by Ngchiwa Ng

Backend/iOS Engineer, rock the world

No responses yet