Error message here!

Hide Error message here!


Error message here!


Hide Error message here!


Error message here!



Jenkins + gitlab + sonnarqube build Ci / CD whole process

MPolaris 2021-01-14 13:02:00 阅读数:6 评论数:0 点赞数:0 收藏数:0

1. CI/CD

1.1 CI - Continuous integration

Continuous integration ( Continuous integration , abbreviation CI ) refer to , Frequently ( Many times a day ) Integrate the code into the trunk . The purpose of continuous integration is to make products iterate quickly , At the same time, it can maintain high quality . Its core measure is before the code is integrated into the trunk , Must pass automated tests . As long as one test case fails , You can't integrate . Through continuous integration, teams can quickly move from one function to another , In short , A large part of agile software development is due to continuous integration .


Components of continuous integration

  • An automated build process , Check out code from 、 Compiling and constructing 、 Run the test 、 Result record 、 Test statistics, etc. are all done automatically , No manual intervention required .

  • A code repository , That is, version control software is needed to ensure the maintainability of the code , At the same time, as the material library of the construction process , In general use SVN or Git.

  • A continuous integration server , Jenkins Is a simple configuration and easy to use continuous integration server .

1.2 CD - Continuous deployment

Continuous deployment is based on continuous integration , Automate the process of deployment to the production environment .


2. Overall architecture description

Single project / Front and rear end separation project Solution ( The implemented )


Microservices Solution


3. GitLab Code management service

3.1 understand

GitLab and GitHub Also belongs to the third party base Git Developed works , Free and open source ( be based on MIT agreement ), And Github similar , You can register users , Submit your code at will , add to SSHKey wait . The difference is GitLab It can be deployed to its own server , The database and other information are in their own hands , Suitable for team internal collaborative development , You can't keep the wisdom of your team on someone else's server ? To put it simply, you can GitLab As a personal version of GitHub.

3.2 install (CentOS7)

What I'm using here is Docker-compose install , be based on twang2218/gitlab-ce-zh Chinese version of the mirror

version: '2'
image: 'twang2218/gitlab-ce-zh'
restart: unless-stopped
hostname: ''
TZ: 'Asia/Shanghai'
external_url ''
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_shell_ssh_port'] = 2222
- '8089:80'
- '8443:443'
- '2222:22'
- ./config:/etc/gitlab
- ./data:/var/opt/gitlab
- ./logs:/var/log/gitlab
# Installation dependent
yum -y install policycoreutils openssh-server openssh-clients postfix
# start-up ssh service & Set to boot
systemctl enable sshd && sudo systemctl start sshd
# Set up postfix Boot from boot , And start the ,postfix Support gitlab Sending function
systemctl enable postfix && systemctl start postfix
# to open up ssh as well as http service , Then reload the firewall list
firewall-cmd --add-service=ssh --permanent
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
# You can modify gitlab To configure , The configuration file has been mounted to Linux The host machine
vim /root/docker/gitlab/config/gitlab.rb
3.4 Configuration page
  1. Administrator account
  1. add group

Use admin root Create group , A group can have multiple project branches , You can add development to a group to set permissions , Different groups are different development projects or service modules of the company , Adding different development to different groups can realize the management of development setting permissions .

  1. Create user
  1. Add users to groups

Gitlab Users in the group have 5 There are two different permissions :

Guest: You can create issue、 Comment , Can't read or write version Library

Reporter: You can clone code , Cannot submit ,QA、PM You can give this permission

Developer: You can clone code 、 Development 、 Submit 、push, Normal development can give this permission

Maintainer: You can create projects 、 add to tag、 Protection branch 、 Add project members 、 Editing project , Core development can give this permission

Owner: You can set project access rights - Visibility Level、 Delete the project 、 Migration project 、 Management team members , The development team leader can give this permission

