Jenkins sonar continuously integrates, detects and sends build emails

Keywords: Linux Docker jenkins IDE

Jenkins sonar continuously integrates, detects and sends build emails

1. Preliminary preparation

1. Deploy jenkins services
2.Jenkins server modifies the domain name resolution of gitlab
The modification command of Linux is as follows:

 vi  /etc/hosts
 192.168.10.248 gitlab.rivamed.cn/

Docker settings are as follows

docker exec <container Id> /bin/sh -c "echo 172.19.12.249 d7739aed6d90 >> /etc/hosts"

To avoid restart failure, put in the startup item:

$ cd /etc/profile.d/ 
$ vi hosts.sh 

//Enter the command and save

docker exec <container Id> /bin/sh -c "echo 172.19.12.249 d7739aed6d90 >> /etc/hosts"

3. Add auto send email configuration
1. Download email plug-in
Through system management → manage plug-ins → optional plug-ins, select Email Extension to download the specified plug-ins.


2. System configuration mail information
Set email information through system management - system settings

3. Configure mail sending template
3.1 setting management email address

3.2 setting sender information and mail template
Set sender information through Extended E-mail Notification in system settings, including setting mail server format, default recipient, mail template, trigger mechanism, etc.

The mail content template can refer to the following template

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>${ENV, var="JOB_NAME"}-The first ${BUILD_NUMBER}Secondary build log</title>
</head>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
      offset="0">
<table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
    <tr>
        This email is sent automatically by the system without reply!<br/>
        Hello, colleagues, the following is ${PROJECT_NAME }Project construction information</br>
        <td><font color="#CC0000 "> build result - ${build_status} < / font ></td>
    </tr>
    <tr>
        <td><br />
            <b><font color="#0b610b "> build information < / font ></b>
            <hr size="2" width="100%" align="center" /></td>
    </tr>
    <tr>
        <td>
            <ul>
                <li>Project Name: ${PROJECT_NAME}</li>
                <li>Build No.: page ${BUILD_NUMBER}Secondary construction</li>
                <li>Trigger reason: ${CAUSE}</li>
                <li>Build status: ${BUILD_STATUS}</li>
                <li>Build log: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                <li>structure  Url :  <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                <li>Working directory: <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
                <li>project  Url :  <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
            </ul>

            <h4><font color="#0b610b "> failure case < / font > < / H4 >
            <hr size="2" width="100%" />
            $FAILED_TESTS<br/>

            <h4><font color="#0B610B">Recently submitted(#$SVN_REVISION)</font></h4>
            <hr size="2" width="100%" />
            <ul>
                ${CHANGES_SINCE_LAST_SUCCESS, reverse=true, format="%c", changesFormat="<li>%d [%a] %m</li>"}
            </ul>
            Detailed submission: <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a><br/>

        </td>
    </tr>
</table>
</body>
</html>

maven uses the sonar plug-in to detect submissions

  1. Maven adds sonarqube configuration information
    Add the sonar plug-in to the source pom file
<plugin>
   <groupId>org.sonarsource.scanner.maven</groupId>
   <artifactId>sonar-maven-plugin</artifactId>
   <version>3.8.0.2131</version>
</plugin>

Add the configuration information of sonarqube in setting.xml in maven installation directory and conf directory

 <!--Add location, profiles Add in sonar Configuration information for  -->
 <profiles>
 <profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
 <!--sonarqube address -->
        <sonar.host.url>http://192.168.111.59:9000/</sonar.host.url>
        <!-- sonarqube Login account -->
		<sonar.login>admin</sonar.login>
		 <!-- sonarqube Login password  -->
		<sonar.password>Aa123456</sonar.password>
    </properties>
</profile>
<profiles>
  1. Construction Engineering
Click new task-Build a freestyle project
![Insert picture description here](https://img-blog.csdnimg.cn/a62d4d52c2454e8d8c1ba8a8e54dc59d.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAUG91bERhaQ==,size_20,color_FFFFFF,t_70,g_se,x_16)
  1. Click Configure to configure the project

  1. Bind git source code
  2. Build trigger mechanism

    Note: the difference between timing build and polling SCM
    The same point: both timing build and polling SCM are triggered by time polling.
    Difference: when polling SCM for construction, it will compare whether the git source code has changed. If there is no change, it will not be built. If it is built regularly, it will not judge whether the git source code has changed, and it will be built directly.
  3. Perform build operations
    When building, select maven build and enter the build command
    dependency:purge-local-repository clean compile sonar:sonar

Note: dependency: purge local repository is used to delete local dependencies and prevent dependency cache

  1. Send email after build

After the build operation, select enable email notification to set mail push

The specific settings are as follows:

Use the external plug-in sonar scanner to detect submission

  1. Jenkins installs the sonar scanner plug-in

Through system management → manage plug-ins → optional plug-ins, search and select sonar scanner to download the specified plug-ins.

  1. System configuration sonar information

Configure the sonacanner tool in system management - global tool configuration

Set the sonarqube data information through system management - system settings

The sonarqube token can be obtained in the sonarqube personal center, as follows:

  1. Building automation engineering

The construction automation project is basically the same as maven's construction steps. You only need to pay attention to adding sonar scanning configuration during construction

The configuration information is described below

#sonar item key value
sonar.projectKey=cn.rivamed.spd:spd-bdm
#sonar project name
sonar.projectName=cn.rivamed.spd:spd-bdm
#Source code type of sonar project
sonar.language=java
#javajdk version
sonar.java.source=1.8
# Sonar login account
sonar.login=admin
# Sonar login password
sonar.password=123456
#Source path
sonar.sources=$WORKSPACE
#Source byte stream path
sonar.java.binaries=$WORKSPACE 

Posted by jackofalltrades on Fri, 17 Sep 2021 07:12:56 -0700