Continuous original output , Click on the blue word above to follow me

Catalog
-
Preface -
How to install Jenkins? -
Environmental preparation -
Start installation Jenkins
-
-
Initialize configuration -
Visit the home page -
Enter the administrator password -
Installing a plug-in -
Create administrator -
The instance configuration -
Configuration complete
-
-
structure Spring Boot project -
To configure JDK、maven、Git Environmental Science -
Installing a plug-in -
add to SSH Server -
Add the credentials -
newly build Maven project -
Build tasks
-
-
How to build hosting in GitLab Project ? -
Installing a plug-in -
add to GitLab API token -
To configure GitLab plug-in unit -
A new task -
Follow up operation
-
-
How to build a multi module project ? -
summary
Preface
I don't know if big guy has heard of it , Have you ever used a similar tool ?
In short , The main job of automatic continuous integration is to be able to build projects 、 Automated testing and release .
Today's article will talk about common tools for continuous integration Jenkins
And how to automatically build Spring Boot
project .
How to install Jenkins?
Jenkins
yes Java A set of tools developed , You can download it directly war
Package is deployed in Tomcat
On , But today the author is most convenient to use 、 One of the most popular Docker
install .
Environmental preparation
Before you start the installation, you need to prepare the following environment and tools :
-
One server , Of course, if you don't, you can use your own computer , The author's server model is Ubuntu
. -
JDK
Environmental installation , The author's version is1.8
, As for how to install , There are many tutorials online . -
Get ready maven
Environmental Science , Download an installation package from the official website , Put it in the specified directory . -
Git
Environmental installation , There are many online tutorials . -
Code hosting platform , such as Github
、GitLab
etc. .
Start installation Jenkins
Docker
install Jenkins
Very convenient , Just follow the author's steps step by step , Must be able to install successfully .
Docker Environmental installation
Each server model is installed in a different way , Readers can install according to their own model , There are many online tutorials .
Pull the mirror image
The version I installed here is jenkins/jenkins:2.222.3-centos
, You can get the version you need here : https://hub.docker.com/_/jenkins?tab=tags
. Execute the following command to install :
docker pull jenkins/jenkins:2.222.3-centos
Create local data volumes
Create a data volume mount locally docker The data volume in the container , I created /data/jenkins_home/
, The order is as follows :
mkdir -p /data/jenkins_home/
You need to modify the directory permissions , Because when mapping local data volumes ,/data/jenkins_home/
The directory is owned by root
user , And in the container jenkins
User uid
by 1000
.
chown -R 1000:1000 /data/jenkins_home/
Create a container
In addition to the need to mount the above created /data/jenkins_home/
outside , You also need to mount maven
、jdk
Root directory . The start command is as follows :
docker run -d --name jenkins -p 8040:8080 -p 50000:50000 -v /data/jenkins_home:/var/jenkins_home -v /usr/local/jdk:/usr/local/jdk -v /usr/local/maven:/usr/local/maven jenkins/jenkins:2.222.3-centos
The above command is parsed as follows :
-
-d
: Background running container -
--name
: Specifies the name of the container to start -
-p
: Specify the port of the mapping , Here is the server's8040
The port maps to the container8080
as well as50000
Mapped to container50000
. 「 Be careful :」8040
and50000
It must be open and unoccupied , If cloud server is used , We also need to open the corresponding rules in the management platform . -
-v
: Mount the local data volume todocker
In the container , 「 Be careful :」 Need to putJDK
andmaven
The directory where is mounted .
Initialize configuration
Container started successfully , You need to configure Jenkins
, Install some plugins 、 Configure remote push and so on .
Visit the home page
Container created successfully , visit http://ip:8040
, If the following page appears, the installation is successful :

Enter the administrator password
Successful launch , You will be asked to enter the password , Here's the picture :

What is required here is the password for management , The hint is in /var/jenkins_home/secrets/initialAdminPassword
, But we have already /var/jenkins_home
This folder is mounted to the local directory , So you just need to mount the directory /data/jenkins_home/secrets/initialAdminPassword
Find... In the file .
Input password , Click to continue .
Installing a plug-in
To initialize the installation, you only need to install some plug-ins recommended by the community , Here's the picture :

Choose here Install the recommended plug-ins
, then Jenkins
The installation will start automatically .
「 Be careful :」 If the plug-in installation is slow , find /data/jenkins_home/updates/default.json
file , The replacement is as follows :
-
take updates.jenkins-ci.org/download
Replace withmirrors.tuna.tsinghua.edu.cn/jenkins
-
take www.google.com
Replace withwww.baidu.com
.
Execute the following two commands :
sed -i 's/www.google.com/www.baidu.com/g' default.json
sed -i 's/updates.jenkins-ci.org\/download/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json

All installed , Move on to the next step .
Create administrator
Create an administrator at will , Fill in the information as required , Here's the picture :

The instance configuration
Configure your own server IP
and port
, Here's the picture :

Configuration complete
Follow the steps above , After configuration, it will automatically jump to the following interface :

structure Spring Boot project
You need to configure some development environment before you build it , such as JDK
,Maven
Such as the environment .
To configure JDK、maven、Git Environmental Science
Jenkins
Integration needs to use maven
、JDK
、Git
Environmental Science , Here's how to configure .
Start by opening System management
-> Global tool configuration
, Here's the picture :