image-20210110020435952 image-20210110020643983
  1. Create projects in groups

  2. Upload the source code to Gitlab Warehouse ( Here we go through SSH The way )

  • Sign in GitLab Private address :

  • To configure SSH Password free login , With SSH The way push,pull Code

    • Download and install Git client
    • open Git Bash
    # To configure Git User name
    git config--global “xxxx”
    # To configure GIt User email
    git config --global “xxxx”
    # See if it exists ssh keys
    cd ~/.ssh
    # If it appears ”No such file or directory”, You need to create a ssh keys
    ssh-keygen -t rsa -C " Your email " # Email address GitLab Email address of account settings
    • Then open the ~/.ssh/ file (~ Represents the user directory , Such as my windows Namely C:\Users\Administrator), Copy the content
    • open gitlab, Search for SSH Key, And paste what you copied in the previous step into Key The corresponding text box , stay Title In the corresponding text box, give this sshkey Set a name , Click on Add key Button
    • Try pulling code and submitting code , At this point, there is no need to enter a password
git clone ssh://git@

4. Jenkins Environment building

4.1 Installation environment (CentOS7)
name Installation package Version number
Jenkins jenkins-2.190.3-1.1.noarch.rpm 2.190.3
JDK 1.8
Maven 3.6.2
4.2 Jenkins Installation and configuration
# Upload the installation package to the server , Installation
rpm -ivh jenkins-2.190.3-1.1.noarch.rpm
# modify Jenkins To configure
vim /etc/syscofig/jenkins
# start-up Jenkins
systemctl start jenkins
4.3 Configuration page
  1. The first entry requires input admin Account password
cat /var/lib/jenkins/secrets/initialAdminPassword
  1. Choose plug-ins to install => Select none => install . To skip plug-in installation , because Jenkins Plug ins need to connect to the default official website to download , It's very slow and it's going to fail , So let's skip the plug-in installation for now

  2. Add an administrator account , And enter Jenkins backstage

# Username
# Password
# Full name
  1. JenKins URL The default can be
4.4 Jenkins Plug in download acceleration
  1. Jenkins It doesn't provide much functionality in itself , We can use plug-ins to meet our needs . For example, from Gitlab Pull the code , Use Maven Build projects and other functions need to rely on plug-ins to complete . Next, show me how to download the plug-in .
  2. modify Jenkins Plug in download address ,Jenkins The download speed of foreign official plug-in address is very slow , So it can be changed to the domestic plug-in address .

enkins->Manage Jenkins->Manage Plugins, Click the optional plug-in and wait for it to load . This is to put Jenkins Download the official list of plug-ins to local , Then modify the address file and replace it with the domestic plug-in address .

cd /var/lib/jenkins/updates
sed -i 's#http:\/\/\/download#https:\/\/\/jenkins#g' default.json
sed -i '#/http:\/\/\/\/' default.json

Last ,Manage Plugins Click on Advanced, hold Update Site Change to the domestic plug-in download address

Submit after , Type in the browser : , restart Jenkins.

4.5 Jenkins User authority management

We can use Role-based Authorization Strategy Plug in to manage Jenkins User permissions

  1. Role-based Authorization Strategy plug-in unit
  1. Enable global security configuration of permissions

Manage Jenkins -> Configure Global Security

  1. Create the role

Manage Jenkins -> Manage and Assign Roles -> Manage Roles

Global roles( Global role ): Advanced users such as administrators can create global based roles

Project roles( Project role ): Roles for one or more projects

Node roles( Node role ): Node related permissions

  1. Let's add the following three roles
  • baseRole: The role is global . This role needs to be bound Overall Below Read jurisdiction , It's for all users to bind the most basic Jenkins Access right . Be careful : If you don't bind this role to subsequent users, an error will be reported :" user name ismissing the Overall/Read permission"
  • role1: This role is the project role . Use regular expression binding "jp.*", It means you can only operate jp The opening project .

  • role2: This role is also a project role . binding "web.*", It means you can only operate web The opening project .

  1. Create user

Manage Jenkins -> Manage Users -> Create two new users, such as xixi / haha

  1. Assign roles to users

Manage Jenkins -> Manage and Assign Roles -> Assign Roles

The binding rules are as follows :

xixi User binding baseRole and role1 role ,haha binding baseRole and role2 role

Create project test permissions !

4.6 Jenkins Voucher management
  1. Credentials can be used to store database passwords that need ciphertext protection 、Gitlab Password information 、Docker Private warehouse password, etc , In order to Jenkins It can interact with these third-party applications .
  2. install Credentials Binding plug-in unit , To be in Jenkins Use the voucher management function , After installation Manage Jenkins The following menu appears
  1. Five types of credentials
