Integrating SonarQube as a pull request approver on AWS CodeCommit
Integrating SonarQube as a pull request approver on AWS CodeCommit
On Nov 25th, AWS CodeCommit launched a new feature that allows customers to configure approval rules on pull requests. Approval rules act as a gate on your source code changes. Pull requests which fail to satisfy the required approvals cannot be merged into your important branches. Additionally, CodeCommit launched the ability to create approval rule templates, which are rulesets that can automatically be applied to all pull requests created for one or more repositories in your AWS account. With templates, it becomes simple to create rules like “require one approver from my team” for any number of repositories in your AWS account.
A common problem for software developers is accidentally or unintentionally merging code with bugs, defects, or security vulnerabilities into important master branches. Once bad code is merged into a master branch, it can be difficult to remove. It’s also potentially costly if the code is deployed into production environments and causes outages or other serious issues. Using CodeCommit’s new features, adding required approvers to your repository pull requests can help identify and mitigate those issues before they are merged into your master branches.
The most rudimentary use of required approvers is to require at least one team member to approve each pull request. While adding human team members as approvers is an important part of the pull request workflow, this feature can also be used to require ‘robot’ approvers of your pull requests, and you can trigger them automatically on each new or updated pull request. Robotic approvers can help find issues that humans miss and enforce best practices regarding code style, test coverage, and more.
Customers have been asking us how we can integrate code review tools with AWS CodeCommit pull requests. I encourage you to check out Amazon CodeGuru Reviewer, which is a service that uses program analysis and machine learning to detect potential defects that are difficult for developers to find and recommends fixes in your Java code, and was launched in preview at the AWS Re:Invent 2019 conference. Another popular tool is SonarQube, which is an open-source platform for performing code quality analysis. It helps detect defects, bugs, and security vulnerabilities in your pull requests. This blog post shows you how to integrate SonarQube into the pull requests workflow.
This post shows…
|Time to read||10 minutes|
|Time to complete||20 minutes|
|Cost to complete (estimated)||$0.40/month for secret, ~$0.02 per build on CodeBuild. $0-1 for CodeCommit user depending on current free tier status. (at publication time)|
|Learning level||Intermediate (200)|
|Services used||AWS CodeCommit, AWS CodeBuild, AWS CloudFormation, Amazon Elastic Compute Cloud (EC2), AWS CloudWatch Events, AWS Identity and Access Management, AWS Secrets Manager|
In this solution, you create a CodeCommit repository that requires a successful SonarQube quality analysis before pull requests can be merged. You can create the required AWS resources in your account by using the provided AWS CloudFormation template. This template creates the following resources:
- A new CodeCommit repository, containing a starter Java project that uses the Apache Maven build system, as well as a custom buildspec.yml file to facilitate communication with SonarQube and CodeCommit.
- An AWS CodeBuild project which invokes your SonarQube instance on build, then reports the status of the analysis back to CodeCommit.
- An Amazon CloudWatch Events Rule, which listens for pullRequestCreated and pullRequestSourceBranchUpdated events from CodeCommit, and invokes your CodeBuild project.
- An AWS Secrets Manager secret, which securely stores and provides the username and password of your SonarQube user to the CodeBuild project on-demand.
- IAM roles for CodeBuild and CloudWatch events.
Although this tutorial showcases a Java project with Maven, the design principles should also apply for other languages and build systems with SonarQube integrations.
The following diagram shows the flow of data, starting with a new or updated pull request on CodeCommit. CloudWatch Events listens for these events and invokes your CodeBuild project. The CodeBuild container clones your repository source commit, performs a Maven install, and invokes the quality analysis on SonarQube, using the credentials obtained from AWS Secrets Manager. When finished, CodeBuild leaves a comment on your pull request, and potentially approves your pull request.
For this walkthrough, you require:
- An AWS account
- A SonarQube server instance (Optional setup instructions included if you don’t have one already)
SonarQube instance setup (Optional)
This tutorial shows a basic setup of SonarQube on Amazon EC2 for informational purposes only. It does not include details about securing your Amazon EC2 instance or SonarQube installation. Please be sure you have secured your environments before placing sensitive data on them.
- To start, get a SonarQube server instance up and running. If you are already using SonarQube, feel free to skip these instructions and just note down your host URL and port number for later. If you don’t have one already, I recommend using a fresh Amazon EC2 instance for the job. You can get up and running quickly in just a few commands. I’ve selected an Amazon Linux 2 AMI for my EC2 instance.
- Download and install the latest JDK 11 module. Because I am using an Amazon Linux 2 EC2 instance, I can directly install Amazon Corretto 11 with yum.
$ sudo yum install java-11-amazon-corretto-headless
- After it’s installed, verify you’re using this version of Java:
$ sudo alternatives --config java
- Choose the Java 11 version you just installed.
- Download the latest SonarQube installation.
- Copy the zip-file onto your Amazon EC2 instance.
- Unzip the file into your home directory:
$ unzip sonarqube-8.0.zip -d ~/
This will copy the files into a directory like
Now, start the server!
$ ~/sonarqube-8.0/bin/linux-x86-64/sonar.sh start
This should start a SonarQube server running on an address like http://<instance-address>:9000. It may take a few moments for the server to start.
Follow these steps to create automated pull request approvals.
Create a SonarQube User
Get started by creating a SonarQube user from your SonarQube webpage. This user is the identity used by the robot caller to your SonarQube for this workflow.
- Go to the Administration tab on your SonarQube instance.
- Choose Security, then Users, as shown in the following screenshot.
- Choose Create User. Fill in the form, and note down the Login and Password You will need to provide these values when creating the following AWS resources.
- Choose Create.
Create AWS resources
For this integration, you need to create some AWS resources:
- AWS CodeCommit repository
- AWS CodeBuild project
- Amazon CloudWatch Events rule (to trigger builds when pull requests are created or updated)
- IAM role (for CodeBuild to assume)
- IAM role (for CloudWatch Events to assume and invoke CodeBuild)
- AWS Secrets Manager secret (to store and manage your SonarQube user credentials)
I have created an AWS CloudFormation template to provision these resources for you. You can download the template from the sample repository on GitHub for this blog demo. This repository also contains the sample code which will be uploaded to your CodeCommit repository. The contents of this GitHub repository will automatically be copied into your new CodeCommit repository for you when you create this CloudFormation stack. This is because I’ve conveniently uploaded a zip-file of the contents into a publicly-readable S3 bucket, and am using it within this CloudFormation template.
- Download or copy the CloudFormation template from GitHub and save it as
template.yamlon your local computer.
- At the CloudFormation console, choose Create Stack (with new resources).
- Choose Upload a template file.
- Choose Choose file and select the
template.yamlfile you just saved.
- Choose Next.
- Give your stack a name, optionally update the CodeCommit repository name and description, and paste in the username and password of the SonarQube user you created.
- Choose Next.
- Review the stack options and choose Next.
- On Step 4, review your stack, acknowledge the required capabilities, and choose Create Stack.
- Wait for the stack creation to complete before proceeding.
- Before leaving the AWS CloudFormation console, choose the Resources tab and note down the newly created CodeBuildRole’s Physical Id, as shown in the following screenshot. You need this in the next step.
Create an Approval Rule Template
Now that your resources are created, create an Approval Rule Template in the CodeCommit console. This template allows you to define a required approver for new pull requests on specific repositories.
- On the CodeCommit console home page, choose Approval rule templates in the left panel. Choose Create template.
- Give the template a name (like
Require SonarQube approval) and optionally, a description.
- Set the number of approvals needed as 1.
- Under Approval pool members, choose Add.
- Set the approver type to Fully qualified ARN. Since the approver will be the identity obtained by assuming the CodeBuild execution role, your approval pool ARN should be the following string:
arn:aws:sts::<Your AccountId>:assumed-role/<Your CodeBuild IAM role name>/*
The CodeBuild IAM role name is the Physical Id of the role you created and noted down above. You can also find the full name either in the IAM console or the AWS CloudFormation stack details. Adding this role to the approval pool allows any identity assuming your CodeBuild role to satisfy this approval rule.
- Under Associated repositories, find and choose your repository (
PullRequestApproverBlogDemo). This ensures that any pull requests subsequently created on your repository will have this rule by default.
- Choose Create.
Update the repository with a SonarQube endpoint URL
For this step, you update your CodeCommit repository code to include the endpoint URL of your SonarQube instance. This allows CodeBuild to know where to go to invoke your SonarQube.
You can use the AWS Management Console to make this code change.
- Head back to the CodeCommit home page and choose your repository name from the Repositories list.
- You need a new branch on which to update the code. From the repository page, choose Branches, then Create branch.
- Give the new branch a name (such as
update-url) and make sure you are branching from master. Choose Create branch.
- You should now see two branches in the table. Choose the name of your new branch (
update-url) to start browsing the code on this branch. On the
update-urlbranch, open the
buildspec.ymlfile by choosing it.
- Choose Edit to make a change.
- In the
pre_buildsteps, modify line 17 with your SonarQube instance url and listen port number, as shown in the following screenshot.
- To save, scroll down and fill out the author, email, and commit message. When you’re happy, commit this by choosing Commit changes.
Create a Pull Request
You are now ready to create a pull request!
- From the CodeCommit console main page, choose Repositories and PullRequestApproverBlogDemo.
- In the left navigation panel, choose Pull Requests.
- Choose Create pull request.
masteras your destination branch, and your new branch (
update-url) as the source branch.
- Choose Compare.
- Give your pull request a title and description, and choose Create pull request.
It’s time to see the magic in action. Now that you’ve created your pull request, you should already see that your pull request requires one approver but is not yet approved. This rule comes from the template you created and associated earlier.
You’ll see images like the following screenshot if you browse through the tabs on your pull request:
Thanks to the CloudWatch Events Rule, CodeBuild should already be hard at work cloning your repository, performing a build, and invoking your SonarQube instance. It is able to find the SonarQube URL you provided because CodeBuild is cloning the source branch of your pull request. If you choose to peek at your project in the CodeBuild console, you should see an in-progress build.
Once the build has completed, head back over to your CodeCommit pull request page. If all went well, you’ll be able to see that SonarQube approved your pull request and left you a comment. (Or alternatively, failed and also left you a comment while not approving).
The Activity tab should resemble that in the following screenshot:
The Approvals tab should resemble that in the following screenshot:
Suppose you need to make a change to your pull request. If you perform updates to your source branch, the approval status will be reset. As your push completes, a new SonarQube analysis will begin just as it did the first time.
Once your SonarQube thresholds are satisfied and your pull request is approved, feel free to merge it!
To avoid incurring additional charges, you may want to delete the AWS resources you created for this project. To do this, simply navigate to the CloudFormation console, select the stack you created above, and choose Delete. If you are sure you want to delete, confirm by choosing Delete stack. CloudFormation will delete all the resources you created with this stack.
In this tutorial, you created a workflow to watch for pull request changes to your repository, triggered a CodeBuild project execution which invoked your SonarQube for code quality analysis, and then reported back to CodeCommit to approve your pull request.
I hope this guide illustrates the potential power of combining pull request approval rules with robotic approvers. While this example is specifically about integrating SonarQube, the same pattern can be used to invoke other robotic approvers using CodeBuild, or by invoking an AWS Lambda function instead.
This tutorial was written and tested using SonarQube Version 8.0 (build 29455).