Configure separately JDK
,Git
,Maven
The path of , Fill in according to your actual path .
「 Be careful 」: there JDK
、Git
、Maven
The environment must be mounted to docker
In the container , Otherwise, the following prompt will appear :
xxxx is not a directory on the Jenkins master (but perhaps it exists on some agents)



After successful configuration , Click save .
Installing a plug-in
In addition to the plug-ins installed in the initialization configuration , Also need to install the following plug-ins :
-
Maven Integration
-
Publish Over SSH
open System management
-> Plug-in management
, choice Optional plug-ins
, Check Maven Integration
and Publish Over SSH
, Click on Direct installation
.

Check the installation interface and restart after installation Jenkins
.

add to SSH Server
SSH Server
Is used to connect to the deployment server , It is used to push your application to the server and execute the corresponding script after the project is built .
open System management
-> The system configuration
, find Publish Over SSH
part , choice newly added

Click on senior
Expand configuration

The final configuration is as follows :

After the configuration is completed, you can click Test Configuration
Test connection , appear success
The connection is successful .
Add the credentials
The credentials It's used to get from Git
Warehouse pull code , open The credentials
-> System
-> Global credentials
-> Add the credentials

The configuration here is Github
, Use it directly user name
and password
, Here's the picture :

Create success , Click save .
newly build Maven project
After the above configuration is completed, you can start to build , First you need to create a new Maven
project , Steps are as follows .
Create tasks
Click... On the home page A new task
-> Construct a maven project
, Here's the picture :
Source code management
In source management , choice Git
, Fill in Warehouse address
, Select the previously added voucher
.

Constructing environment
Check Add timestamps to the Console Output
, The log will be printed out during code construction .

Build command
stay Build
in , Fill in Root POM
and Goals and options
, That's your command to build the project .

Post Steps
choice Run only if build succeeds
, add to Post
step , choice Send files or execute commands over SSH
.

The options in the figure above are analyzed as follows :
-
name
: Select the previously addedSSH Server
-
Source files
: Files to push -
Remove prefix
: Prefix to remove from file path , -
Remote directory
: Which directory to push to the target server -
Exec command
: The script to be executed on the target server
Exec command
Specifies the script to be executed , as follows :
# jdk Environmental Science , If global configuration , It can be omitted
export JAVA_HOME=/xx/xx/jdk
export JRE_HOME=/xx/xx/jdk/jre
export CLASSPATH=/xx/xx/jdk/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
# jenkins Compiled jar Bag location , It's mounting docker Under the directory of
JAR_PATH=/data/jenkins_home/workspace/test/target
# Self defined jar Bag location
DIR=/data/test
## jar The name of the package
JARFILE=swagger-demo-0.0.1-SNAPSHOT.jar
if [ ! -d $DIR/backup ];then
mkdir -p $DIR/backup
fi
ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9
if [ -f $DIR/backup/$JARFILE ]; then
rm -f $DIR/backup/$JARFILE
fi
mv $JAR_PATH/$JARFILE $DIR/backup/$JARFILE
java -jar $DIR/backup/$JARFILE > out.log &
if [ $? = 0 ];then
sleep 30
tail -n 50 out.log
fi
cd $DIR/backup/
ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf
The above script roughly means that will kill
The original process , Start a new build jar
package .
Scripts can be customized , Such as backup
Jar
Wait for the operation .
Build tasks
After the completion of the new project , Everything is ready , Click on Build immediately
You can start building tasks , The console can see log
Output , If the build fails , stay log
The reason will be output in .

The task build process executes the script to start the project .
How to build hosting in GitLab Project ?
The example described above is to build Github
Warehouse project , But enterprises are generally private service GitLab
, So how to configure it ?
In fact, the principle is the same , It's just that when you build a task, you choose GitLab
The credentials , More on this below .
Installing a plug-in
stay System management
-> Plug-in management
-> Optional plug-ins
Mid search GitLab Plugin
And install .
add to GitLab API token
Start by opening The credentials
-> System
-> Global credentials
-> Add the credentials
, Here's the picture :

In the picture above API token
How to get it ?
open GitLab
( For example, the company intranet GitLab
Website ), Click... Under the personal settings menu setting
, Click again Account
, Copy Private token
, as follows :

The image above Private token
It is API token
, Just fill it in .
To configure GitLab plug-in unit
open System management
-> The system configuration
->GitLab
, Here's the picture :

After successful configuration , Click on Test Connection
, If you are prompted Success
Then the configuration is successful .
A new task
Create a new one Maven Mission , The configuration steps are the same as above , The only difference is configuration Git
Where the warehouse address is , Here's the picture :

Warehouse address and credentials need to be filled in Gitlab
Corresponding .
Follow up operation
The following operations , Like building projects , Console output and other operations , It's all with GitHub
Same operation , I'm not going to repeat that .
How to build a multi module project ?
If your multiple modules are not dependent on private server repositories , So there is a sequence in building packages , When creating a new task, you need to configure Build
Of maven
command , Here's the picture :
In the picture above Goals and options
The command in is to build api
The command of this module , As for what this order means , There's a separate article earlier on , Please have a look at Thinking about a package , original maven I can still play like this ~.
summary
This article describes in detail how to deploy a from zero installation Jenkins
, Now I can boast again , ha-ha ....
If it feels good , You can't get lost ~
