This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

Part reference from the Dark Horse Programmer Java tutorial to automate Deploying Jenkins from environment configuration to project development

Sonar integration

SonaQube profile

SonarQube is an open platform for managing code quality by quickly locating potential or obvious errors in code. Currently support Java, C #, C/C + +, Python, PL/SQL, Cobol, javascript, two kinds of programming languages such as Groovy code quality management and inspection.

The environment

software version
JDK 1.8
MySQL 5.7
SonarQube 7.7

MySQL installation

Use the yum installation mode

  • Centos7 has MariaDB (a branch of MySQL) installed by default, so it is executedyum install mysqlThe following command simply updates the Mariadb database
  • Let’s check Mariadb first
 rpm -qa|grep mariadb
Copy the code
  • Then delete
RPM -e --nodeps nameCopy the code

The installation of the source

Website address: dev.mysql.com/downloads/r…

  • Take a look at the version of your system first
 cat /etc/redhat-release
Copy the code

  • Select the version to download
 wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Copy the code

  • The installation
 sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
Copy the code
  • Select version

    This is because we are installing MySQL5.7, and the default installation selects the current stable version

    So take a look at all MySQL versions of the current yum library

 yum repolist all | grep mysql
Copy the code

  • You can see that version 8.0 is enabled and the others are disabled

    Let’s change it by order

 sudo yum-config-manager --disable mysql80-community
 sudo yum-config-manager --enable mysql57-community
Copy the code
  • Check again to see that the default installation version has been successfully modified

  • View the currently enabled MySQL repository

 yum repolist enabled | grep mysql
Copy the code

Ontology installation and use

  • The installation
 sudo yum install mysql-community-server
Copy the code
  • Start the
 sudo systemctl start mysqld.service
Copy the code
  • Check the status
 sudo systemctl status mysqld.service
Copy the code
  • Viewing the initial password

    After MySQL starts for the first time, the super administrator account root@localhost is created. The initial password is stored in the log file:

 sudo grep 'temporary password' /var/log/mysqld.log
Copy the code

  • The login
 mysql -uroot -p
Copy the code
  • Changing the Initial Password
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password ';Copy the code

If the report prompts ERROR 1819 (HY000): Your password does not satisfy the current policy requirements, it is because the password is too simple

The default password policy of MySQL is to contain more than 8 digits, uppercase and lowercase letters, and special characters

  • Allow root remote access
GRANT ALL PRIVILEGES ON *.* TO 'user '@'%' IDENTIFIED BY' user 'WITH GRANT OPTION;Copy the code

Then FLUSH FLUSH PRIVILEGES;

  • Set the encoding format to UTF-8

    Edit /etc/my.cnf, add the following code to the [mysqld] node:

     character_set_server=utf8
     init-connect='SET NAMES utf8'
    Copy the code

  • Setting startup
 systemctl enable mysqld
 systemctl daemon-reload
Copy the code
  • Create sonar database
 create database sonar;
Copy the code

Let’s see if it works

 show databases;
Copy the code

To install sonar

  • Select the version official website download address
[sonar] [sonar] [sonar] [sonar] [sonar] [sonar] [sonar] /opt/sonar # Change the permissions of the sonar directory and fileCopy the code
  • Modify the configuration file opt/sonarqube- version /conf/ sonara. properties of sonar

    Change the database connection user name and password, and uncomment the connection address in the bottom line of the diagram

    Ps: Sonar listens on port 9000 by default. If port 9000 is occupied, change it.

  • Start the sonar
Sh start # Start su User name./bin/linux-x86-64/sonar. Sh status # Check the status su user name /bin/linux-x86-64/sonar.sh stop # Stop tail -f logs/sonar.log # Check logsCopy the code

  • The login

    The default account is admin/admin

  • Create a key

    Enter the key to generate a string of keys, as other platforms to connect to sonar password, to write down

Integrate in Jenkins

  • Running logic:

Installing a plug-in

  • Install the SonarQube Scanner plug-in

Install SonarQube Scanner

  • Install the SonarQube Scanner automatically in Jenkins

    【Manage Jenkins】->【Global Tool Configuration】->【SonarQube Scanner】

    You can select the latest version

Add credentials

  • Add the SonarQube credentials

    [Manage Jenkins] -> [Manage Credentials] -> [global] ->

    Select the [Secret Test] type, fill in the token key of Sonar, write the credential description and click OK

Configure the global service environment

  • Perform global configuration

    [Manage Jenkins] -> [Configure System] -> [SonarQube Servers]

    Enter the name, deployment address of Sonar, and select the certificate configured in the previous step

    Click Apply and save

Add a SonarQube code review to your project

Nonpipeline type

The first thing I’m going to show you is typing directly in Jenkins

  • In the project configuration, add the Execute SonarQube Scanner build step

Select JDK1.8 and then enter the relevant parameters for sonar Analysis in the Analysis Properties input box as the template is given

Template:

Sonar. ProjectKey =web_demo # this is the name and version ProjectName =web_demo sonar. ProjectVersion =1.0 # Path is relative to the SonarQube uis. sonar sonar-project.properties file. Replace "" by "/" on Windows. # This property is optional if sonar.modules is set. Sonar. Sources =. # Scan file path sonar. Exclusions =**/test/**,**/target/** # Scan file Sonar.java. source=1.8 # Encoding of the source  code. Default is default system encoding sonar.sourceEncoding=UTF-8Copy the code
  • Click Apply Save, then build

  • After construction, you can see the detection structure in Sonar, success

Pipeline type
  • Created under project filesonar-project.propertiesFile, write the same configuration parameters as the previous step, modify the project name

  • Modify the Jenkinsfile script to add the code review step

Stage ('check'){steps{script{scannerHome = tool 'SonarQube-Scanner'} step {script{scannerHome = tool 'SonarQube-Scanner' WithSonarQubeEnv ('SonarQube'){// the scannerHome is configured in the global service environment sh "${scannerHome}/bin/sonar-scanner" // install sonar service environment}}}Copy the code
  • Push to GitLab, trigger build, success

  • Sonar also came back with test results

  • successful