zoukankan      html  css  js  c++  java
  • jekins测试环境自动化

    最近搭建测试环境自动化,之前都是用机器猫。机器猫的流程大概是这样,开发打包上传到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 设置为可执行权限

     ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' -C "IP"  创建key

    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

    pid=`ps -ef|grep java |grep -v 'grep' |grep "$plat_home"|awk '{print $2}'`
    if [[ -n  $pid ]]; then
        echo -e $jar "is running"
            exit
    else
        su - zoumi -c "$java_home/java $java_arg  -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=9900,suspend=n  -jar $plat_home/$jar --spring.config.location=$pro_file/$pro_name &"
        
    sleep 2
       pid2=`ps -ef|grep java |grep -v 'grep' |grep "$plat_home"|awk '{print $2}'`
       if [[ -n $pid2 ]]; then
           echo $jar " is start"
       else
          echo  $jar "not running.please check it"
            exit
       fi
            
    fi
    }
    
    function stop() {
     pid4=`ps -ef|grep java |grep -v 'grep' |grep "$plat_home"|awk '{print $2}'`
    if [[ -n $pid4 ]]; then
        echo $jar "is running,we will stop it !!!!"
            kill -9 $pid4
        sleep 3
        pid3=`ps -ef|grep java |grep -v 'grep' |grep "$plat_home"|awk '{print $2}'`
        if [[ ! -n $pid3 ]]; then
            echo $jar  "is stop !!!"
        else 
            echo $jar " is running,please check it !!!"        
                    exit
        fi    
    fi
    
    
    
    }    
    
    
    
    function restart() {
        stop
        sleep 5
        start
    }
    
    
    function status() {
     pid5=`ps -ef|grep java |grep -v 'grep' |grep "$plat_home"|awk '{print $2}'`
    if [[ -n $pid5 ]]; then
        echo $jar "is start"
    else
        echo $jar "is stop"
        
    fi
    
    }
    case "$1" in
        start)
            start
            ;;
        stop)
            stop
            ;;    
        restart)
            restart
            ;;    
        status)
            status
            ;;    
        *)    
        echo $"Usage: $0 {start|stop|restart|status}"
    esac
    View Code

    项目回滚

    ./autoshell meng roll_back

     邮箱配置:

    https://blog.csdn.net/fullbug/article/details/53024562

  • 相关阅读:
    字符串编码之一:字符串语法
    PHP进阶学习系列1:字符串编码提纲
    关于技术成长的一些反思
    Yii2学习笔记002---Yii2的控制器和视图
    PHP5.3--PHP7 新特性总结
    计算机软考笔记之《数据库基础》
    计算机软考笔记之《文件结构》
    计算机软考笔记之《抽象数据类型(ADT)》
    计算机软考笔记之《数据压缩》
    计算机软考笔记之《数据结构与算法》
  • 原文地址:https://www.cnblogs.com/milanmi/p/8618048.html
Copyright © 2011-2022 走看看