Learn from zero Jenkins deployment SpringBoot project


Although Jenkins has been used in previous projects, they are only used. They have not built their own projects and fully deployed a project. Since there is no independent deployment in the work, you can only do it yourself, and you don't have too many skills.

At first, Jenkins is downloaded and installed on the local machine, and then the local project is uploaded to Gitlab. It is expected to build the project on Jenkins, and then deploy and publish the project. However, in the process of starting work, it was found that Jenkins on this machine could not create a new task. He either built a virtual machine or set up a server. He just had a server to play with and just fit the actual production, so there was the following.



Prepare java environment, jdk1.8

Check whether java is installed. If not, first install the java environment: java -version

View jdk installation path: echo $JAVA_HOME

[root@iZ8vbf6lodiycfjz81qhkyZ home]# echo $JAVA_HOME

Configure environment variables

export JAVA_HOME
export PATH

Then the configuration file becomes effective,

be careful

If you do not configure the environment variable, this will be the result

[root@iZ8vbf6lodiycfjz81qhkyZ alternatives]# java -version
openjdk version "1.8.0_302"
OpenJDK Runtime Environment (build 1.8.0_302-b08)
OpenJDK 64-Bit Server VM (build 25.302-b08, mixed mode)


Install yum install git using the yum command.

git --version
#View installation location
which git


yum -y install maven 
mvn -v  # Note: mvn is not maven. If it is used incorrectly, it is assumed that maven environment variable is not configured. Wrong usage: maven -v

Apache Maven 3.0.5 (Red Hat 3.0.5-17)
Maven home: /usr/share/maven
Java version: 1.8.0_302, vendor: Red Hat, Inc.
Java home: /usr/lib/jvm/java-1.8.0-openjdk-
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.11.1.el7.x86_64", arch: "amd64", family: "unix"

Modify the configuration file with the following command:

vi /usr/share/maven/conf/settings.xml

#Then add the alicloud image address
  <name>aliyun maven</name>

#Set up local warehouse

Configure environment variables,

vi ~/.bash_profile

#Add the following contents
export M3_HOME=/usr/share/maven
export PATH=$M3_HOME/bin:$PATH


The latest version of Gradle used in this article is 7.2. You should check before proceeding to the next step Gradle version page To see if a newer version is available.

First use the following wget The command downloads only the Gradle Binary zip file in the / tmp Directory:

wget https://services.gradle.org/distributions/gradle-7.2-bin.zip -P /tmp

After downloading, unzip the zip file in the / opt/gradle Directory:

sudo unzip -d /home/gradle /tmp/gradle-7.2-bin.zip

Verify that the Gradle file was extracted by listing the / opt/gradle/gradle-7.2 Directory:

ls /home/gradle/gradle-7.2
bin  init.d  lib  LICENSE  NOTICE  README

Configure environment variables,

vi ~/.bash_profile

#Add the following contents

View version number

gradle -v

Install jenkins

(1) Install stable version

sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins

(2) Install the latest version. The latest version is installed by default

wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo 
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install -y jenkins

After installation, if you want to check the jenkins version, go to the / var/lib/jenkins directory and execute the following command according to the Internet:

cat config.xml|grep version

But there is something wrong with the output of my own server.

For automatic installation, the default user name is jenkins, the port is 8080, and the default directory is:

  • Installation directory: / var/lib/jenkins
  • Log directory: / var/log/jenkins/jenkins.log
  • Default configuration directory: / etc/sysconfig/jenkins

You can change jenkins user to root user, modify port, etc

vim /etc/sysconfig/jenkins
service jenkins restart

Uninstall jenkins

Uninstall jenkins installed in yum mode (the default installation home directory is / var/lib/jenkins /)

service jenkins stop
yum clean all
yum -y remove jenkins


Activate jenkins

First start jenkins and execute the following command:

service jenkins start

Then visit http::/ip:8080,

You need to enter the password for the first login. See the prompt on the page:


Finally, log in successfully.

The browser enters Jenkins

(1) Login: http: / / own IP:8081/

(2) Close: http: / / own IP:8081/exit

(3) Restart: http: / / own IP:8081/restart

