使用Github Action来自动化构建Maven项目
简介
本文主要介绍如何把Github仓库中的Maven项目通过Github Action进行自动化构建后生成Jar包,接着拷贝到服务器进行部署。
服务器脚本
脚本
application.sh
用于启停应用: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
# application jar's name
appDir=~/java_deploy
application=$(find $appDir -type f -name "*.jar")
## determine existence of processus of application
isExist(){
## check pid
pid=`ps -ef | grep ${application} | grep -v grep | awk '{print $2}'`
## if pid not exist return 0 else 1
if [ -z "${pid}" ]; then
return 0
else
return 1
fi
}
log=$appDir/application.log
## start application in background and generate appDir/application.log
function start()
{
## deermine existence of processus
isExist
## if not running
if [ $? -eq "0" ]; then
echo "Your ${application} is not running"
nohup java -jar $application --spring.profiles.active=test > $log &
echo "${application} startup success"
else
echo "${application} is running, pid=${pid} "
fi
}
## stop the processus application
function stop()
{
isExist
## if not exist - ok
if [ $? -eq "0" ]; then
echo "${application} is not running"
else
echo "${application} is running, pid=${pid}, prepare kill it "
# if exist - kill the processus
kill -9 ${pid}
echo "${application} has been successfully killed"
fi
}
## check status
function status()
{
appPid=`ps -ef |grep java|grep $application |awk '{print $2}'`
if [ -z $appPid ]; then
echo -e "Not running "
else
echo -e "Running [$appPid] "
fi
}
## restart
function restart(){
stop
start
}
## arg
case "$1" in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
echo "please enter the correct commands: "
echo "such as : sh application.sh [ start | stop | restart |status ]"
;;
esac
配置仓库Setting –> Secrets
Key | Value |
---|---|
HOST | 你的服务器地址 |
USERNAME | SSH连接用户名 |
PASSWORD | SSH连接密码 |
PORT | SSH端口 |
Github Action
1 | name: Java CI with Maven & Deploy |
使用Github Action来自动化构建Maven项目
https://note.vgearen.com/2021/07/10/使用Github-Action来自动化构建Maven项目/