Shell scripts automatically deploy Springboot projects

1, Introduction

The project deployment described above is manual deployment, that is, every step of the deployment process requires our manual operation. Next, we need to explain the automatic deployment of the project to simplify the operation of project deployment. Let's first understand the process and operation steps of automatic deployment of the project as a whole.

The operation steps are as follows:

1). Create a remote warehouse on Gitee and push the local project code to the remote warehouse

2) Installing Git in Linux and cloning code

3). Installing maven in Linux

4). Write Shell script (pull code, compile, package, start)

5). Grant users permission to execute Shell scripts

6). Execute Shell script

2, Centos 7 software installation

2.1 Git software installation

Install git online through yum command and execute the following instructions:

yum list git            list git Installation package
yum install git            Online installation git

Through the above instructions, after installing git, we can verify the GIT environment through git --version.

2) Git clone code

cd /usr/local/
git clone git address

2.2 Maven installation

Since our project is a maven project, we need Maven instructions to compile and package the project, so we need to install Maven. The specific operation steps are as follows:

1). Upload maven's installation package

2) Unzip the maven installation package to the / usr/local directory

tar -zxvf apache-maven-3.5.4-bin.tar.gz -C /usr/local

3). Configure the environment variables in the / etc/profile configuration file

vim /etc/profile

Modify the configuration file, enter the command mode, and press G Switch to the last line and press a/i/o Enter the insertion mode, and then add the following contents at the end :
export MAVEN_HOME=/usr/local/apache-maven-3.5.4

Then press ESC Enter the command mode and enter :wq Save and exit

In order for the configured environment variables to take effect, the following instructions need to be executed:

source /etc/profile

4). Modify maven's settings XML configuration file to configure the local warehouse address

A. Switch directory

cd /usr/local/apache-maven-3.5.4/conf

B. Edit settings XML configuration file

vim settings.xml

Add the following configuration to configure the local warehouse address: Here I suggest to use the local warehouse address configured by myself


And in settings In the tag in XML, configure Alibaba cloud

<name>aliyun maven</name>

2.3 Linux permissions

Shell script

echo =================================
echo     Automated deployment script launch
echo =================================

echo Stop the original running project

tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
	echo 'Stop Process...'
	kill -15 $tpid
sleep 2
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`
if [ ${tpid} ]; then
	echo 'Kill Process!'
	kill -9 $tpid
	echo 'Stop Success!'

echo Ready from Git The warehouse pulls the latest code
cd /usr/local/helloworld

echo Start from Git The warehouse pulls the latest code
git pull
echo Code pull completed

echo Start packing
output=`mvn clean package -Dmaven.test.skip=ture`

cd target

echo Start project
nohup java -jar hellowprld.jar &> helloworld.log &
echo Project launch completed

However, if the Shell script wants to execute normally, it also needs to assign execution permission to the Shell script. Because Linux system is a multi-user operating system, and for each user, Linux will strictly control the operation authority. Next, we need to introduce the permission control of Linux system.

1). chmod (English spelling: change mode) command is a command that controls the user's permissions on files

2) There are three kinds of permissions in Linux: read (r), write (w) and execute (x)

3). Linux file permissions are divided into three levels: file Owner, user Group and Other Users

4). Only the owner and superuser of the file can modify the permissions of the file or directory

5). To execute a Shell script, you need to have execution permission (x) on this script file. If not, it cannot be executed

The chmod command can use octal numbers to specify permissions (0 - for none, 1 - execute x, 2 - write W, 4 - read r):

value jurisdiction rwx
7 Read + Write + execute rwx
6 Read + Write rw-
5 Read + execute r-x
4 read-only r--
3 Write + execute -wx
2 Write only -w-
1 Execute only --x
0 nothing ---

give an example:

chmod 777   Grant read, write and execute permissions to all users
chmod 755   Grant read, write and execute permissions to file owners, and grant read and execute permissions to users in the same group and other users
chmod 210   Grant write permission to the file owner, grant execution permission to users in the same group, and no permission to other users

be careful:

The three numbers represent the permissions of different users

  • The first digit indicates the permission of the file owner
  • The second digit indicates the permissions of users in the same group
  • The third digit indicates the permissions of other users

2.2.7 authorize and execute scripts

In the test phase, we can give everyone permission to execute the shell script. Therefore, the following instructions can be executed:

chmod 777

Note: when the maven command is executed for packaging, the first execution may take a long time, because when packaging the maven project, you need to download the jar packages and plug-ins that the project depends on from the central warehouse (you can configure Alibaba cloud private server in settings.xml to speed up the download).

Posted by SparkleD on Sun, 17 Apr 2022 15:53:20 +0930