一、 新建jenkins任务

1.1、新增任务

1.2、配置svn源码路径、账号

1.3、配置构建环境

1.4、配置构建脚本

    cd $WORKSPACE/build; # 切换到工具目录
    sh jenkins.8081.deploy.sh; # 构建程序

    error_count=`grep "ERROR" ant.8081.log | wc -l`;
    if [ ${error_count} -eq 0 ]; then
        OLD_BUILD_ID=$BUILD_ID;
        echo $OLD_BUILD_ID;
        export BUILD_ID=dontKillMe;

        echo "sh /opt/jenkins/scp.220.staging.sh" | at now+1min; # 自动部署程序

        export BUILD_ID=$OLD_BUILD_ID;
        echo $BUILD_ID;
    else
        echo "Package has Error."
        set -e #注意,这句最重要,一定要先设置这个,非常感谢 诺亚舟 的提示
        exit 1  #然后再退出,jenkins就会报红显示构建失败
    fi
  • jenkins打包工具相关配置详见: [工具配置]

  • scp.220.staging.sh文件内容:

    #!/bin/bash
    
    j_home=/var/lib/jenkins/workspace/iform-staging
    s_home=/opt/jenkins/iform-staging
    
    ssh 192.168.3.220 rm -rf ${s_home}/iform-basis-gateway-exclude ${s_home}/iform-oauth-server2-exclude ${s_home}/iform-provider-platform-exclude ${s_home}/iform-provider-business-exclude;
    scp -r ${j_home}/3-iform-v3.1-dev/iform-basis-root/modules/basis-gateway/target/iform-basis-gateway-exclude root@192.168.3.220:${s_home}/;
    scp -r ${j_home}/3-iform-v3.1-dev/iform-oauth-root/modules/oauth-server2/target/iform-oauth-server2-exclude root@192.168.3.220:${s_home}/;
    scp -r ${j_home}/3-iform-v3.1-dev/iform-provider-root/modules/provider-platform/target/iform-provider-platform-exclude root@192.168.3.220:${s_home}/;
    scp -r ${j_home}/3-iform-v3.1-dev/iform-provider-root/modules/provider-business/target/iform-provider-business-exclude root@192.168.3.220:${s_home}/;
    ssh -f -n 192.168.3.220 sh ${s_home}/reset.sh e;
    • 主机间设置了免密登陆操作,相关设置可自行百度。
  • 192.168.3.220机器上的reset.sh文件内容:

      #!/bin/bash
      script_home=$(cd `dirname $0`; pwd)
      iform_home=/home/v3/iform-staging-v3 # 真实的部署目录
      script_username=v3 # 真的的部署用户名
    
      # stop all services. 停止所有服务
      echo "stop all service."
      su ${script_username} -l -c "sh ${iform_home}/stop.sh;"
    
      #echo "sleeping 3s."
      #sleep 3;
    
      # remove all old service files. 删除所有老的文件
      echo "remove all old service files."
      rm -rf ${iform_home}/gateway-test ${iform_home}/oauth2server-test ${iform_home}/platform-test ${iform_home}/business-test;
    
      #echo "sleeping 3s."
      #sleep 3;
    
      # copy all new service files.  复制所有新的文件
      echo "copy all new service files."
      cp -rf ${script_home}/iform-basis-gateway-exclude ${iform_home}/gateway-test;
      cp -rf ${script_home}/iform-oauth-server2-exclude ${iform_home}/oauth2server-test;
      cp -rf ${script_home}/iform-provider-platform-exclude ${iform_home}/platform-test;
      cp -rf ${script_home}/iform-provider-business-exclude ${iform_home}/business-test;
    
      #echo "sleeping 3s."
      #sleep 3;
    
      # chown all new service fles.  启动所有服务
      echo "chown all new service fles."
      chown -R ${script_username}:${script_username} ${iform_home}/gateway-test;
      chown -R ${script_username}:${script_username} ${iform_home}/oauth2server-test;
      chown -R ${script_username}:${script_username} ${iform_home}/platform-test;
      chown -R ${script_username}:${script_username} ${iform_home}/business-test;
    
      #echo "sleeping 3s."
      #sleep 3;
    
      # start all services.  启动所有服务
      echo "start all services. "
      su ${script_username} -l -c "cd ${iform_home};sh start.sh;"
    
      if [ -n "$1" ] ;then
          echo "exit...";
          exit;
      fi

FAQ

1、Jenkins重启后自动杀掉衍生进程(子进程)问题

https://blog.csdn.net/longfeng1234/article/details/141635007

作者:hugh  创建时间:2024-01-03 10:07
最后编辑:hugh  更新时间:2024-11-15 11:25