Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps / deployment

Code Quality Check with AWS CodeBuild

5.00/5 (1 vote)
13 Jan 2024CPOL3 min read 4.9K  
A step-by-step guide on how to easily perform code quality checks with AWS CodeBuild.
In this article, we will learn how to link a GitHub repository to AWS CodeBuild and easily run quality checks.

Contents

Introduction

In order to run quality checks, we somehow need to run the code from the repository. We can’t run our code directly in the repository and need some service that will automatically read the code and run it. AWS CodeBuild is our way to go. AWS CodeBuild is designed to build given code and to create built artifacts. However, in this manual, we don’t want to go into details of the entire artifact building process, but we’ll use AWS CodeBuild as a helper service to launch tests on every pull request created or updated.

Image 1

Since we're using GitHub here as an entry point for our quality check, it's should be mentioned that
GitHub is giving us its own tools to do the quality checks and even building the entire pipeline.
However, in this step-by-step guide, we'll look into AWS CodeBuild usage in conjunction with GitHub, to broaden our CI/CD experience.

Prerequisites

You will need:

  • GitHub account
  • AWS account
  • Strong desire to gain practical experience 😀

Code Overview

We will use the following repo: aws-code-build-quality-learning

You need to create/use your own repo, download or fork aws-code-build-quality-learning to your personal GitHub account.

Inside the repo, there is a simple app with very basic example functionality.
And there is also a guide for AWS CodeBuild of the commands to run - buildspec file quality-check.yml.

Let’s take a look at quality-check.yml.

Image 2

Nothing more than instructions for AWS CodeBuild to automate quality checks. You you'd like to learn more about buildspec configuration, see the Build specification reference for CodeBuild

Image 3

AWS CodeBuild Configuration

To create build project, we need to follow the steps below:

Open AWS CodeBuild and push Create project.

https://us-east-2.console.aws.amazon.com/codesuite/codebuild/start?region=us-east-2

Image 4

Project name: "aws-code-build-quality-learning" (can be your own name, if you will)

Graphical user interface, text, application, emailDescription automatically generated

Connecting via OAuth is a simple way, however it may not work for unknown reasons. In this case, you’ll have to go to your GitHub repo settings and issue a personal access token.

Graphical user interface, text, application, emailDescription automatically generated

Then, select the repository. All existing repositories of your GitHub account will be listed in the dropdown.

Graphical user interface, text, applicationDescription automatically generated

Next step is an important one. We need to check Report build statuses in order to see build status in our pull request. Otherwise, the quality check process won’t be displayed in the PR at GitHub side.

Graphical user interface, text, applicationDescription automatically generated

We also have to specify when the quality check build is to trigger.

Graphical user interface, text, application, emailDescription automatically generated

The following setting must be specified. It doesn’t require extra explanations here.

Image 10

Now we need to reference the buildspec file we have already learnt about.

buildspecs/quality-check.yml is the path to quality-check.yml in our source code.

Graphical user interface, text, applicationDescription automatically generated

Build project created, but no builds so far.

Graphical user interface, text, applicationDescription automatically generated

It’s about time we made a pull request.

Creating Pull Request to Trigger Quality Check

Creating a new feature branch:

git checkout -b feature-branch (no -b option if branch already exist)

Image 13

A single comment line can help us see if quality check is triggering on a PR update.

adding "//feature test change 1" comment to the app.js

JavaScript
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/api/v1/info', (req, res) => {
  res.send({ application: 'sample-app', version: '1' });
});
app.post('/api/v1/getback', (req, res) => {
  res.send({ ...req.body });
});
app.listen(80, () => console.log(`Listening on: 80`));
//feature test change 1

Committing and pushing:

git add .

git commit -m 'add a change to trigger quality check'

git push origin feature-branch

Image 14

Creating PR

Graphical user interface, applicationDescription automatically generated

Image 16

Image 17

Image 18

Quote:

The thing is that we can merge pull requests while the quality check is still running. This happens because we haven’t set branch protection. Branch protection is only available in the corporate GitHub subscription. For the sake of example we will leave it as is.

AWS CodeBuild console. Waiting for the build to complete...

Image 19

⚠ Note that CodeBuild gives us 100 minutes of free execution per month as part of the free tier. Although the execution of our script takes less than 1 minute, it is worth being careful in case a hang occurs and the execution time increases. In this case, it is recommended to manually stop the process.

Image 20

The result is ‘All checks have passed’

Conclusion

We found another way to build and check the quality of our code. We learned that we can use GitHub in conjunction with AWS CodeBuild for this purpose. This opens up the possibility to use the rest of Amazon's services to deploy our code, and to scale our application infinitely.

History

  • 13th January, 2024: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)