# Username with password: User name and password
# SSH Username with private key: Use SSH User and key
# Secret file: Text files that need to be kept secret , When using Jenkins The file will be copied to a temporary directory , Set a variable to the file , When the build is finished , The copy of Secret file It will be deleted .
# Secret text: Need to save an encrypted text string , Like a nailing robot or Github Of api token
# Certificate: By uploading the certificate file
  1. Jenkins management Gitlab voucher - User password type
  • install Git plug-in unit . In order to make Jenkins Support from the Gitlab Pull source code , Need to install Git plug-in unit
  • install Git Tools , In order to make Jenkins Support from the Gitlab Pull source code , Need to be in CentOS7 Installation on Git Tools
# install
yum install git -y
# Check the version after installation
git --version
  • Create user password type credentials

Manage Jenkins -> Manage Credentials -> Add the credentials

  • Test the availability of credentials

Create a free style project , find " Source code management "->"Git", stay Repository URL Copy Gitlab Projects in China URL.

image-20210110032659717 image-20210110032908956 image-20210110032927972

see /var/lib/jenkins/workspace/ Catalog , The discovery has come from Gitlab Successfully pulled the code to Jenkins in .

  1. Jenkins management Gitlab voucher - SSH type
# stay Jenkins Where CentOS System use root User generated public key and private key
ssh-keygen -t rsa
# stay /root/.ssh/ The directory holds the public and private keys
  • Put the generated public key in Gitlab in .

With root The account login -> Click on the picture ->Settings->SSH Keys. Copy just now Here's the content of the document , Click on "Add Key"

  • stay Jenkins Add credentials to , Configure private key

stay Jenkins Add a new credential , The type is "SSH Username with private key", Copy the contents of the private file generated just now

4.7 Maven install and configure

stay Jenkins Integration server , We need to install Maven To compile and package projects .

  1. install Maven

  2. Configure environment variables

vim /etc/profile
export MAVEN_HOME=/home/apache-maven-3.5.4
export PATH=${PATH}:${MAVEN_HOME}/bin
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
# Enable environment variables
source /etc/profile
# see Maven edition
mvn -v
  1. Global tool configuration Association JDK and Maven

Jenkins -> Global Tool Confifiguration -> JDK-> newly added JDK


Jenkins->Global Tool Confifiguration->Maven-> newly added Maven

  1. add to Jenkins Global variables

Manage Jenkins->Confifigure System->Global Properties , Add three global variables

  1. modify Maven Of settings.xml
# Create a local warehouse Directory
mkdir /root/repo
# Modify local warehouse Path
vim /opt/maven/conf/settings.xml
# Add alicloud private server address
alimaven aliyun maven central
  1. test Maven Is the configuration successful?
  • Use the previous test project , Modify the configuration
  • structure -> Add build steps -> Execute Shell
  • To build again , If you can type the project into war package , representative maven Environment configuration successful !
4.8 Tomcat Installation and configuration

What we use here is window The server . Configuration and Linux There's no difference in servers

  1. install Tomcat8.5
  2. To configure Tomcat User role permissions

By default Tomcat There are no user role permissions configured . however , follow-up Jenkins Deploy the project to Tomcat The server , Need to use Tomcat Users of , So modify tomcat Following configuration , Add users and permissions .

<!-- Get into tomcat Of conf/tomcat-users.xml Add the following configuration -->
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
<!-- In order to be able to log in to Tomcat, You also need to modify the following configuration -->
<!-- Get into webapps/manager/META-INF/context.xml, Comment out the following code -->
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
  1. restart Tomcat, Access test
  1. Tomcat tuning , adjustment Jvm Memory size ( Solve the problem of back-end project deployment failure bug)

Due to the large size of the company's projects , I use it many times Jenkins Auto deploy projects all failed , But there is no obvious error in the code . Later it was found that tomcat Of jvm The setting is too small to overflow , It can be adjusted properly jvm Big memory , Just restart it .

  • Use the character you just created to enter manager application , Click on Server Status see JVM state
  • modify JVM Memory size

Modify the way : open Tomcat Of bin/catalina.bat File modification

Be careful : Memory should not be set too large , According to the server memory size appropriate configuration

Increase heap memory

Increase non heap memory

5. Jenkins Various types of project construction

5.1 Automatically build project types

