Jenkins部署到远程服务器并备份包文件

  • Post author:
  • Post category:其他


前言

Jenkins自动构建后发送到远程服务器,执行shell文件,和shell脚本。shell中实现程序自动部署和安装包版本备份。

前提条件

一台远程服务器比如10.10.10.100,已经设置好SSH免密访问。一台装有Jenkins项目服务器。

配置操作

sh文件内容:

echo  'start.........................'
project_path=$(cd `dirname $0`; pwd)
project_name="${project_path##*/}"
echo $project_path
echo $project_name

cd $project_path/aoc/

git pull
git status

cd ..

echo 'git pull end........'


DATE=$(date +%Y%m%d_%H%M)

export JAVA_HOME PATH CLASSPATH

JAVA_HOME=/usr/local/java/jdk1.8.0_191

PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH

DIR=/jenkins-in-release/server/aoc-master/aoc-home/target

JARFILE=AOC-PLATFORM.jar

if [ ! -d $DIR/backup ];then

   mkdir -p $DIR/backup

fi

cd $DIR

ps -ef | grep $JARFILE | grep -v grep | awk '{print $2}' | xargs kill -9

pwd
cp -f ./$JARFILE ./backup/$JARFILE$DATE${BUILD_NUMBER}

BUILD_ID=dontKillMe nohup java -jar $JARFILE > out.log &

if [ $? = 0 ];then

        sleep 30

        tail -n 50 out.log

fi

mv -f $JARFILE 

cd backup/

ls -lt|awk 'NR>5{print $NF}'|xargs rm -rf

echo 'END-----------'

shell 命令:

#!/bin/sh 
tomcat_path=/jenkins-in-release/server/aoc-master/aoc-home/target/backup
project_name=$WORKSPACE/server/aoc-master/aoc-home/target/AOC-PLATFORM.jar
echo $tomcat_path
echo $project_name

if [ -f $project_name ] ;then 
	echo -- 正在部署$project_name 
	if [ -f $tomcat_path/AOC-PLATFORM.jar ] ;then
      #rm $tomcat_path/server/aoc-master/aoc-home/target/AOC-PLATFORM.jar
      #rm -rf $tomcat_path/server/aoc-master/aoc-home/target/$JOB_NAME 
      echo -- 1正在拷贝$project_name 
	  cp $project_name $tomcat_path/AOC-PLATFORM-$BUILD_NUMBER.jar
    else 
        echo -- 2正在拷贝$project_name 
        scp $project_name root@10.10.10.100:$tomcat_path/AOC-PLATFORM-$BUILD_NUMBER.jar
        fi 
else 
	echo $BUILD_ID - 未找到$project_name 
fi

说明

1.远程拷贝需求SCP命令 cp不得行

2.拷贝文件后以构建号码BUILD_NUMBER命名备份

3.可以创建测试和开发自动部署到任意服务器,并备份生成包



版权声明:本文为xuqiang918原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。