-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkins_deploy
211 lines (193 loc) · 7.37 KB
/
Jenkins_deploy
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
Jenkins 部署构建项目
一、General
1、丢弃旧的构建(勾选丢弃旧的构建,选择是否备份被替换的旧包)
二、源码管理
1、配置Git或Subversion
2、
三、构建触发器
1、Poll SCM(指定检查 SVN 代码是否有提交的时间)
四、构建环境
1、Add timestamps to the Console Output(代码构建的过程中会将日志打印出来)
五、Bulid
1、Goals and options(指定 mvn 执行目标或选项)
-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
-Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。
六、Post steps
1、Execute Windows batch command(windows下执行jenkins自动部署)
@echo off
set tomcat_path="C:\develop\apache-tomcat-8.5.29"
set war_name="demo_spring_boot-0.0.1-SNAPSHOT.war"
set project_name="demo_spring_boot-0.0.1-SNAPSHOT"
set jenkins_path="C:\develop\Jenkins\workspace\test\target\"
set t_port=8085
cd \
cd C:\develop\Jenkins\workspace\test\target\
if exist %war_name% (
cd \
cd %tomcat_path%\bin
for /f "tokens=3 delims=: " %%a in ('netstat -an') do (
if "%%a"=="%t_port%" goto en
)
if not "%%a"=="%t_port%" goto en1
:en
echo "tomcat is Opening..."
call shutdown.bat
goto end
:en1
echo "tomcat is Not Opening..."
goto end
cd %tomcat_path%\webapps
if exist %war_name% (
rd /s/q %project_name%
echo "执行命令tomcat下 rd /s/q %project_name%"
)
if exist %project_name% (
goto existp
) else (
goto notexistp
)
:existp
rd /s/q %project_name%
echo "执行命令tomcat下 rd /s/q %project_name%"
copy /y %jenkins_path%%war_name%
echo "执行命令tomcat下 copy /y %jenkins_path%%war_name%"
goto end
:notexistp
copy /y %jenkins_path%%war_name%
echo "执行命令tomcat下 copy /y %jenkins_path%%war_name%"
goto end
:end
echo "valid end ,Ready to start tomcat"
cd %tomcat_path%\bin
call startup.bat
)
2、Execute shell(linux下执行)
#!/bin/bash
#defined variable
TOMCAT_HOME="/test/apache-tomcat-8.5.30"
PROJECT="demo_boot"
BAK_HOME="/test/jenkinsBakProject/demo_boot/"
WAR_HOME="/var/lib/jenkins/workspace/demo_boot/target/"
WAR_NAME="demo_boot-0.0.1-SNAPSHOT.war"
STARTTOMCAT="/test/apache-tomcat-8.5.30/bin/startup.sh"
current_time=`date "+%Y-%m-%d %H:%M:%S"`
LAST_MONTH=`date -d "last month" +%s`
BUILD_ID="DONTKILLME"
echo "current date $current_time"
echo "last month the same times $LAST_MONTH"
#get tomcat process
processid=`ps -ef |grep tomcat |grep -v grep |awk '{print $2}'`
echo "process ID:$processid"
if [ "$processid" != "" ]; then
#loop kill
for id in $processid
do
echo "Kill the process:$id"
kill -9 $id
done
fi
#operation tomcat
pwd
cd $TOMCAT_HOME/webapps/
echo "remove project..."
rm -rf "$PROJECT*"
#cp war
echo "copy war to $TOMCAT_HOME/webapps/"
cd $WAR_HOME
cp $WAR_NAME $TOMCAT_HOME/webapps/
cd $TOMCAT_HOME/webapps/
mv $WAR_NAME $PROJECT".$current_time.war"
#bak project
echo "loop file,save the last month ,Backup project"
for file in $BAK_HOME/*
do
if [ -f "$file" ];
then
file_date_l=${file#*.}
file_date=${file_date_l%.*}
file_times=`date -d "$file_date" +%s`
echo "current file createtime:$file_times"
if [ $file_times -lt $LAST_MONTH ]; then
echo "remove $file"
rm -rf "$file"
fi
fi
done
#bak project
cp $PROJECT".$current_time.war" $BAK_HOME
#rename tomcat project
mv $PROJECT".$current_time.war" $PROJECT".war"
#start tomcat
$STARTTOMCAT
3、远程部署
(1)、系统管理 配置SSH Servers
(2)、任务配置,构建后操作,选择send bulid artififacts over ssh
远程服务器执行shell
例:
#!/bin/bash
#defined variable
TOMCAT_HOME="/root/apache-tomcat-8.5.31"
PROJECT="demo_boot"
BAK_HOME="/root/jenkinsBakProject/demo_boot/"
WAR_HOME="/root/"
WAR_NAME="demo_boot-0.0.1-SNAPSHOT.war"
STARTTOMCAT="/root/apache-tomcat-8.5.30/bin/startup.sh"
current_time=`date "+%Y-%m-%d %H:%M:%S"`
LAST_MONTH=`date -d "last month" +%s`
BUILD_ID="DONTKILLME"
echo "current date $current_time"
echo "last month the same times $LAST_MONTH"
#get tomcat process
processid=`ps -ef |grep tomcat |grep -v grep |awk '{print $2}'`
echo "process ID:$processid"
if [ "$processid" != "" ]; then
#loop kill
for id in $processid
do
echo "Kill the process:$id"
kill -9 $id
done
fi
#operation tomcat
cd /
cd $TOMCAT_HOME/webapps/
echo "remove project..."
rm -rf "$PROJECT*"
#cp war
echo "copy war to $TOMCAT_HOME/webapps/"
cd $WAR_HOME
cp $WAR_NAME $TOMCAT_HOME/webapps/
cd $TOMCAT_HOME/webapps/
mv $WAR_NAME $PROJECT".$current_time.war"
#bak project
cd $WAR_HOME
if [ ! -d "jenkinsBakProject" ]; then
mkdir jenkinsBakProject
else
cd jenkinsBakProject
if [ ! -d "demo_boot" ]; then
mkdir demo_boot
fi
fi
echo "loop file,save the last month ,Backup project"
for file in $BAK_HOME/*
do
if [ -f "$file" ];
then
file_date_l=${file#*.}
file_date=${file_date_l%.*}
file_times=`date -d "$file_date" +%s`
echo "current file createtime:$file_times"
if [ $file_times -lt $LAST_MONTH ]; then
echo "remove $file"
rm -rf "$file"
fi
fi
done
#bak project
cd $TOMCAT_HOME/webapps/
cp $PROJECT".$current_time.war" $BAK_HOME
#rename tomcat project
mv $PROJECT".$current_time.war" $PROJECT".war"
#start tomcat
$STARTTOMCAT