(4) Reload: http: / / own IP:8081/reload

Startup and shutdown

service jenkins start
service jenkins stop

#If you encounter warning
systemctl start jenkins
systemctl stop jenkins

Plug in installation

Authorize Project

Configure authorization for the project.

Gitlab Hook Plugin

Trigger automatic build when Github has code submitted

SSH Slaves plugin

Update - April 21, 2020:

1.26 it is also called SSH slave

1.31.2 has been updated to SSH build Agents


When a new commit is pushed to Gitlab, jenkins can automatically trigger the build process. In addition to Gitlab hook plugin, Gitlab plugin is also required.

Deploy to container

Associated with Gitlab

Configure SSH

If we want to pull code through git protocol, we need to establish password free communication between Jenkins server and Gitlab server. First, we need to survive a set of key pairs.

[root@iZ8vbf6lodiycfjz81qhkyZ tmp]# SSH keygen - t RSA - C "your mailbox (optional)" Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:D998pKifmjJgSmUcNNlaEDAdhB/UU4wr+QkPVMkd1KU xxxxxThe key's randomart image is:+---[RSA 2048]----+|  oBOX.B+o ..    ||  ..*.O o ..     ||   + * o  E      ||    @ .          ||   o * .S     .  ||  . o +  + + o   || . o .    + + .  ||  .   o  o . .   ||       o+oo      |+----[SHA256]-----+

After completion, two files will be generated in / root/.ssh /. id_rsa and id_rsa.pub. The former is the private key. Please keep it. The latter is the public key.

Add SSH Key to GitLab

After logging into GitLab: profile settings = > SSH keys = > Add SSH key

Enter the previously generated public key and customize the title.

Recommended reading: How Jenkins of continuous integration passes the authority authentication on Gitlab (3)

Gitlab token

For the steps of configuring gitlab access token in jenkins, it is recommended to read: How to get GitLab Token?

Jenkins installation and Gitlab project deployment details

GitLab+Jenkins continuous integration

New task

SSH needs to be configured before creating a new task.

1. New task


Where Gitlab Connection is configured when obtaining Gitlab token. JDK can select the path on the server, which we have configured in the global tool configuration before.

3. Source code management

At this time, click the Add button, there will be a jenkins option, and then enter the page:

The type options can be Username with password or SSH. If the former is selected, enter the account and password of gitlab. If the latter is selected, configure the private key in the private key, that is, the ID when configuring SSH_ The contents of the RSA file.

After successful configuration

If your account number, password and warehouse address are correct, the error prompt disappears.

4. Build trigger

We check "build whoever a snapshot dependency is build". At the same time, you need to configure gitlab's webhook and configure the address marked in the above red box into gitlab.

You can click the test button to try and select pubsh events.

5. Build environment


Build is the operation to be done after the code is pulled down. As it is a maven project, we need to specify the command of compiling and packaging. Here is:

clean package -Dmaven.test.skip=true

7,Post Steps

This step is the most critical. The success depends on the configuration here. First, give the corresponding shell script:


echo 'start maven Build service'

#export BUILD_ The sentence id = dontkillme is very important, which specifies that Jenkins will not kill the project after it is started.
export BUILD_ID=dontKillMe

mvn clean install

#Specify the location where the last compiled jar is stored

#Compiled jar location in Jenkins

cd  ${jar_path}

#Get the jar name compiled in Jenkins, where XXX is the artifactId value in your pom file. This step is mainly to dynamically obtain the project file name according to the project version number
#jar_name=`ls |grep XXX-|grep -v original`
jar_name=`ls |grep mybatis-generator-|grep -v original`

#It should be noted that there is no corresponding pid during the initial build, so you need to judge whether the file exists
#Get the ID of the running compiled process, so that we can kill the previous process when redeploying the project
if [ -f "/home/wwwroot/jenkins/Mybatis-Generate.pid" ];then
  pid=$(cat /home/wwwroot/jenkins/Mybatis-Generate.pid)
  #Kill the project process that may have been started before
  kill -9 ${pid}

#Enter the last specified location for storing jar s
cd  ${www_path}

#Delete the original jar file first
rm -rf ./${jar_name}

