Sign up on AICS on SonarCloud with your Github account.
Click +, then select Analyze new project.
Select your project name and click “Set Up” button.
This is the screen when you first enter the newly added project, copy the organization and project key for later use.
Go to My Account, or click this:
Select Security tab. Generate a new token:
Copy the token. Make sure you copy it now, you won’t be able to see it again!
When you want to run sonar analysis locally or through Travis, you can use this token to represent yourself. Also, this token will be used in the next section.
To run sonar cloud with Travis-ci, you have to (1) add the copied token via web UI on
ASUS AICS on travis, and (2) add config to .travis.yml
.
Go to ASUS AICS on travis. Select your project, and click More options > Settings
We have to add two variables:
Do not display the value in build log!
.travis.yml
First, edit travis’ config file .travis.yml
:
sonarcloud addons:
addons:
sonarcloud:
organization: "asus-aics"
token:
secure: $SONAR_TOKEN
Add these codes snippet in your jobs’ stage:
# Java .travis.yml sample
jobs:
include:
- stage: "Unit Tests & SonarCloud Analysis"
script:
- gradle jacocoTestReport
- gradle sonarqube -Dsonar.login=$SONAR_TOKEN -Dsonar.projectKey=$SONAR_PROJECT_KEY
# Node.js .travis.yml sample
jobs:
include:
- stage: "Run Tests & SonarCloud Analysis"
script:
- npm test
- sonar-scanner -Dsonar.projectKey=$SONAR_PROJECT_KEY
.travis.yml
file to git, commit and push.If you want to run a local testing, use command:
gradle sonarqube -Dsonar.login=<your_own_token>
For IDE lint extension support, see SonarLint for more detail.
Here we provide Java and Node.js for your reference. For more examples in the other coding languages, check it on SonarCloud examples.
Edit your build.gradle
and gradle.properties
files.
build.gradle
org.sonarqube
plugin in build.gradle file. This plugin can activate the static code analysis through your code stack.
Also, apply jacoco
plugin to calculate code coverage report for you whenever you run unit-test jobs.
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Gradle plugin to help analyzing projects with SonarQube
id "org.sonarqube" version "2.7"
// Apply the application plugin to add support for building an application
id 'application'
// Generate code coverage report
id 'jacoco'
}
Add a jacoco test report job to generate the code coverage report.
jacocoTestReport {
// Forcing the execution of 'test' before the execution of 'jacocoTestReport'
dependsOn test
}
To test generating report locally, run command: gradle jacocoTestReport
Usually, the test report path will be generated at build/jacoco/test.exec
.
Checkout HelloJava/build.gradle for full detail.
gradle.properties
Fill in the coverage report path in the last step.
systemProp.sonar.host.url=https://sonarcloud.io
systemProp.sonar.organization=<replace_your_organization_key>
systemProp.sonar.projectKey=<replace_your_project_key>
systemProp.sonar.jacoco.reportPaths=<replace_your_test_report_path>
Remember to edit your .travis.yml file and make sure you provide the corresponding environment variables in your Travis-ci.com project settings page like we mentioned earlier.
If you want to get the test coverage information. Try Istanbul With Mocha. Add “report” command to your package.json scripts section.
{
"scripts": {
"test": "mocha test/*test.js --exit",
"report": "nyc --reporter=lcov --reporter=text-lcov npm test"
}
}
SonarCloud can parse the lcov file generated by Istanbul and assign lcov file path in your sonar-project.properties file.
sonar-project.properties
sonar.projectName=HelloNode
sonar.projectVersion=1.0
sonar.language=js
sonar.sources=src
sonar.tests=test
sonar.exclusions=node_modules/**
# Properties specific to language plugins:
# - For code coverage reports
sonar.javascript.lcov.reportPaths=coverage/lcov.info