jenkins 集成部署站点

 Jenkins 集成部署站点

最近为了在两台Linux中,去作jenkins的集成发布,所以在部署jenkins的这台电脑(A) 要把打包好的war 部署到远程服务器(B)

  • 在两台电脑间进行传输的时候,要配置一个免登入的配置
    1.1. 在A电脑上 ssh-keygen生成秘钥,同时把生成的id_ras.pub文件上传到B服务器(/home/${user}/.ssh/authorized_keys文件里)
      
    1.2. 修改B服务器的 .ssh 文件属性为 700,authorized_keys为 600
  • 在A上写好 jenkins 部署的文件
  1. build_post.sh
 1 #! /bin/sh/
 2 #复制文件
 3 echo "cp web.war   --------------->   ROOT.war"
 4 #目标文件
 5 rootfile=/opt/app/deploy/build/web/ROOT.war
 6 #执行复制
 7 cp -r -f  /home/developer/.jenkins/workspace/web-build/build/web/libs/web.war $rootfile
 8 
 9 
10 echo "send file to remote server"
11 #将本地文件上传的服务器
12 sh /opt/app/deploy/web/upload.sh -f /opt/app/deploy/web/hostfile -s  $rootfile -p web
13 
14 if [ $? -ne 0 ] ;then
15 
16 echo "execute upload shell failed"
17 
18 exit $?
19 
20 fi
21 
22 echo "send success"
23 exit $?

 

  1. upload.sh的文件
 1 #! /bin/sh
 2 
 3 echo "start sync war to remote server"
 4 
 5 
 6 while getopts f:s:p: OPT;do
 7         case $OPT in
 8                 f)
 9                    files="$OPTARG $files"
10                    ;;
11                  s)
12                  sourcefiles="$OPTARG"
13                 ;;
14                  p)
15                 project="$OPTARG"
16 echo "project is $project"
17                 ;;
18                 *)
19                   echo "usage :`basename $0` [-f hostfile] [-s source file]  <from><to>"
20                   exit 2
21                   esac
22                   done
23 
24 
25 shift `expr $OPTIND - 1`
26 
27 
28 if [ "" == "$project" ] ;then
29 echo "usage: `basename $0` [-p project]"
30 exit 2
31 fi
32 
33 if [ "" == "$sourcefiles" ] ;then
34 echo "usage:`basename $0` [-s sourcefile]"
35 exit 2
36 fi
37 
38 
39 if [ "" == "$files" ] ;then
40 echo "usage :`basename $0` [-f hostfile] <from><to>"
41 exit 2
42 fi
43 
44 for file in $files
45 do
46 if [ ! -f "$file" ] ;then
47 echo "no hostfile file : $file"
48 exit 2
49 fi
50 
51 hosts="$hosts `cat $file`"
52 done
53 
54 for host in $hosts; do
55 
56 echo "send $sourcefiles to $host"
57 
58 echo "remote server url :/home/appuser/upload/$project/"
59 
60 scp -r  $sourcefiles appuser@$host:/opt/app/webapps/$project/webapps/
61 
62 done
63 
64 exit

 

  • 这里要启动远程服务器的tomcat重启站点
#! /bin/sh

echo "call remote tomcat stop"

hostfile=/opt/app/deploy/web/hostfile

hosts=`cat $hostfile`

for host in $hosts; do

ssh appuser@$host '/opt/app/bin/tomcat-web-stop.sh'
echo "wait 5s for tomcat stop"

sleep 5
if [ $? -eq 0 ] ; then
  echo 'tomcat Stopped'

ssh appuser@$host '/opt/app/bin/tomcat-web-start.sh'

if [ $? -eq 0 ] ;then
echo "tomcat started"

echo "wait 30s for tomcat starting"

sleep 30
# 查看是否已经启动
url="http://$host:8091/status"

echo "curl $url"

curl $url

fi
fi
done
exit $?

 

posted @ 2016-05-20 09:43  一字真言  阅读(615)  评论(0编辑  收藏  举报