How To Trigger A CodeBuild Build Using GitHub Webhook

How To Trigger A CodeBuild Build Using GitHub Webhook

How To Trigger A CodeBuild Build Using GitHub Webhook

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to schedule a codebuild build using a trigger.

https://cloudaffaire.com/how-to-schedule-an-aws-codebuild-using-trigger/

In this blog post, we will discuss how to trigger a CodeBuild build using GitHub Webhook. You can use GitHub webhook events to trigger a CodeBuild build. For example, suppose your source code is hosted in GitLab and you want to perform a quick build using AWS CodeBuild before accepting any pull request to check if your code is ok after the change. You can use webhook filter groups to specify which GitHub webhook events trigger a build. For example, you can specify that a build is triggered for specified branches only. You can create one or more webhook filter groups to specify which webhook events trigger a build. A build is triggered if all the filters on one or more filter groups evaluate to true.

How To Trigger A CodeBuild Build Using GitHub Webhook:

Requirements:

AWS CLI installed and configured. You can follow the below blog post to install and configure AWS CLI.

https://cloudaffaire.com/how-to-install-aws-cli/

https://cloudaffaire.com/how-to-configure-aws-cli/

Git installed and configured.

https://cloudaffaire.com/category/devops/git/

One GitHub account.

Step 1: Create your GitHub access token.

In order for CodeBuild to communicate with GitHub, you need to set up an access token for GitHub in AWS CodeBuild. For GitHub, your personal access token must have the following scopes.

  • repo: Grants full control of private repositories.
  • repo:status: Grants access to commit statuses.
  • admin:repo_hook: Grants full control of repository hooks.

Follow the below steps to get your GitHub access token.

Step 1.a: Login to your GitHub account and click on ‘Settings’. Navigate to ‘Developer settings’ and click on ‘Personal access tokens’ and finally click ‘New GitHub App’.

How To Trigger A CodeBuild Build Using GitHub Webhook

Step 1.b: Provide a name and select the access scope and click on ‘Generate token’.

How To Trigger A CodeBuild Build Using GitHub Webhook

Your GitHub access token generated successfully. Copy and save the access token in a secure place.

How To Trigger A CodeBuild Build Using GitHub Webhook

Step 2: Create a private repository in your GitHub account and upload the source code that you want to build using CodeBuild. If you don’t have any source code, you can use the sample code provided by AWS from the below repo.

https://github.com/CloudAffaire/CodeBuild

Step 3: Add GitHub access token in AWS CodeBuild

Step 4: Create an IAM role for AWS CodeBuild.

Step 5: Create an S3 bucket to store your build artifacts.

Step 6: Create a CodeBuild build project.

Note: You will get charged for each build minute, 120 minutes free for the free tier with small compute (used in this demo)

Step 6: Create a webhook for GitHub.

 

How To Trigger A CodeBuild Build Using GitHub Webhook

Note: AWS CodeBuild allows setting up of different conditions for your build trigger using GitHub webhook. For simplicity, I am setting the condition for any push event in GitHub. You can refer to AWS documentation to get a complete list of filters.

https://docs.aws.amazon.com/codebuild/latest/userguide/github-webhook.html

Step 7: Push a change in your GitHub repository.

Note: This will trigger a CodeBuild build of your GitHub project using GitHub webhook.

How To Trigger A CodeBuild Build Using GitHub Webhook

Step 8: Get your CodeBuild build project logs and artifacts.

Step 9: Cleanup.

Hope you have enjoyed this blog post. To get more details on AWS CodeBuild, please refer below AWS documentation

https://docs.aws.amazon.com/codebuild/index.html

 

This Post Has 2 Comments

  1. We recommend that you use a filter group to specify which GitHub users can trigger a build in a public repository. This can prevent a user from triggering an unexpected build. For more information, see GitHub webhook events . If a CodeBuild information page is displayed, choose Create build project. Otherwise, on the navigation pane, expand Build, choose Build projects, and then choose Create build project .

  2. If a CodeBuild information page is displayed, choose Create build project. Otherwise, on the navigation pane, expand Build, choose Build projects, and then choose Create build project . Enter a name for this build project. Build project names must be unique across each AWS account. You can also include an optional description of the build project to help other users understand what this project is used for.

Leave a Reply

Close Menu