码迷,mamicode.com
首页 > 其他好文 > 详细

jekins测试环境自动化

时间:2018-03-21 17:27:27      阅读:342      评论:0      收藏:0      [点我收藏+]

标签:ted   config   func   tomcat   host   copy   jenkin   general   star   

最近搭建测试环境自动化,之前都是用机器猫。机器猫的流程大概是这样,开发打包上传到svn,给测试一个svn地址,测试到机器猫上传文件,然后再运行启动。

为了减去开发打包这个环节,所以专用jenkins。

实现需求如下:

1、自动拉取git代码,自动打包

2、备份远程机的jar包

3、上传至远程机,并启动项目

4、提供回滚功能

 

cat /data/jekins_shell/autoshell

#参数 $1项目  $2方法  $roll_bak参数
app=meng

if [ -n "$1" ];then
  app=$1
fi

app_home=/data/webroot/youximao/${app}
app_jar=${app_home}/${app}.jar
bak_home=${app_home}/bak
bak_file=${bak_home}/${app}_$(date +"%Y-%m-%d_%H%M%S").jar

if [ ! -d "$bak_home" ]; then
  mkdir $bak_home
fi

#保留个数
save_count=10


#执行回滚操作
function roll_back(){
  echo  filenameis: ${1}
  filename=`ls $bak_home/*jar |sort -n|head -n 1`
  echo $1
  if [ -n "$1" ]; then
     if [ -e $bak_home/$1 ]; then
       filename=$bak_home/$1
     fi
  fi

  echo old${filename}  
  if [ -e $filename ]; then
    echo 拷贝开始 $filename   $app_jar
    \cp -f  $filename $app_jar
    echo 拷贝完成 开始启动
    sh /etc/init.d/tomcat-$app restart 
  fi
}

function add_bak(){

  cp $app_jar ${bak_file}

  #文件总个数
  count=`ls $bak_home/ |wc| awk ‘{print $1}‘`

  #删除个数
  del_count=$[$count-$save_count]

  #倒序排列 删除最后几条
  if [ $del_count -gt 0 ]; then

        echo $del_count" count deleted "
        ls ${bak_home}/*jar  |sort -r|tail -n $del_count|xargs rm -rf
  fi

}

case $2 in
       add_bak)
     add_bak;;
       roll_back)
         roll_back $3;;
esac

 [root@jenkins-50-130 .ssh]# cat ~/.jenkins/jekins_shell/general_shell 

技术分享图片
#!/bin/bash



#app名称
app=$1

#远程服务器地址
remote_host=root@${2}

#最新的jar包
new_jar=/root/.jenkins/workspace/${app}/target/${app}.jar

#远程jar包目录
remote_home=/data/webroot/youximao/${app}

#远程jar包
remote_jar=${remote_home}/${app}.jar
ssh ${remote_host} "/data/jekins_shell/autoshell ${app} add_bak"

scp ${new_jar} ${remote_host}:${remote_jar}

#远程执行
ssh ${remote_host} "/etc/init.d/tomcat-${app} restart >/dev/null 2>&1"
View Code


1、允许root登陆
vi /etc/ssh/sshd_config #PermitRootLogin no (注释掉)
/etc/init.d/sshd restart 重启sshd服务
2、创建自动化脚本
mkdir /data/jekins_shell
上传autoshell 脚本
chomd 555 autoshell 设置为可执行权限

3、登陆jekins 服务器
cd /root/.ssh
执行 ssh-copy-id 192.168.50.197 将公钥发送到远程,显现登陆免密【远程没有添加的情况下】
登陆远程 ssh root@192.168.50.197 必须要登陆一次才行

scp autoshell root@192.168.50.211:/data/jekins_shell/

 

4、创建项目 类型为maven

调用默认的shell 参考cat
编写自定义shell 参考meng

 

项目回滚

./autoshell meng roll_back

 

jekins测试环境自动化

标签:ted   config   func   tomcat   host   copy   jenkin   general   star   

原文地址:https://www.cnblogs.com/milanmi/p/8618048.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!