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

Docker settings are as follows

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

To avoid restart failure, put in the startup item:

$ cd /etc/profile.d/ 
$ vi 

//Enter the command and save

docker exec <container Id> /bin/sh -c "echo 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>
    <meta charset="UTF-8">
    <title>${ENV, var="JOB_NAME"}-The first ${BUILD_NUMBER}Secondary build log</title>

<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
<table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        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>
        <td><br />
            <b><font color="#0b610b "> build information < / font ></b>
            <hr size="2" width="100%" align="center" /></td>
                <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>

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

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


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

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  -->
 <!--sonarqube address -->
        <!-- sonarqube Login account -->
		 <!-- sonarqube Login password  -->
  1. Construction Engineering
Click new task-Build a freestyle project
![Insert picture description here](,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 project name
#Source code type of sonar project
#javajdk version
# Sonar login account
# Sonar login password
#Source path
#Source byte stream path$WORKSPACE 

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