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.
- 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.
- Generate GitHub token
- Go to you GitHub account -> Settings -> Developer settings -> Personal access token
- Select the scope and then generated the token.
- 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!
- Michelle & Automated Testing 5:Internal API and External API Automation Testing, Lessons Learned - August 15, 2017
- Michelle & Automated Testing 4: How to Integrate Jenkins, Sonar and Github for your Pull Request? - July 27, 2017
- Michelle & Automated Testing 3: Using Maven AWS S3 Repository Instead of Nexus Server - June 28, 2017