(1), automatic deployment: Linux+Jenkins+Maven+Git+SpringBoot

1. Environment preparation

Note: Jenkins can only support jdk8 version 2.346 and below. If a higher version of Jenkins is installed, just change the jdk to 11 or 17.

Need to install maven, git, jdk8

1. maven installation

Download the installation package: Maven – Download Apache Maven

Upload to the server and decompress: tar -zxvf apache-maven-3.9.1-bin.tar.gz

Configure environment variables:

vim /etc/profile

Add configuration to the configuration file

#maven home
# /usr/local/maven/apache-maven-3.9.1: The path after maven decompression
export MAVEN_HOME=/usr/local/maven/apache-maven-3.9.1
export PATH=$PATH:$MAVEN_HOME/bin

reload configuration file

source /etc/profile

Verify: mvn -version

Change the maven configuration file: enter the maven decompressed path

vim conf/settings.xml

Add content to the configuration file

<!--Configure the local warehouse path, add the localRepository tag in settings.xml -->
<localRepository>/usr/local/maven/apache-maven-3.9.1/repository</localRepository>

<!-- #Configure Alibaba Cloud mirroring (acceleration), add the mirror tag under mirrors in the settings.xml file -->
<mirror>
   <id>nexus-aliyun</id>
   <mirrorOf>*</mirrorOf>
   <name>Nexus aliyun</name>
   <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

2. Git installation

Download the installation package: Git – Downloads (git-scm.com)

Unzip the file: tar -zxvf git-2.39.2.tar.gz

Enter the decompression path, install the required dependencies, and enter y when prompted.

yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

Uninstall the old version of git (the old version was installed when installing dependencies):

yum -y remove git

Compile:

make prefix=/usr/local/git all

Install:

make prefix=/usr/local/git install

Configure environment variables:

vim /etc/profile
#git home
export GIT_HOME=/usr/local/git
export PATH=$PATH:$GIT_HOME/bin
source /etc/profile

Verify: git -v

3. jdk8 installation

View available Java versions

yum -y list java* 

Install

yum install -y java-1.8.0-openjdk* 

Path after installation: /usr/lib/jvm

2. Jenkins installation and configuration

1. Download the Jenkins package

Old version address: War Jenkins Packages

New version address: Jenkins download and deployment

2. Start

Enter the path to upload Jenkins.war and run the command

java -jar jenkins.war --httpPort=8080

A development port is required if remote access is required

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload

Access address: http://server Ip:8080

3. Initialization

Enter for the first time and get the password according to the prompt

Go to the next interface and select recommended plug-ins

Wait for the installation to complete

Register an account to enter the Jenkins home page

4. Install the maven plugin

Enter the option: Manage Jenkins -> Manage Plugin -> Optional Plugin -> Search maven -> Select Install

Click and wait for the installation to complete

5. Configure global variables

Enter the option: Manage Jenkins -> Global Tool Configuration

1. Maven configuration: select the configuration file path. Configuration file in maven installation directory

2. Configure jdk:

3. Configure Git and get the location through the command which git

4. Configure maven, maven installation path

5. After the configuration is complete, click Apply

3. Project creation

On the homepage, select New Item, enter the interface, enter the project name, select the maven project, and click OK

1. Configure git, enter warehouse address, account number, password, branch

2. Configure the trigger. The input here is separated by 5 * spaces, which means that as long as there is code submission, it will be built.

3. Configure Pre Steps (operations performed before building), select Execute shell

#Stop the running project, change test to target jar
echo "Stopping SpringBoot Application"
sp_pid=`ps -ef | grep test.jar | grep -v grep | awk '{print $2}'`
if [ -z "$sp_pid" ];
then
 echo "[not find sp-tomcat pid]"
else
 echo "find result: $sp_pid"
 kill -9 $sp_pid
the fi

4. Configure packaging: clean package -Dmaven.test.skip=true

5. Configure the operation after the build. Note that the first build will fail, because the jar package path in the script is different. After the build, find the corresponding directory jar and change the following configuration. You can query the build log in the build history to find the corresponding location.

echo "startUp"
#startup.sh Starting the project
echo "Grant permission to current user"
chmod 777 /root/.jenkins/workspace/test/target/test-0.0.1-SNAPSHOT.jar
echo "Execute"
sleep 1s
BUILD_ID=dontKillMe nohup java -Xms1024M -Xmx1024M -XX:PermSize=256M -XX:MaxNewSize=256M -XX:MaxPermSize=256M -jar /root/.jenkins/workspace/test/target/test-0.0.1-SNAPSHOT.jar >> /dev/null 2> &1 &
echo "startEnd"

6. After the configuration is completed and saved, execute Build Now.