自动打包发布项目脚本(svn)

  1 #!/bin/bash
  2 
  3 export work_dir=/home/jenkins/work_dir/uat-coms-123
  4 
  5 username=user
  6 password=password
  7 name=xxx-service-
  8 coms_addr="ip1"
  9 api_addr="ip2"
 10 coms_user=root
 11 
 12 cd ${work_dir}
 13 function svn_update() {
 14     for i in $@
 15         do
 16             cd ${work_dir}
 17             if [ -d "$i" ]; then
 18                 cd $i
 19                 local_revision=`/usr/bin/svn info |awk '/版本:/{i++;if(i<2){print $0}else{exit;}}'|cut -d' ' -f2`
 20                 url=`/usr/bin/svn info|grep URL|cut -d' ' -f2`
 21                 svn_revision=`/usr/bin/svn info ${url} |awk '/版本:/{i++;if(i<2){print $0}else{exit;}}'|cut -d' ' -f2`
 22                 if [ $local_revision -lt $svn_revision ];then
 23                     commit_people=`svn info ${url} |grep "最后修改的作者"|cut -d' ' -f2`
 24                     commit_time=`svn info ${url} |grep "最后修改的时间"`
 25                     echo "$i项目版本库有提交,现在开始更新本地库"
 26                     echo "$i项目最后提交项目工作人员为: $commit_people"
 27                     echo "$i项目${commit_time}"
 28                     /usr/bin/svn up
 29                     if [ $? -eq 0 ];then 
 30                         echo "更新成功"
 31                         return 0
 32                     else 
 33                         echo "更新失败"
 34                         return 1
 35                     fi
 36                 else
 37                     echo "本地已经是最新版本"
 38                     return 2
 39                 fi
 40             else
 41                 echo "该项目在服务器上不存在,将首次拉取代码"
 42                 /usr/bin/svn --username $username --password $password co https://xxx.xxx.xxx/svn/coms/trunk/coms-common-parent/$i 
 43                 cd ${work_dir}
 44                 return 3
 45             fi
 46         done
 47 }
 48 
 49 function mvn_common_install() {
 50     for p in $@
 51         do
 52             svn_update $p
 53             if [ $? -eq 0 -o $? -eq 3 ];then     
 54                 cd ${work_dir}/$p 
 55                 echo "开始编译$p"
 56                 /data/maven-3.3.9/bin/mvn clean install -Denv=uat deploy
 57                 if [ $? -eq 0 ];then
 58                     echo "编译成功"
 59                     cd ${work_dir}
 60                 fi
 61             fi
 62         done
 63 }        
 64 
 65 function mvn_service_install() {
 66     for p in $@
 67         do
 68             cd ${work_dir}
 69             svn_update $p
 70             if [ $? -eq 0 -o $? -eq 3 ];then     
 71                 cd ${work_dir}/$p 
 72                 echo "开始编译$p"
 73                 /data/maven-3.3.9/bin/mvn clean install -Denv=uat
 74                 if [ $? -eq 0 ];then
 75                     echo "编译成功"
 76                 fi
 77             fi
 78         done
 79 }
 80 
 81 function pack_common() {
 82     mvn_common_install 公共包1 公共包2 公共包3...
 83 }
 84 
 85 function pack_service() {
 86     mvn_service_install $1
 87 }
 88 
 89 function help_usage() {
 90  cat <<EOF       
 91        1:包名1 2:包名2 3:包名3
 92     4:包名4 5:包名5
 93 EOF 
 94 }
 95 
 96 function transfer_coms_consumer() {
 97     consumer_path="/path"
 98     scp -r ${work_dir}/$1/target/{$1.jar,lib} comsadmin@${coms_addr}:${consumer_path}/$1
 99     if [ $? -eq 0 ];then
100         echo "代码发布成功"
101         return 10
102     else
103         return 11
104     fi
105 }
106 
107 function transfer_coms_service() {
108     service_path="/path"
109     service_jar_name=`echo $1 |cut -d'-' -f3`
110     scp -r ${work_dir}/$1/target/{$1.jar,lib} comsadmin@${coms_addr}:${consumer_path}/${service_jar_name}
111     if [ $? -eq 0 ];then
112         echo "代码发布成功"
113         return 10
114     else
115         return 11
116     fi    
117 }
118 
119 function transfer_coms_bkend() {
120     bkend_path="path"
121     scp -r ${work_dir}/$1/target/{$1.jar,lib} apiadmin@${api_addr}:${bkend_path}
122     if [ $? -eq 0 ];then
123         echo "代码发布成功"
124         return 10
125     else
126         return 11
127     fi
128 }
129 
130 function restart_coms() {
131     ssh comsadmin@${coms_addr} '/home/comsadmin/service.sh $1'
132 }
133 
134 while true
135     do
136         help_usage
137         declare -A dict
138         dict=([1]="包名1" [2]="包名2" [3]="包名3" [4]="包名4" [5]="包名5")
139         consumer_64=(包名1 包名2 包名3)
140         service_64=(包名4 包名5)
141         read -p "请输入需要更新的应用名称前面的ID号>:" program_id
142         count=0
143         for key in $(echo ${!dict[*]})
144             do 
145                 if [ "${program_id}" -eq $key ];then
146                     program_name=`echo ${dict[$key]}`
147                     if [[ "${consumer_64[@]}" =~ ${program_name} ]];then
148                         echo "开始更新${program_name}项目"
149                         if [ $count -eq 0 ];then
150                             pack_common
151                         fi
152                         pack_service ${program_name}
153                         sleep 2
154                         echo "开始${program_name}发布代码"
155                         transfer_coms_consumer ${program_name}
156                         if [ $? -eq 10 ];then
157                             echo "代码更新完成,即将重启"
158                             restart_coms ${program_name}
159                         else
160                             echo "代码更新失败,请检查原因"
161                         fi
162                         let count+=1
163                     elif [[ "${service_64[@]}" =~ ${program_name} ]];then
164                         echo "开始更新${program_name}项目"
165                         if [ $count -eq 0 ];then
166                             pack_common
167                         fi
168                         pack_service ${program_name}
169                         transfer_coms_service ${program_name}
170                         if [ $? -eq 10 ];then
171                             echo "代码更新完成,等待重启"
172                             restart_coms ${program_name}
173                         else
174                             echo "代码更新失败,请检查原因"
175                         fi                        
176                         let count+=1
177                     else
178                         echo "开始更新${program_name}项目"
179                         if [ $count -eq 0 ];then
180                             pack_common
181                         fi
182                         pack_service ${program_name}
183                         transfer_coms_bkend ${program_name}
184                         if [ $? -eq 10 ];then
185                             echo "代码更新完成,等待重启"
186                             ssh apiadmin@${api_addr} '/home/apiadmin/coms/consumer/coms-consumer-bkend/startBkend.sh'
187                         else
188                             echo "代码更新失败,请检查原因"
189                         fi                        
190                         let count+=1
191                     fi
192                 fi
193             done
194     done     
195  

 

posted @ 2018-05-04 13:52  一米八大高个儿  阅读(1614)  评论(0编辑  收藏  举报