#Enter the specified location of the compiled jar
cd  ${jar_path}

#Copy the compiled jar to the last specified location
cp  -r ${jar_path}/${jar_name} ${www_path}

#Enter the last specified location for storing jar s
cd  ${www_path}

#Start the jar, specify the SpringBoot profiles as beta, and start in the background
#java -jar -Dspring.profiles.active=beta ${jar_name} &
java -jar ${jar_name} &

#Store the process ID in the ufind-web.pid file
echo $! > /home/wwwroot/jenkins/Mybatis-Generate.pid

How should we configure the above script?


First, find the installation address of jenkins in the server. As mentioned above, it is located in / var/lib/jenkins.

There is a workspace folder in this directory, where mybatis generate is the name of our task, which finally constitutes our jar_path, mvn after executing the command, the corresponding jar package will be generated and stored in the target directory.


We will copy a copy of the jar package to another folder later, so we will create this folder according to our habits.

8. Build settings, email configuration and additional functions do not affect the construction of the project

First, we need to download two plug-ins: email extension and email extension template. These two plug-ins can help us write, send and format emails.

There are many specific operation steps on the Internet. It is recommended to read here: 03 Jenkins Email configuration and task Email sending practice and Practice

Netease 163 free email server information:


Using Jenkins to automate the deployment of SpringBoot projects

Configuring the automated build of SpringBoot using Jenkins

Problem record

Solve the problem of too high memory consumption of jenkins

  • Modify the configuration file vim /etc/sysconfig/jenkins
  • The contents are as follows:
# JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"
JENKINS_JAVA_OPTIONS="-XX:MaxPermSize=512m -Djava.awt.headless=true"
  • Restart jenkins

Java 11 is the recommended version to run Jenkins on;

There are two environments, linux and jenkins installed through brew in Mac OS.

1, linux

vi /etc/init.d/jenkins

Amend the following:


Add jdk installed locally


If this method does not work, it is recommended to read: https://stackoverflow.com/questions/55243120/jenkins-add-jdk-11-to-jdk-list

2, macOS

Try to install the gitlab hook plug-in in jenkins, and it will raise an error when installing ruby - runtime

java.lang.RuntimeException: unsupported Java version: 11 at org.jruby.RubyInstanceConfig.initGlobalJavaVersion(RubyInstanceConfig.java:1674) at org.jruby.RubyInstanceConfig.<clinit>(RubyInstanceConfig.java:1387) Caused: java.lang.ExceptionInInitializerError at org.jruby.embed.internal.AbstractLocalContextProvider.<init>(AbstractLocalContextProvider.java:42)

1. Open the file vi /usr/local/opt/jenkins/homebrew.mxcl.jenkins.plist

2. Modify java configuration




jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units

[root@izm5e1qim1yoreb6toj6d9z local]# service jenkins start
Starting jenkins (via systemctl):  Warning: jenkins.service changed on disk. Run 'systemctl daemon-reload' to reload units.
                                                           [  OK  ]
[root@izm5e1qim1yoreb6toj6d9z local]# systemctl daemon-reload
[root@izm5e1qim1yoreb6toj6d9z local]# systemctl restart jenkins

What if there is no Maven project when creating a project in Jenkins

Download Plug-in: Maven Integration

jenkins Failed to create parent directories for tracking file

When jenkins is used to build, the following error is prompted. The reason is that the permissions of maven warehouse are not enough. The solution is to increase the folder permissions:

chmod 777 maven warehouse Path, such as:

chmod 777 /home/maven/repository

jenkins Failed to create /home/jenkins/.m2

chmod 777 /home/jenkins


Looking back on the above, it seems very simple. You just install the necessary tools and configure Jenkins, but the actual operation takes a lot of time. Sometimes it's just a little problem, but you don't understand. You often need to read a lot of articles, compare and combine them to find the correct answer.

Maybe find an experienced person to guide and finish it soon, but not everyone happens to have such a big man around.

Still that sentence, a good memory is not as good as a bad pen. If you do it again, write it down and step on the pit, you will leave a deep impression.

Tags: git GitLab jenkins Maven Spring Boot

Posted on Tue, 28 Sep 2021 15:55:07 -0400 by jamest