Notice:文本以定时任务tms-schedule-war为例梳理了一下通过hudson来新建任务实现自动化发布的流程
Step1:点击hudson新建任务来构建一个新的项目
Step2:点击ok,进入到配置页面。主要有三处需要设置
1)基本信息:项目名称,描述等
2)源码管理,如果用的是svn的话,就选择subversion,并设置相应的路径。
3)设置maven的一些信息和需要执行的脚本
选中你需要设置的模块,接下来设置详情
Shell脚本放在120的/opt/hudson/deploy下面
关于shell说明
Sh /opt/hudson/deploy /deployRemote.sh 执行该脚本
后面4个参数分别是
Tms-schedule-war : 项目名
10.161.161.172 :目标机器ip
/opt/schedule-tomcat :目标机器中的tomcat (step3中会做说明)
具体脚本如下:
WAR_NAME=$1;IP=$2;TOMCAT_PATH=$3;SRC_PATH=
4
;
e
c
h
o
′
w
a
r
n
a
m
e
=
′
4; echo 'war_name='
4;echo′warname=′WAR_NAME
echo 'target_ip='
I
P
e
c
h
o
′
t
o
m
c
a
t
p
a
t
h
=
′
IP echo 'tomcat_path='
IPecho′tomcatpath=′TOMCAT_PATH
echo ‘src_path=’$SRC_PATH
echo ‘kill and clean begin…’
/opt/hudson/deploy/killCleanTomcat.exp $IP $TOMCAT_PATH $WAR_NAME a
echo ‘kill and clean …OK!!!’
/opt/hudson/deploy/scpFileRoot.exp
S
R
C
P
A
T
H
/
t
a
r
g
e
t
/
SRC_PATH/target/
SRCPATH/target/WAR_NAME-*.war $IP $TOMCAT_PATH/webapps a
echo ‘copy war to tomcat…OK!!!’
sleep 2
/opt/hudson/deploy/sshStartTomcat.sh $IP $TOMCAT_PATH a
echo ‘restart tomcat…OK!!!’
killCleanTomcat.exp的脚本:
#!/usr/bin/expect -f
set IP [lindex $argv 0]
set TOMCAT_PATH [lindex $argv 1]
set WAR_NAME [lindex $argv 2]
set timeout 10
set USER_NAME root
set PASS_WORD a111111!
spawn ssh
U
S
E
R
N
A
M
E
@
USER_NAME@
USERNAME@IP
expect {
“yes/no" { send “yes\r”; exp_continue}
“password:" { send “KaTeX parse error: Can't use function '\r' in math mode at position 10: PASS_WORD\̲r̲" } } sleep…TOMCAT_PATH’ | grep -v ‘grep’ | cut -c 9-15 | xargs kill -9\r”
send "rm -rf
T
O
M
C
A
T
P
A
T
H
/
w
e
b
a
p
p
s
/
TOMCAT_PATH/webapps/
TOMCATPATH/webapps/WAR_NAME-\r”
sleep 2
send "rm -f
T
O
M
C
A
T
P
A
T
H
/
w
e
b
a
p
p
s
/
TOMCAT_PATH/webapps/
TOMCATPATH/webapps/WAR_NAME.war-\r”
send “exit\r”
expect eof
scpFileRoot.exp的脚本:
#!/usr/bin/expect -f
set FULL_WAR_NAME [lindex $argv 0]
set TARGET_PATH [lindex $argv 2]
set IP [lindex $argv 1]
set USER_NAME root
set PASS_WORD a111111!
set timeout 10
spawn scp $FULL_WAR_NAME
U
S
E
R
N
A
M
E
@
USER_NAME@
USERNAME@IP:KaTeX parse error: Can't use function '\r' in math mode at position 47: …no" { send "yes\̲r̲"; exp_continue…PASS_WORD\r" }
}
expect “#*”
sshStartTomcat.sh的脚本
#!/usr/bin/expect -f
set IP [lindex $argv 0]
set TOMCAT_PATH [lindex $argv 1]
set timeout 10
set USER_NAME root
set PASS_WORD a111111!
spawn ssh
U
S
E
R
N
A
M
E
@
USER_NAME@
USERNAME@IP
expect {
“*yes/no” { send “yes\r”; exp_continue}
“password:" { send “$PASS_WORD\r” }
}
expect "#”
send “cd $TOMCAT_PATH/bin\r”
send “./startup.sh &\r”
send “exit\r”
expect eof
至此:hudson上面的配置已经设置完成
Notice: 在执行脚本的时候,要注意权限问题 最好将需要调用到的脚本的权限设置为755 可读可写
Step3: 目标机器tomcat中的修改
1) 修改server.xml中的端口好 以及设置中文编码格式
2) 修改启动参数 catalina.sh
将配置中心config放到tomcat的webapps中(也可以选择其他路径)
再将改路径配置到 -Dglobal.config.path(也可选择其他路径)
大功告成!!!