web analytics

Michelle & Automated Testing 4: How to Integrate Jenkins, Sonar and Github for your Pull Request?

The purpose to integrate Jenkins, Sonar and GitHub is to run Sonar and show Sonar report for each pull request of GitHub.

  1. Install Sonar GitHub plugin
  • Login as admin into your Sonar server
  • As the screenshot shows, go to System -> Update Center, and click the “Available” tab, find “GitHub” plugin, and then clicked the “install” button at the right side.

  • Sonar server will require to be restarted
  • Go to “Configuration” -> GitHub, you will see the setting as the following screenshot shows, leave them default.

  1. Generate GitHub token
  • Go to you GitHub account -> Settings -> Developer settings -> Personal access token
  • Select the scope and then generated the token.

  1. Set up Jenkins job
  • Create a new Jenkins job
  • In the section “Source Code Management”, set the GitHub repository information here. Branch info leave to blank.
  • In the section “Build Trigger”, set the trigger as the following screenshot shows, then once there is a new push to ANY branch, the job will be triggered.

  • Run the following scripts, the scripts will get the all open pull request id, and run Sonar check for your pull request
#!/bin/bash

repo_name=$1

ids=`curl -H “Authorization:token your_token” https://api.github.com/repos/your_organization/$repo_name/pulls?state=open | grep “\”commits_url\”: \”https://api.github.com/repos/your_organization/$repo_name/pulls” | sed ‘s/”commits_url”: “https:\/\/api.github.com\/repos\/your_organization\/’$repo_name’\/pulls\///g’ | sed ‘s/\/commits”,//g’`

echo $ids

for id in $ids

do

echo $id

mvn sonar:sonar -Dsonar.analysis.mode=issues

-Dsonar.github.pullRequest=$id

-Dsonar.github.repository=https://github.com/your_organization/$repo_name.git

-Dsonar.github.oauth=your_token

-Dsonar.host.url=http://localhost:9000/

echo “sonar is done”

done!

You may also like

(Views: 2.4k)

Michelle Xie

Michelle Xie is a Quality Assurance Analyst at PrintFleet in Kingston, Canada. Michelle is a passionate software engineer and a software QA expert. Most of her efforts focus on automation test best practices and common tech issues that can be avoided in automation tests, and she hopes to continue sharing her technical expertise through ECM TechNews.

Leave a Reply

Your email address will not be published. Required fields are marked *