Github AWS CodeBuild integration

This whole post/project is no longer needed, CodeBuild now has native Github integration, please use that.

As you might know, AWS CodeBuild is a service by AWS which can run your integration test or builds for you. It can be triggered by CodePipeline to deliver artifacts, and you can use CodeDeploy to deploy those artifacts to your servers. Unfortunately, it’s currently not possible to invoke CodeBuild directly from Github, or have CodePipeline trigger on any branch change.

Which is where this project comes in.

This project creates a webhook endpoint for your Github repository, which is called every time you create/update a Pull Request. It keeps track of your build, and updates your PR status whether your build/tests have succeeded or failed.

The nice thing about having your builds run in AWS CodeBuild, is that everyting is completely serverless. Everything also runs within your own AWS account. You don’t have to setup any additional billing, etc. On top of that, your AWS CodeBuild project builds are free for the first month (based on the free tier)! And ofcourse you get IAM control out of the box.

You can find all the source code here: https://github.com/svdgraaf/github-codebuild-webhook

Quick launch 🚀

If you just want to launch the service yourself, you can use this magic button which will setup everything for you in your AWS account through the magic of CloudFormation:

Architecture

How does this work?

When you deploy the CloudFormation template, a couple of things happen. First, some arbitrairy resources are created (IAM roles, Loggroups, etc). The Lambda functions are created, then a Step Function is created which points to the Lambda functions. An ApiGateway is created which is connected to the Step Function. And lastly a Custom Resource is created, which calls another Lambda function, which installs the webhook on the repository that you configured.

When you create a PR on your repository, a notification is send to the ApiGateway endpoint and the Lambda Step Function is triggered. This will trigger the start of your CodeBuild project, and sets a status of pending on your specific commit.

While the build is running, the Lambda Step Function will check the status of your build every X seconds.

When the status of the build changes to done or failed , the Github api is called and the PR status will be updated accordingly.

Setup steps

Use the steps below to launch the stack directly into your AWS account. You can setup as many stacks as you want, as the stack is currently connected to 1 CodeBuild project.

First, we’ll need to setup an AWS CodeBuild project. Create a new project in the AWS console, and be sure to add a buildspec.yml file to your project with some steps. Here’s an example. Create a github api token in your account here, so that the stack is allowed to use your account: https://github.com/settings/tokens/new. You can ofcourse choose to setup a separate account for this. Deploy the stack: (or use sls deploy ). This will setup all necessary resources, and will attach a webhook to your Github repository to the ApiGateway endpoint. Create a Pull Request on your project, and see the magic be invoked 😎 … Profit!

Example video (nay, gif!)

In the example video below, a PR is created, and a build runs, which fails. Then, a new commit is pushed, a new build is started, and it succeeds! When you click on the ‘details’ link of the PR status, it will take you to the CodeBuild build log.

TODO