Jenkins There are many types of Autobuild projects in , There are three commonly used :

  • Free style software project (FreeStyle Project)
  • Maven project (Maven Project)
  • Assembly line project (Pipeline Project)

Each type of build can actually complete the same build process and result , It's just the way it works 、 Flexibility and so on , In actual development, you can choose according to your own needs and habits .(PS: I recommend the pipeline type , Because the flexibility is very high )

5.2 Free style project building

Demonstrate the integration process : Pull the code -> compile -> pack -> Deploy

  1. Create free style projects web_test2
  2. Configure source management , from GitLab Pull the code
  1. Compile the package
  1. Deploy

Deploy projects to remote Tomcat Inside

  • install Deploy to container plug-in unit .Jenkins It cannot be remotely deployed to Tomcat The function of , Need to install Deploy to container Plug-in implementation
  • add to Tomcat User credentials
  • Add post build actions
  • Click on Build Now, Start the build process
image-20210110094128822 image-20210110094204122
  • Then change the code and push To GitLab, stay Jenkins The project can be rebuilt and deployed in
5.3 Maven Project structures,
  1. install Maven Integration plug-in unit
  1. establish Maven project web_test2_maven
  1. Configuration items

The process of pulling code and remote deployment is the same as that of free style projects , It's just " structure " Part of it's different

5.4 Assembly line project construction
  1. Pipeline brief introduction

Pipeline, Simply put, it's a set of programs that run on Jenkins Workflow framework on , Connect tasks that previously ran independently on a single or multiple nodes , To realize the complex process arrangement and visualization work that single task is difficult to complete .

Use Pipeline There are the following benefits :

  • Code :Pipeline In the form of code , Usually checked into source code control , Enables the team to edit , Review and iterate its delivery process .

  • persistent : Whether it's a planned or unplanned server restart ,Pipeline It's all recoverable .

  • Can stop :Pipeline Can receive interactive input , To determine whether to proceed with Pipeline.

  • multi-function :Pipeline Support complex continuous delivery requirements in the real world . It supports fork/join、 Loop execution , The ability to perform tasks in parallel .

  • Scalable :Pipeline The plug-in supports DSL Custom extension for , And multiple options for integration with other plug-ins .

