docker pull sonarqube
Creating volumes helps prevent the loss of information when updating or upgrading. Use the following commands:
$ docker volume create --name sonarqube_data
$ docker volume create --name sonarqube_logs
$ docker volume create --name sonarqube_extensions
Make sure to use volumes and not bind mounts to prevent issues with plugin population.
Use the following command to run the SonarQube image:
docker run -d --name sonarqube -p 9000:9000 -v sonarqube_data:/opt/sonarqube/data -v sonarqube_extensions:/opt/sonarqube/extensions -v sonarqube_logs:/opt/sonarqube/logs --restart=always sonarqube:community
Access the SonarQube server using the default credentials (admin/admin). Change the password on initial login. Create additional users and a Token for Jenkins integration.
- Install the SonarQube Scanner for Jenkins plugin.
- Create a new credential as secret text in Jenkins.
- Go to "Manage Jenkins" > "Configure System" and add the server URL of SonarQube with a name.
- Go to "Manage Jenkins" > "Global Tool Configuration" and enable SonarQube Scanners.
Update the Jenkinsfile to use the SonarQube scanner. Below is an example syntax for Maven code:
stage('sonar-scanner') {
environment {
SCANNER_HOME = tool 'SonarScanner'
}
steps {
withCredentials([string(credentialsId: 'JenkinsToken', variable: 'sonarLogin')]) {
sh "${SCANNER_HOME}/bin/sonar-scanner -D sonar.host.url=http://xx.xx.xx.xx:9000 -D sonar.login=${sonarLogin} -D sonar.projectVersion=${env.BUILD_NUMBER} -D sonar.projectKey={project.key} -D sonar.sources=./src/main/ -D sonar.tests=./src/test/ -D sonar.language=java -D sonar.java.binaries=."
}
}
}
After the pipeline is executed, access the scan report in the SonarQube server.