Codiga has joined Datadog!

Read the Blog·

Interested in our Static Analysis?

Sign up
← All posts
Julien Delange Wednesday, June 2, 2021

Check your code quality with AWS Build, CodePipeline, and Codiga

Share

AUTHOR

Julien Delange, Founder and CEO

Julien is the CEO of Codiga. Before starting Codiga, Julien was a software engineer at Twitter and Amazon Web Services.

Julien has a PhD in computer science from Universite Pierre et Marie Curie in Paris, France.

See all articles

Integration of Codiga with AWS CodeBuild and AWS CodePipeline means checking your code quality just got a lot easier

Why check your code quality?

It’s important to ensure your code quality stays above a given threshold that guarantees the quality of your software.

However, this is hard:

  • This is a constant effort to inspect the source code and look for potential issues
  • This is often a manually intensive process
  • Software auditors need to make sure all tools are up to date

Thankfully, Codiga lets you analyze your code quality automatically at each commit and present all analysis results in a user-friendly dashboard.

What is a Continuous Integration Pipeline?

A Continuous Integration Pipeline (often referred to as CI Pipeline) is a series of automated steps to automate tests and deployments. Generally, a Continuous Integration Pipeline consists of three major steps as shown below

  1. Clone the code: get the current version of the code from configuration management (GitHub, GitLab, Bitbucket).
  2. Run tests: check that the current version of the code passes all required tests, checking the quality of the software against the requirements.
  3. Deploy: install the new revision of the software in a testing or production environment.

Code Quality Process

Depending on your organization, deployment schedule, and testing policy, your CI pipeline might be more complicated but it generally follows the patterns explained above.

When testing the software, various approaches are used. From running unit tests, integration tests to checking other aspects of the code quality.

This is in this particular step that we want to integrate with Codiga and automatically check that the code quality is above a given threshold. This is done in the testing phase where the quality of the code is being evaluated before any tests. If the code does not satisfy certain requirements, the code is not tested, built, or deployed.

What are AWS CodeBuild and AWS CodePipeline?

AWS CodeBuild is a platform to test and build your project. It runs in AWS and can check out code from various platforms such as GitHub, GitLab, or Bitbucket.

AWS CodePipeline is a platform to sequence a set of actions to check out your code, run tests, and deploy an application. It uses AWS CodeBuild to build software.

Integrating AWS CodeBuild to check code quality

Let’s explain how you can call Codiga in your Aws CodeBuild configuration. We will call Codiga when CodeBuild is invoked to check the Code Quality of your project.

Step 1: Add environment variable to your CodeBuild project

The first step is to define environment variables in your AWS CodeBuild environment. Edit your AWS CodeBuild configuration and its environment.

You need to create the three following variables:

  • CODIGA_API_TOKEN : your Codiga token (API keys)
  • CODIGA_PROJECT_NAME : your Codiga project name

For your API keys, you get them directly on your Codiga once logged.

The environment variables window should look like the picture below.

Add your Codiga token on AWS Build

Call Codiga in AWS CodeBuild

The second step is to add steps in your buildspec.yml CodeBuild file to call Codiga. It is done in two steps

  1. Install the codiga Python package
  2. Call codiga-check-quality to check the code quality of your project. This program will succeed if your program meets the code quality criteria specified or fail if it does not.

For example, if you call codiga-check-quality with the argument --min-quality-score 75 it means that the project should have a minimum quality score of 75.

You can pass multiple criteria to check the code quality:

  • Code Quality Score
  • Ratio of number of violations per line of code
  • Ratio of complex functions
  • Ratio of long functions
  • Ratio of code duplication

You can learn more about all criteria on the citool Python project.

The following file shows an example of how to install the codiga Python package (pip install codiga) and how to check the code quality in the pre_build phase (codiga-check-quality ...).

version: 0.2

phases:
 install:
 commands:

- apt-get update -y
- apt-get install apt-transport-https gnupg -y
- pip install codiga
  finally:
- echo Preliminary tasks done

pre_build:
 commands:

- codiga-check-quality --project "${CODIGA_PROJECT_NAME}" --min-quality-score 75  --sha "${CODEBUILD_RESOLVED_SOURCE_VERSION}" --max-timeout-sec 60

  finally:

  - echo pre-build done

build:
 commands:

- echo Entering the build phase
  finally:
- echo build done
  post_build:
  commands:
- echo Entered the post_build phase...
- echo Build completed on `date`

See the results in action

Once your buildspec.yml is edited and the environment variables are correctly defined, you are ready to test everything! Start a build and you can see the results of the codiga-check-quality in action. When running the tool, it shows the analysis results.

Codiga analysis results in AWS Build

Finally, your AWS CodeBuild can be integrated into a CodePipeline configuration to check your code quality before deploying.

AWS Build results

Wrapping up

In this article, we explained how to integrate Codiga with AWS CodeBuild and CodePipeline to check your code quality at each commit. No need to manually review the code, everything is done automatically by Codiga.

Want to learn more or have any questions?

Are you interested in Datadog Static Analysis?

Sign up