Integrate SonarCloud with AWS CodeCommit and CodeBuild

Harshana Serasinghe
4 min readAug 13, 2020
Header Image
SonarCloud, AWS CodeCommit and AWS CodeBuild logos

What is SonarCloud?

Analyzing Code Quality, Security and Bugs can be identified as a major task that we need to do during or after developing a software application. When the application is bigger and have so much components in it, it is somewhat harder to analyze manually. To solve this issue, We can use a tool like SonarCloud. You can import your GitHub, Bitbucket, Azure DevOps or GitLab repository but it doesn’t directly support AWS CodeBuild or CodeCommit Projects.

In this article, I’m going to guide you on how to integrate SonarCloud with your AWS Codebuild environment.

What you need?

  • AWS Developer Account with Access to CodeCommit and CodeBuild
  • SonarCloud Account

Step #1 — Creating a SonarCloud Project

First you’ve to login to your SonarCloud Account. Then click on the plus icon in the right side and click Analyze new Project

SonarCloud.io — Creating the project

In the next page, click on Create a Project Manually

SonarCloud.io — Selecting manual option

In the next page, enter a project key and display name you like and click Set up.

SonarCloud.io — Setting up the project

It will setup the project. Then click Manually to configure the project manually

SonarCloud.io — Selecting Manual Option

Then Sonarcloud will ask about your build and Operating system. I have selected Other for build and Linux as the OS because CodeBuild projects run on Linux.

SonarCloud.io — Selecting Build Option and OS

After selecting those two options, you will get the SONAR_TOKEN and a command that you can use in an upcoming step. Save the token and a command in a place where you can easily access.

Step #2 — Creating a CodeCommit Project

First, login to your AWS account and then navigate into CodeCommit.

CodeBuild — Dashbaord

Then Click Create Repository > Give the repository a name and Create the repository.

CodeBuild — Creating New Repository

After creating the repository, Clone the repository . After that, you can add your code into the repository.

Here, we need to add a special file to initiate the build process in CodeBuild. It is known as the buildspec.yml file. Create a file called buildspec.yml and paste the below content in it.

CodeBuild — Sample Buildspec

Then change the following values with the values you got when creating the sonarcloud project

  • SONARCLOUD_ORG = Your Sonar Cloud Organization name
  • PROJECT_KEY = Your Sonar Project Key
  • SONARCLOUD_TOKEN = Your Sonar Project Token

Then push the entire codebase to the repository.

Step #3 — Creating CodeBuild Project

Then navigate into CodeBuild and create a project.

CodeBuild — Create CodeBuild Project

When creating the project, select the CodeCommit repository you created earlier. Then click create to create the project.

CodeBuild — Start Build

After creating the project, You can run the build. If everything is correctly configured, the sonar scanner will analyze the project. After everything is completed, you will be able to see the analyses report on Sonarcloud.

SonarCloud.io — Project Analyses

Keep reading about SonarCloud and AWS

--

--

Harshana Serasinghe

In love with tech ever since I got to know what a computer is :)