How to create Jenkins Pipeline?

  • Pipeline The script was created by Groovy The realization of language , But we don't have to study alone Groovy

  • Pipeline Two kinds of grammar are supported :Declarative( declarative ) and Scripted Pipeline( Scripted ) grammar

  • Pipeline There are also two ways to create : Can be directly in Jenkins Of Web UI Input script in the interface ; You can also create a Jenkinsfifile The script file is put into the source library of the project ( It's generally recommended that Jenkins Directly from source control (SCM) Directly loaded in Jenkinsfifile Pipeline This method )

  1. install Pipeline plug-in unit
  1. After installing the plug-in , There's more time to create projects “ Assembly line ” type
  1. Pipeline A quick introduction to grammar
  • Declarative declarative -Pipeline

Assembly line -> choice HelloWorld Templates


The generated content is as follows

stages: Represents all execution phases of the entire pipeline . Usually stages Only 1 individual , It contains more than one stage

stage: Represents a stage in the pipeline , May appear n individual . It is generally divided into pull code , Compiling and constructing , Deployment and so on .

steps: Represents the logic that needs to be executed in a phase .steps Inside is shell Script ,git Pull the code ,ssh Any content such as remote Publishing .

pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'

Write a simple declarative Pipeline

pipeline {
agent any
stages {
stage(' Pull the code ') {
steps {
echo ' Pull the code '
stage(' Compiling and constructing ') {
steps {
echo ' Compiling and constructing '
stage(' Project deployment ') {
steps {
echo ' Project deployment '

Click on the building , You can see the whole build process

  • Scripted Pipeline Scripted -Pipeline

Assembly line -> choice Scripted Pipeline


The generated content is as follows

Node: node , One Node It's just one. Jenkins node ,Master perhaps Agent, Is to perform Step The specific operating environment , I will talk about Jenkins Of Master-Slave It's used in architecture .

Stage: Stage , One Pipeline It can be divided into several Stage, Every Stage Represents a group of operations , such as :Build、Test、Deploy,Stage It's a concept of logical grouping .

Step: step ,Step Is the most basic unit of operation , It could be printing a sentence , You can also build a Docker Mirror image , By all kinds of Jenkins Plug in provides , For example, command :sh ‘make’, It's equivalent to what we usually do shell Execution in terminal make command


node {
def mvnHome
stage('Preparation') {
stage('Build') {
stage('Results') {
  • We can generate it quickly through the lower left pipeline syntax pipeline Code

Take the pull code as an example , Select the fragment generator template

image-20210110101327060 image-20210110101550186
pipeline {
agent any
stages {
stage(' Pull the code ') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '0c93f73d-1e42-4d39-a8ab-f07d5dda668f',
url: 'ssh://git@']]])
stage(' Compile the package ') {
steps {
sh label: '', script: 'mvn clean package'
stage(' Project deployment ') {
steps {
deploy adapters: [tomcat8(credentialsId: '22b62c37-4009-4ac3-b82b-fbf926a0cc63', path: '',
url: '')], contextPath: null, war: 'target/*.war'
  • Pipeline Script from SCM

Just now we were all directly in Jenkins Of UI Interface writing Pipeline Code , This is not convenient for script maintenance , Make a proposal to Pipeline The script is in the project ( Version control together ), Created in the project root directory Jenkinsfifile file , Copy the script content to the file


6. Jenkins Build trigger

6.1 Jenkins built-in 4 Build triggers
  • Trigger remote build

  • Trigger after other projects are built (Build after other projects are build)

  • Time to build (Build periodically)

  • polling SCM(Poll SCM)

6.2 Trigger remote build Demo

Trigger a build url:

6.3 Trigger after other projects are built
6.4 Time to build
  • The timing strings from left to right are : branch when Japan month Zhou
  • Some examples of timed expressions :

Every time 30 Build once in minutes :H On behalf of the parameter H/30 * * * * 10:02 10:32

Every time 2 Build once an hour : H H/2 * * *

Daily 8 spot , 12 spot , 22 spot , Build... In a day 3 Time : ( Multiple time points are separated by commas ) 0 8,12,22 * * *

Every day at noon 12 Build once on a regular basis H 12 * * *

Every afternoon 18 Build once on a regular basis H 18 * * *

Every... In the first half of every hour 10 minute H(0-29)/10 * * * *

Every two hours , Every weekday in the morning 9 Point to the afternoon 5 spot ( Maybe in the morning 10:38, Afternoon 12:38, Afternoon 2:38, Afternoon 4:38) H H(9-16)/2 * * 1-5

6.5 polling SCM

polling SCM, When specified, scan the local code warehouse for code changes , If the code changes, the project build is triggered .

Be careful : This time build triggers ,Jenkins The code of the whole local project will be scanned regularly , Increase the cost of the system , Not recommended .

6.6 Git hook Automatically trigger build plug-ins
  1. Just now we saw in Jenkins In the built-in build trigger of , polling SCM Can achieve Gitlab Code update , Project auto build , But the performance of the scheme is not good . Is there a better plan ? yes , we have . Is the use Gitlab Of webhook Implementation code push To the warehouse , Trigger project auto build immediately .
  1. install GitLab Hook plug-in unit :Gitlab Hook And GitLab Two plug-ins
  1. Jenkins Set up automatic build
  1. GitLab To configure webhook
  • Turn on webhook function

Use root Account login to the background , Click on Admin Area -> Settings

Check " Allow hooks and services to access the local network "

  • Add... To the project webhook
  • Be careful : The following settings must be completed , Otherwise, an error will be reported !

7. Jenkins Parametric construction

7.1 understand

Sometimes in the process of building a project , We need to input some parameters dynamically according to the user's input , This will affect the results of the entire build , In this case, we can use parametric construction .

Jenkins Support very rich parameter types .

7.2 Demonstrate deploying different branch projects by branch name

Pipeline project as a demonstration

  1. Project creation Branch , And push it to Gitlab On
  1. stay Jenkins Add string type parameter to project configuration
image-20210110110624415 image-20210110110750738
  1. modify pipeline Pipelining pull step code
  1. Click on Build with Parameters

Enter the branch name to build ! Visit... After the build is complete Tomcat View results


8. Jenkins Configure the mailbox server to send the build results

8.1 install Email Extension Plugin plug-in unit

Manage Jenkins -> Confifigure System

  1. Set up administrator mailbox
  1. Set mail parameters
  1. Set up Jenkins Default mailbox information
  1. Prepare the email content ( Templates )

Write... In the project root directory email.html, And push the files to Gitlab, The contents are as follows :

<!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">
<td>( This email is Jenkins The program is automatically distributed , Please do not reply !)</td>
<td><h2><font color="#0000FF"> The build results - ${BUILD_STATUS}</font></h2></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 number : The first ${BUILD_NUMBER} Time to build </li>
<li>SVN edition : ${SVN_REVISION}</li>
<li> trigger :${CAUSE}</li>
<li> The build log :<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
<li> Build address :<a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li> working directory :<a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
<li> Project address :<a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
<li> Changeset :${JELLY_SCRIPT,template="html"}</li>
<td><b><font color="#0B610B">Changes Since Last Successful Build:</font></b><hr size="2" width="100%" align="center" /></td>
<li> Historical change record : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
</ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="<pre>[%a]<br />%m</pre>",pathFormat="%p"}
<td><b><font color="#0B610B">Failed Test Results:</font></b><hr size="2" width="100%" align="center" /></td>
<td><pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">${FAILED_TESTS}</pre>
<br /></td>
<td><b><font color="#0B610B"> The build log ( Last 100 That's ok ):</font></b>
<hr size="2" width="100%" align="center" /></td>
<td>Test Logs (if test has ran): <a
<br />
<br />
<td><textarea cols="80" rows="30" readonly="readonly"
style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
  1. Write script, add build, send mail

Pipeline syntax interface selection post Templates


Pipeline syntax interface select mail template

pipeline {
agent any
stages {
stage(' Pull the code ') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '0c93f73d-1e42-4d39-a8ab-f07d5dda668f',
url: 'ssh://git@']]])
stage(' Compile the package ') {
steps {
sh label: '', script: 'mvn clean package'
stage(' Project deployment ') {
steps {
deploy adapters: [tomcat8(credentialsId: '22b62c37-4009-4ac3-b82b-fbf926a0cc63', path: '',
url: '')], contextPath: null, war: 'target/*.war'
post {
always {
subject: ' Build notifications :${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}',
to: ''
  1. Email results show
  1. Email related global parameter reference list :

Configure System -> Extended E-mail Notifification -> Content Token Reference, Click next to ? You can refer to


9. SonarQube Code review tool

SonarQube Is an open platform for managing code quality , It can quickly locate potential or obvious errors in the code . At present, we support java,C#,C/C++,Python,PL/SQL,Cobol,JavaScrip,Groovy Code quality management and testing of more than 20 programming languages .

Bottom use Elasticsearch As a code retrieval tool .

9.1 Jenkins Integrate SonarQube Icon
9.2 Installation environment (CentOS7)
Software The server edition
MySQL 5.7(8+ The version will report an error )
SonarQube 6.7.4
SonarQube Sinicization jar package 1.19
Sonar-Scanner client 4.2.0
9.3 Installation steps
  1. stay MySQL Create in the database sonar database

  2. install SonarQube And set permissions

yum install unzip
unzip # decompression
mkdir /opt/sonar # Create directory
mv sonarqube-6.7.4/* /opt/sonar # Moving files
user add sonar # establish sonar user , must sonar Used to start , Otherwise, the report will be wrong
chown -R sonar. /opt/sonar # change sonar Directory and file permissions
  1. modify SonarQube The configuration file
vim /opt/sonar/conf/
# The contents are as follows :

Be careful :SonarQube Default listening 9000 port , If 9000 The port is occupied and needs to be changed .

  1. start-up sonar
cd /opt/sonar
su sonar ./bin/linux-x86-64/ start # start-up
su sonar ./bin/linux-x86-64/ status # Check the status
su sonar ./bin/linux-x86-64/ stop # stop it
tail -f logs/sonar.log # Check the log
  1. visit sonar Default account :admin/admin

  1. establish token, be used for jenkins Yes sonarqube Certificate management of
# I have generated the voucher here - SonarQube token
root: f0e71f7e7f194dfe4912d20ed5fa0d481b20e7cf
  1. Sinicization SonarQube

Make it Chinese jar Put in bags SonarQube The installation directory extensions/plugins Next , restart SonarQube)

  1. Close and upload the review results to SCM The function of
  1. Jenkins install SonarQube plug-in unit
  1. Jenkins add to SonarQube voucher
  1. Jenkins install SonarQube Scanner client

Manage Jenkins->Global Tool Confifiguration

  1. Jenkins Conduct SonarQube To configure

Manage Jenkins -> Confifigure System -> SonarQube servers

9.4 Jenkins Project code review
  1. Add build steps for non pipeline projects
  1. Add build steps to pipeline project
stage('SonarQube Code review ') {
script {
# introduce SonarQubeScanner Tools
scannerHome = tool 'sonarqube-scanner'
# introduce SonarQube Server environment
withSonarQubeEnv('sonarqube6.7.4') {
sh "${scannerHome}/bin/sonar-scanner"
  1. Profile templates
# The unique mark of the project ( Here I'll write the project name directly )
# Project name
# Project version No
# Specify the path to scan the code ,. Scan all... In the root directory of the current project
sonar.sources=stidem/src/main # Scan the specified directory
# crater ! new edition sonar The plug-in needs to specify**/target/classes
# Exclude those that don't need to be scanned
# Jdk edition
# Source code format
  1. common Bug
  1. Code review results
image-20210110000116888 image-20210110000252883

10 Jenkins Deploy Vue Project static resources to Nginx

10.1 Icon
10.2 Installation and configuration Nginx The server
  1. install Nginx The server

  2. With Jeeplus Vue Take the project as an example nginx.conf The configuration file

worker_processes 1;
events {
worker_connections 1024;
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_comp_level 9;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
server {
listen 80;
server_name Production server domain name or ip;
location ^~ /jeeplus-customer {
proxy_set_header Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ^~ /userfiles {
proxy_set_header Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
root html/jp-ui/dist;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
  1. Windows Next Nginx Relevant command
cmd Get into Nginx Unpack the directory Execute the following command
# start-up nginx service
start nginx
# Reload takes effect after modifying the configuration
nginx -s reload
# To stop or close quickly Nginx
nginx -s stop
# To stop or close normally Nginx
nginx -s quit
# Reopen the log file
nginx -s reopen
# test nginx Is the configuration file correct
nginx -t -c /path/to/nginx.conf
# Verify that the configuration is correct :
nginx -t
# see Nginx Version number of
nginx -V
10.3 Jenkins install NodeJS plug-in unit
10.4 Jenkins To configure Nginx The server
  1. install Node.js Environmental Science

Manage Jenkins -> Global Tool Configuration

  1. Jenkins install SSH plug-in unit
  1. Jenkins To configure SSH remote server

Manage Jenkins ->Configure System

  1. Create front end free style projects jp-ui And configuration

Other steps are consistent with the previous project configuration , Here's just a list of different

image-20210110130812722 image-20210110130843824 image-20210110131014463
  1. Window Production server installation SSH Tools
  • I can't get Linux root In the case of accounts winscp And other tools can not achieve file transfer , Now we can borrow Bitvise SSH;

  • Download address : Bitvise SSH Server installer - version 7.32, size 14.0 MB. We need to server Version of the software, the tool can achieve two-way transmission , namely linux To Windows and Linux obtain Windows End file .

  • Double click Install after download -> Check personal version -> Enter personal information , The information here is not taken as scp Account . I'll talk about it later scp Account

  • Click after entering “open easy setting”

    • server setting, Check open firewall , Of course, for security, you can set up your own LAN only
    • virtual account Set up , Here is the scp Account information , Very important . Click Add
    • Set account information
    • Click on virtual account password, Set account password
  • Click on save changes go back to server Interface . Click on start server, You can start ssh service . If there is anti-virus software block, please block all

  • Let's test it , Be careful window Catalog writing

    • from Linux obtain Windows file
    • from Linux Send the file to Windows Catalog
  1. Jenkins Click build to start execution , Check for success . And access Nginx To test

11. Jenkins + Docker + SpringCloud Continuous integration of microservices

To be continued ...

12. be based on Kubernetes/K8S Container choreography tools build Jenkins Continuous integration

To be continued ...
Copyright statement
In this paper,the author:[MPolaris],Reprint please bring the original link, thank you