惜风不起、唯有努力!
jenkins传统cicd测试脚本

jenkins传统cicd测试脚本

#!/bin/sh 
#CI

WOKE=/home/jenkins/workspace
FTP_SERVER=192.168.136.153
#{git拉取代码}
cd $WOKE/$JOB_NAME/$PROJECT_NAME && printf '******开始打包******\n'
mvn clean package ||  mvn clean package
cd target && scp *.jar root@${FTP_SERVER}:/home/file_server/java && printf "******jar包已发往FTP******\n"


################################################################
#!/bin/sh 
#CD

SERVICE_IP=192.168.136.144
SERVICE_PORT=8080
FTP_SERVER=192.168.136.153

#ssh方式
printf "******开始执行部署脚本******\n"
ssh root@${SERVICE_IP} > /dev/null 2>&1 << emo
bash /opt/deploy.sh
sleep 1s
exit
emo
sleep 1s
curl http://${SERVICE_IP}:${SERVICE_PORT}/hello
tcping ${SERVICE_IP} ${SERVICE_PORT}
#------------------------------------------------------------------------------
#ansible方式
printf "******开始执行部署脚本******\n"
ansible A -m shell -a "bash /opt/deploy.sh"
sleep 1s
curl http://${SERVICE_IP}:${SERVICE_PORT}/hello
tcping ${SERVICE_IP} ${SERVICE_PORT}

#############################################
#!/bin/bash
#deploy service
#搭配Jenkins

FTP_SERVER=192.168.136.153
#SERVICE_NAME为外部传参服务事项名,可根据情况而定
SERVICE_NAME=$1
#服务事项版本号,可根据情况而定
VERSION=$2
PID=$(ps -ef|grep -v "grep"|grep -v "$0"|grep -i "$SERVICE_NAME"|awk '{print $2}')
BAK_DATE=$(date +%Y%m%d_%H%M)

function KillService(){
    #echo "准备kill老服务进程..."
    if [ "$PID" != "" ];then
        kill $PID || kill -9 $PID
        echo "$(date) 进程已杀掉!!!" >> ./deploy.log
    else
        echo "$(date) 没有找到进程,进程不存在!!!" >> ./deploy.log
    fi
}

function BakUpdate(){
    #echo "准备备份老服务包..."
    if [ ! -d /project/${SERVICE_NAME} ];then
        mkdir -p /project/$SERVICE_NAME
    fi

    cd /project/$SERVICE_NAME
    if [ ! -d ./jar_bak ];then
        mkdir ./jar_bak
    fi

    local service_name=$(ls |grep -i "${SERVICE_NAME}")
    if [ ! -f $service_name ];then
        echo "$(date) not $service_name" >> ./deploy.log
    else
        mv $service_name ${service_name}.$BAK_DATE && mv ${service_name}.$BAK_DATE ./jar_bak && cd ./jar_bak
    fi

    local back_num=$(ls -rt | wc -l)
    if [ ${back_num} -gt 10  ];then
        let n=${back_num}-10
        if [ ${n} -gt 0 ];then
            ls -rt | head -n $n | xargs rm -rf
        fi
    fi
    cd ../
}

function deploy(){
    #echo "开始部署服务..."
    cd /project/$SERVICE_NAME
    wget http://${FTP_SERVER}:7010/java/${SERVICE_NAME}-${1}-SNAPSHOT.jar
    nohup java -jar ./${SERVICE_NAME}-${1}-SNAPSHOT.jar >& ./${SERVICE_NAME}.log &
    echo "$(date) deploy ok !!!" >> ./deploy.log
}

function main(){
    KillService && BakUpdate && deploy $VERSION
}
main

发表回复

您的电子邮箱地址不会被公开。