jenkins enables automatic deployment

Posted by balkar on Wed, 05 Jan 2022 13:55:40 +0100

jenkins enables automatic deployment (CD/CI)

Execution flow chart

I. environment server configuration

1. The following environments need to be installed on the server:
1) Install jdk-1.8
2) Install maven-3.6 one
3) Installing git-2.34
4) Install node-16.13 1 (optional, depending on the deployment method)

2. After installation, configure the environment variables

3. Enter maven's setting XML file changes the store to

4. Change the image address to Alibaba cloud image address. Two images are added as follows to prevent some images from finding packages

		<name>aliyun maven</name>
		<name>aliyun maven</name>

5. By command
mvn install:install-file -Dfile=/usr/local/jars/formal/Algorithm_assessment.jar -DgroupId=com.xjgd -DartifactId=algorithm_sd -Dversion=2.0 -Dpackaging=jar
Add algorithm package to maven warehouse

6. After adding to the maven warehouse, you can see the algorithm package in the reposity / COM / xjgd directory

7. The configuration process may be due to the lack of maven metadata local XML file leads to compilation failure all the time. This file is a coordinate guide, which will guide us where to find the package path. If maven can't find the package without this file, it will find the corresponding package in the cloud image. The customized package can't be found in the cloud image, and an error will be reported if it can't be found.

8. In this case, we copy the package directly from the original normal package. I copy the whole reposity directly under windows to avoid making a difference

II. Installation of plug-ins

Related plug-ins
1 jdk
2 maven
3 git
4 Gedentials Binding (Document related)
5 Gitlab Hook (Build triggers. In order to use real-time build at build time, submit code and build)
6 gitlab
7 SonarQube Scaner(Code check plug-in)
Other plug-ins are installed according to their own needs. The above is the main plug-in

Since jenkins is installed on the server, we don't need to install it. After logging in to jenkins, click mange jenkins to enter the management page - > Click Mannage Plugins, as shown below

Three configuration

1. After installation, go back and click mange Jenkins to enter the management page - > Click Global Tool Configuration to enter global configuration to view the installed plug-ins

2. Then configure the information corresponding to the plug-in

3. Configure credentials / credentials

4. Configure credentials to associate with gitlab

IV. new projects

1. After the preparatory work is configured, create a new project. Here we mainly introduce the freestyle project

2. Set retention time after build

3. Configure gitlab

4. Build trigger configuration

5. Configuration script specific script

mvn clean package
#This directory is the target directory of the jenkins workspace
#jar package in jenkins working directory
#jar package in jenkins working directory
if [ -f $app_file_path ] ;then
echo -- stop app
       pidlist=`ps -ef | grep track-admin.jar | grep -v grep | awk '{print $2}'`
       if [ "$pidlist" = "" ] ;then
              echo "process pid non-existent!"
          echo "process pid :$pidlist"
          kill -9 $pidlist
          echo "KILL $ pidlist:"
# export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-
   echo -- Start $app_path
    nohup java -jar $app_path &
   echo -- Start complete

Similarly, when deploying the front-end project, the script

echo $PATH
node -v
npm -v
#npm install is required for the first installation, but not later
echo 'Start installation npm'
# npm install
echo 'Start packing'
npm run build:prod
echo 'Package succeeded'
#Remove original path
rm -R /opt/track/track-ui/
#Put the newly packaged file in the original path
cp -r /var/lib/jenkins/workspace/freestyleTrack-ui/dist/. /opt/track/track-ui
echo 'to configure track-ui success'

V. configure SonarQube code walkthrough

Official website:
The download version is 6.7 4. If the download version is too high, mysql5.0 will not be supported Version 7 and jdk1 Version 8

1 in mysql5 7 create sonar database in database

2 download sonar compressed package
After downloading, unzip it to the / opt directory

3. To create a sonar user through the command useradd sonar, you must start it with sonar, otherwise an error will be reported
Through the command chown - R sonar/ Opt / sonar change sonar directory and file permissions

4. Modify sonar configuration file
Via the command VIM / opt / sonar / conf / sonar Properties enter sonar Properties file

5. Configure the following places


Note: sonar listens to the 9000 port by default. If the 9000 port is occupied, it needs to be changed.

6. Start sonar. During the startup process, some pits can be solved through Baidu
cd /opt/sonar
su sonar ./bin/linux-x86-64/ start start
su sonar ./bin/linux-x86-64/ status view status
su sonar ./bin/linux-x86-64/ stop stop
tail -f logs/sonar.logs view logs

7. Visit sonar

Default account: admin/admin
Create token
When logging in for the first time, you will be asked to create a token. At this time, the token should be saved, which will be used in jenkins integration later. Since the token value has been set, there is no screenshot effect of setting token here

8. Installing the SonarQube Scanner plug-in in jenkins

9. Add SonarQube credentials
1) In user list - > root - > credentials

Then click "add credentials"

10. Jenkins configures SonarQube, and manage Jenkins - > configure system - > SonarQube servers

11. Configure in manage Jenkins - > global tool configuration

12. SonaQube turns off the function of uploading audit results to SCM

13. Add SonaQube code review to the project (non pipeline project)
Add build steps:

# Project unique ID
# The name and version displayed in the SonarQube UI.
#This configuration is used to load the compiled files under the project. If this version of sonar does not have this configuration, an error will be reported =/var/lib/jenkins/workspace/freestyleImportExport/target/classes
#Scan elements Represents all files in the directory
# Set encoding

In fact, CI/CD continuous integration continuous deployment is to monitor the code Submission on gitlab through a webhook trigger. If new code is submitted, synchronize the code to genkins workspace, and then recompile and package the projects in the workspace and start after script deployment.

Topics: Operation & Maintenance jenkins Maven