0 Experimental Environment

The server IP
Jenkins server 192.168.1.155
Gitlab server 192.168.3.159
Sonarqube server 192.168.1.151

1 Docker install SonarQube

  1. Create a new directory on the host to mount the Docker data volume
#The directory of the data volume is displayed
cd /home/jenkins/docker_volume
#New Folder
mkdir postgresql sonarqube
cd sonarqube
mkdir data extensions  logs
Copy the code
  1. Docker starts the PostgresQL database
#Pull a PostgresQL database image
docker pull postgres
#Accessing a Data Volume
cd /home/jenkins/docker_volume
#Start an instance of PostgresSQL to create a sonar database
docker run -itd --name postgresql -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -p 5432:5432 -v $PWD/postgresql:/var/lib/postgresql/data  postgres
Copy the code
  1. Edit /etc/sysctl.conf, set vm.max_map_count to 262144, and run sysctl -p
  2. Docker starts sonarqube image and uses -v parameter to map data volume to achieve data persistence
#Pull sonarqube image
docker pull sonarqube
#Start the sonarqube
docker run -itd --name sonarqube -p 9000:9000 --link postgresql:db  \
-e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=sonar -e SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar   \
-v $PWD/sonarqube/data:/opt/sonarqube/data  -v $PWD/sonarqube/extensions:/opt/sonarqube/extensions -v $PWD/sonarqube/logs:/opt/sonarqube/logs   sonarqube
Copy the code

2 Sonar Scanner configuration

Sonar Scanner download address

Configure the/etc/profile

export SONAR_HOME=/home/software/sonar-scanner
export PATH=$PATH:$SONAR_HOME/bin
Copy the code

Source /etc/profile Enables the configuration to take effect

Add automated code scanning to pipeline

3.1 Configuring the Java Language

SonarQube used here is version 8.9 and does not require additional Java plug-ins. Go to Configuration > General Configuration > Language > Select Java

3.2 write a pipeline

Write Sonarqube.groovy in a shared library

package org.devops

// scan
def SonarScan(projectName, projectDesc, projectPath) {
  def sonarHome = "/home/software/sonar-scanner"
  def sonarServer = "http://192.168.1.151:9000/"
  def sonarDate = sh returnStdout: true.script: 'date +%Y%m%d%H%M%S'
  sonarDate = sonarDate - "\n"
  sh """ ${sonarHome}/bin/sonar-scanner -Dsonar.host.url="${sonarServer}" \ -Dsonar.projectKey=${projectName} \ -Dsonar.login=admin \ -Dsonar.password=123456 \ -Dsonar.projectName=${projectName} \ -Dsonar.projectVersion=${sonarDate}  \ -Dsonar.ws.timeout=30 \ -Dsonar.projectDescription=${projectDesc} \ -Dsonar.sources=${projectPath} \ -Dsonar.sourceEncoding=UTF-8 \ -Dsonar.java.binaries=target/classes \ -Dsonar.java.test.binaries=target/test-classes \ -Dsonar.java.surefire.report=target/surefire-reports """
}
Copy the code

Add a new stage in jenkinsfile, named QA, that calls the methods defined in the shared library.

    stage("QA") {
      steps {
        script {
            tools.PrintMes('Code scan'."green")
            sonar.SonarScan("${JOB_NAME}"."${JOB_NAME}"."src")}}}Copy the code

3.3 perform pipeline

Successful assembly line execution:

Successful results of QA:

Add a statement that does not conform to the SonarLint specification to test Sonarqube code scanning

// Test Sonarqube quality scan
private final String message = "test sonarqube";
Copy the code

3.4 Problems encountered

Jenkins Shell execute sonar-scanner prompt problem with command

Bash_profile ~/.bashrc /etc/profile ~/.bashrc /etc/profile ~/.bash_profile ~/.bashrc /etc/profile ~/.bash_profile Solution: Run the ln -sv /home/software/sonar-scanner/bin/sonar-scanner /usr/local/bin/sonar-scanner command on the server

The IP addresses of several servers ping each other

The reason for this problem is unknown. Some IP addresses cannot be pinged through each other (sometimes they can be pinged through, sometimes they cannot be pinged through). Firewalls are also shut down, and they are all on the same network segment.

4 to optimize pipeline

Install SonarQube Scanner for Jenkins

Generate the token Jenkins used on SonarQube: My Account > Security > Token

Configure SonarQube information on Jenkins, the name configured here needs to be used in pipeline writing.

When Jenkins plug-in is used to perform QA, the Jenkins project management page will display the QA results, and a SonarQube icon will be added next to the pipeline items executed. You can directly view the results by linking to SonarQube.