Bamboo CI with SonarQube and Django

This post describes how to use Atlassian Bamboo to create a simple Django build plan and scan the source code with SonarQube when a pull request is created in Bitbucket. It uses two cheap apps (if you are a small company) that are available on the Atlassian Markeyplace:

Sonar for Bitbucket

and

Sonar for Bamboo

Setup

1) Install Bamboo, SonarQube, and SonarScanner

2) Install Sonar for Bitbucket Server and Sonar for Bamboo from the Atlassian Marketplace

Configure

1) Create a Project and Plan in Bamboo

2) Click the plan then click "Actions > Configure plan"

3) Create a job or click "Default Job" and pick repo to checkout in the "Source Code Checkout" task

4) Create a script file in a directory of your choice. The script below will create a virtual environment, activate it, pip install requirements.txt, pip install the unittest-xml-reporting package that Bamboo needs to parse unit tests, and finally run the unit tests.

```bash
#!/bin/bash

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --root=${bamboo.build.working.directory}/tmp --ignore-installed
export PATH=${bamboo.build.working.directory}/tmp/usr/local/bin:$PATH
export PYTHONPATH=${bamboo.build.working.directory}/tmp/usr/local/lib/python2.7/dist-packages:$PYTHONPATH
pip install --root=${bamboo.build.working.directory}/tmp --ignore-installed virtualenv
python3 -m venv venv
source venv/bin/activate
echo Pip is located `which pip`
pip install -r requirements.txt
pip install unittest-xml-reporting
python manage.py test --noinput --testrunner="xmlrunner.extra.djangotestrunner.XMLTestRunner"
```

5) Add new task of type "Command", "Add new executable", and point it to the script created above

6) Now, add a new task of type "SonarScanner", "Add new executable", and point it to the SonarScanner home folder

7) Since this is setup with SonarScanner, add the projectKey -Dsonar.projectKey=MyRepo to "Additional Parameters", select "Auto branch" and "Use legacy branching" (legacy branching is only required if using the free Community Edition since branching is not supported) then save

This plan can now be ran manually.

Bamboo Trigger and Bitbucket Integration

To get the build plan to run when a pull request is created, follow these steps:

1) Click "Create plan branch"

2) Select the branch that should trigger the build plan when a pull request is created (i.e. dev) and click "Create"

3) Give it a name (i.e. DEV PR) and click save

4) Finally, fo to "Plan Configuration", click the "Triggers" tab, and verify the "Bitbucket Server repository triggered" trigger is there and "MyRepo is checked." If not, add the trigger.

5) Manually run the Plan so the dev repo will be pulled and tested (this is required for a baseline if you are using Sonar for Bitbucket Server)

6) Go to your Bitbucket repo and click "Repository settings" on the bottom left. Name the server, select the master project you just ran, add the "Project base key" (i.e. MyRepo), and select "Compatibility mode" since SonarScanner is being used then save

Now when a pull request is created the plan should automatically run and you should see the results in Bitbucket.

May 23, 2019, 4:54 p.m.
Load Comments