# 使用shell脚本部署jar包到linux服务器

# 脚本说明

这里我们把前端项目打包到spring boot应用的static目录中,而不是选用nginx发布前端。可根据项目实际情况来修改此脚本。 因为本人当前使用项目基本上都是通过jar包的方式发布,考虑到方便发布、降低打包的容错率、提高部署速度、减少宕机时间等因素, 故使用shell脚本一键部署。 业界更好的做法是使用DevOps等工具对项目进行全生命周期的管理,这部分内容暂不在本文讨论范围内。
上传和部署均设置了5s的延时,可以随时键入Ctrl+C中断脚本运行。

注意

  1. 如果想要更进一步减少宕机时间,可使用软连接的方式链接jar包以节省拷贝jar包的带来的时间损耗。
  2. 登录服务器后的提示符如果不是#,则需要修改脚本进行适配。

# 部署目录结构

为了方便项目服务管理,软链接intellect-chart.service文件到Linux系统服务下,利用Linux系统服务来管理我们的应用。 可参考上篇博文进行Linux服务的创建。upload路径是脚本上传编译产物的路径,每次上传都会覆盖之前的文件。每次运行脚本都会在backup目录下进行备份。

注意

  1. backup和upload路径需要手动创建。
  2. 若项目发布失败需要手动恢复备份。
├── backup
│   └── warning-chart_202112171752.jar
├── intellect-chart.service
├── intellect-chart-v1.0.0.jar
├── logs
│   └── 2021-12-17
│       └── 2021-12-17.log
└── upload
    └── intellect-chart-v1.0.0.jar
1
2
3
4
5
6
7
8
9

# 依赖命令

ssh git yarn/npm mvn expect

# shell脚本

#!/bin/bash

set -e

# workdir
work_path="/Users/xxx/Files/Build/lanxi"
frontend_path="$work_path/liwinon-intelligent-chart"
backend_path="$work_path/intellect-chart"

# git
frontend_branch="master"
backend_branch="master"

# target
target_name="intellect-chart-v1.0.0.jar"
target="$backend_path/target/$target_name"

# ssh
host="192.168.0.1"
username="root"
password="password"
host_work_path="/root/intellect-chart/"
upload_path="/root/intellect-chart/upload/"
backup_path="/root/intellect-chart/backup/"
backup_prefix="intellect-chart"

# run
port="8080"
run_cmd="systemctl restart intellect-chart"



echo "🦀️ Processing frontend build..."
cd "$frontend_path"
git checkout "$frontend_branch"
git pull
# yarn install
yarn run build

echo "🦀️ Cleaning the old frontend dist files..."
rm -r -f "$backend_path"/src/main/resources/static/*

echo "🦀️ Copying new dist files to static path..."
cp -rf dist/* "$backend_path"/src/main/resources/static/

echo "🦀️ Processing backend build..."
cd "$backend_path"
git checkout "$backend_branch"
git pull
mvn clean compile package -DskipTests -Dfile.encoding=UTF-8

echo "🍺 Build successfully!"

for i in {5..1}
do
  printf "\r\033[33m ⚠️  Will be uploaded to the 「%s」 server in %s seconds, type \"Ctrl + C\" to abort! \033[0m" "$host" "$i";
  sleep 1
done

echo ""

echo "🦀️ Processing the building target to upload..."
cd "$backend_path"
expect <<EOF
spawn scp $target $username@$host:$upload_path
expect "*password:"
send $password\r
expect eof
EOF

echo "🍺 Upload successfully!"

for i in {5..1}
do
  printf "\r\033[33m ⚠️  Will be deployed to the 「%s」 server in %s seconds, type \"Ctrl + C\" to abort! \033[0m" "$host" "$i";
  sleep 1
done

echo ""

echo "🦀️ Processing the building target to deploy..."
backup_file_name="$backup_prefix_$(date "+%Y%m%d%H%M").jar"
cd "$work_path"
expect <<EOF
    set timeout 30

    spawn ssh $username@$host
    expect "*password:"
    send $password\r

    expect  "#" {
      # backup the old jar file to backup path
      send "cd $host_work_path\r"
      send "mv $target_name $backup_path$backup_file_name\r"
      # copy the target from upload path
      send "cp -rf ./upload/$target_name .\r"
      # restart service
      send "$run_cmd\r" }

    send "exit\r"
    expect eof
EOF

echo "🍺 Deploy successfully!"
echo "          App running at: http://$host:$port"
echo "          The service restart might take a while..."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
上次更新: 2 years ago