How to trigger AWS CodeBuild jobs for selective file changes in AWS CodeCommit
Unnecessary build pipelines cause unnecessary CPU and storage consumption. If you’re managing your application source code and your documentation files in the same repository, you might want the flexibility to build only the files that have changed. For example, the application doesn’t need to be rebuilt every time the documentation files change.
Version control process is an important requirement in the software development model. Continuous integration (CI) is a DevOps software development practice where developers regularly merge their code changes into a central repository. After a merge, automated builds and tests are run. The key goals of CI are to find and address bugs quicker, improve software quality, and reduce the time it takes to validate and release software updates.
App teams often require pipeline build times to be shorter so product features can be delivered faster and aligned with the business needs. They usually manage their application source code (like files with extensions .py, .java, .c, etc.) along with the required documentation files (like files with extensions .md, .docx, .pdf, etc.) in the same repository. A build job corresponding to this repository builds and publishes the source code and the documentation files as artifacts for deployment. Any change in the documentation files doesn’t change the functionality of the application code, but it still triggers an unnecessary build pipeline.
Consider a scenario in which an CodeCommit repository has a Dockerfile and documentation files. You can use the CodeBuild job to build and publish a new Docker image to Amazon Elastic Container Registry (Amazon ECR) by identifying the modification to the Dockerfile with the help of a Lambda function.
|About this blog post|
|Time to read||7 minutes|
|Time to complete||15-20 minutes
|Cost to complete||~ $0 to test the solution
|Learning level||Advanced (300)|
|AWS services||AWS CodeCommit
The following diagram shows how AWS services are used for this demonstration.
You can configure the CodeCommit repository so that code pushes, or other events, trigger actions, such as sending a notification from Amazon Simple Notification Service (Amazon SNS) or invoking a function in Lambda. You can create up to 10 triggers for each CodeCommit repository. The solution involves the following steps.
- The developers of an application make changes to the source code or the documentation.
- The CodeCommit repository is configured so that code pushes or other events trigger actions, such as invoking a Lambda function.
- The Lambda function performs the following steps:
- Uses the GetCommit operation in the CodeCommit API to get the latest commit.
- Uses the GetDifferences operation to get a list of each file that was added, modified, or deleted.
- From the list of files identified above, checks whether the Dockerfile is present.
- If yes, then triggers the CodeBuild job to build the Docker image. If no, then no action is taken.
- The CodeBuild job builds the Docker image based on the Dockerfile and pushes the created image to the Amazon ECR repository.
The Lambda code is written in Python using the Boto3 libraries. You can find the example source code in aws-codecommit-selective-build-trigger repository.
Provisioning the AWS resources
It is time to deploy the solution using an AWS CloudFormation template.
- Download the source code from the aws-codecommit-selective-build-trigger GitHub repository.
- Sign in to the AWS Management Console and choose the AWS Region of your choice.
- Create an Amazon Simple Storage Service (Amazon S3) bucket. For information, see How Do I Create an S3 Bucket? in the AWS documentation.
- Upload the Lambda deployment package,
lambda.zip, to the S3 bucket that you created.
For this example, we created an S3 bucket named
codecommit-selective-build in the N. Virginia (us-east-1) Region and used the console to upload the deployment package from the GitHub repository to the S3 bucket.
Creating the stack
- In the AWS CloudFormation console, choose Create stack.
- On the Specify template page, choose Upload a template file, and then choose the aws-codecommit-selective-build-trigger.yml template, downloaded from the GitHub repository.INSERT IMAGE
- Specify the following parameters:
- Stack name:
CodecommitSelectiveBuildStack(You can use your own stack name, if you prefer.)
codecommit-selective-build(This is the name of the S3 bucket where you put the sample code.)
lambda.zip(This is the key name of the sample code S3 object. The object should be a zip file.)
sampletest(The name of your CodeCommit repository.)
- Stack name:
- Choose Next and again Next.
- On the Review page, under Capabilities, choose the following option:I acknowledge that AWS CloudFormation might create IAM resources with custom names.
- Choose Create stack to begin the stack creation process. When the stack creation is complete, the resources that were created are displayed on the Resources tab. The stack creation takes approximately 5-7 minutes.
The stack creates an CodeCommit repository (
sampletestcoderepo), which is configured for invoking a Lambda function (
sampletest-CodeBuild-Job-Lambda-Trigger) when commits are pushed to it.
Testing the solution
Now, you can try to push a commit to the remote CodeCommit repository.
- Clone the CodeCommit repository
sampletestcoderepoto your local computer. For information, see Connect to an AWS CodeCommit Repository in the AWS documentation. The following example shows how to clone a repository named
sampletestcoderepoin the US East (N. Virginia) Region.
- Enter the cloned folder and create a README.md file.
- Add and commit this file change. Committing this file does not trigger a build process.
- Push the commit to the remote CodeCommit repository.
Look for the following output.
- Now, to trigger the build, create a basic Dockerfile.
- Add and commit the following file change.
- Push the commit to the remote CodeCommit repository.
After the local commit has been pushed to the remote CodeCommit repository
sampletestcoderepo, the Lambda
sampletest-CodeBuild-Job-Lambda-Trigger function detects the file addition, and it triggers the CodeBuild job
sampletest-CodeBuild-Job. The CodeBuild job processes the Dockerfile and creates the Docker image, which is then pushed to the Amazon ECR repository
sampletest-imagerepo. The following screenshot shows a sample CodeBuild run output.
To avoid additional infrastructure costs from the examples described in this post, be sure to delete the stack. On the AWS CloudFormation console, select the stack that you created, and then choose Delete. This automatically deletes the resources that it created, including CodeCommit repositories, IAM roles/policies, Lambda functions, and CodeBuild projects.
In this blog post, we showed you how to build a sample solution that triggers build jobs based on the files that are modified in the CodeCommit repository. Thus, the solution restricts unnecessary builds from getting triggered, which avoids unnecessary CPU and storage consumption.
You can find the example CloudFormation template and Lambda function, demonstrated in this blog post, in the aws-codecommit-selective-build-trigger GitHub repository. Using the sample code, you can customize the Lambda code to trigger CodeBuild job for specific files or file extensions.
If you have questions or other feedback about this example, please open an issue or submit a pull request.