Integrating AWS CodeBuild with GitHub

Tikiri Diasena
4 min readJun 30, 2022

--

Most Software developers use GitHub and Amazon Web Services for their professional or personal projects. In this article I’m going to discuss how to connect those two, so that a GitHub repository can be deployed in an AWS CodeBuild.

AWS Codebuild is a continuous integration service that provides continuous scaling and the ability to process multiple builds concurrently. When the GitHub is integrated with AWS CodeBuild, each time the GitHub source code is updated, a build is run on the CodeBuild where the code is compiled, and tests and other integrated services are also run. AWS creates this connection with GitHub through a Webhook.

What is an AWS Webhook?

A Webhook in AWS is as the name implies a link between AWS and Git. It deploys an HTTPS endpoint which you can use as the link. With a Webhook in place, each time a Git user pushes a commit, your repository is automatically retrieved, zipped, and uploaded to an Amazon Simple Storage System (Amazon S3) bucket. AWS CodeBuild can be then configured to use this S3 bucket as a source.

Following is a step-by-step guide to integrate a GitHub repository with AWS CodeBuild.

Prerequisites:

  • An AWS account with a CodeBuild Project. You can create a new AWS account by following the instructions given here. AWS CodeBuild project can be created by following any of the four methods shown here.
  • GitHub Repository with Admin access.

Step-by-step Process

  1. Configure AWS CodeBuild

First click the Edit button in the Source section under Build details in the AWS CodeBuild, the following form (fig: CodeBuild 1) can be obtained. Here, we have to set the Source Provider to GitHub.

After changing the Source provider, there’s an option to select Repository type. From here we have to select Repository in my GitHub account since we need to add Webhooks. (Webhooks cannot be applied to Public repositories). Then add the new repository URL under the GitHub repository and connect to GitHub using a Personal Access Token or OAuth.

fig: CodeBuild 1

Below the Source section there’s the Primary source webhook events section. In here check the Rebuild every time a code change is pushed to this repository option to enable Webhook. Then Webhook events can be added as per requirement. In this example (fig: CodeBuild 2), I’ve have added 4 event types related to PULL_REQUESTS. This means each time a Pull Request is created, updated, reopened or merged, a build is triggered in AWS CodeBuild.

fig: CodeBuild 2

Then click Update source and it would show the updated build details as follows (fig: CodeBuild 3).

fig: CodeBuild 3

2. Configure GitHub Repository

The Webhook is auto generated by AWS and it can be seen in the GitHub repository settings as follows (fig: CodeBuild 4).

fig: CodeBuild 4

Finally click the Edit button next to the new Webhook to configure it according to the requirement of a PR-hook. In here, Pushes checkbox is also selected by default. This would trigger the CodeBuild every time new code is pushed to the GitHub repository. Therefore, uncheck that and make sure that only the Pull requests checkbox is selected (fig: CodeBuild 5).

fig: CodeBuild 5

Conclusion

This article described how to integrate a GitHub repository with AWS CodeBuild by following a few simple steps. Similarly, GitHub can be integrated to other services in AWS such as AWS CodePipeline and AWS CodeDeploy. These integrations can be very useful for projects that need automated and continuous deployments.

--

--