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 connect CodeBuild directly to Github. Which is where this project comes in. This project creates an endpoint for your github repository webhook, which is called every time you create/update a Pull Request.
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, so 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:
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
failed, the Github api is called and the PR status will be updated accordingly.
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.
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 seperate account for this.
Deploy the stack:
Create a Pull Request on your project, and see the magic be invoked ðŸ˜Ž
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.