jekins vue 发布脚本 shell

#!/bin/bash

# Description:对应jenkins上的shell脚本
# Author:yangdejian
# CompeleteTime:2021-03-10

start_time=$(date "+%Y-%m-%d %H:%M:%S")
script_root="/root/consignmentweb"
serv_path="/home/jsxt-admin-test"
project_name="xxx系统(vue)"
subscriber="oth4257RpdizMwyWEPpImC1nB8EA" # 暂时只通知给我

# 处理jenkins配置的输入参数
# 处理测试环境
env="test"
if [[ "${in_env}" = "生产环境" ]]; then
    env="prod"
fi

# >>>>>>>>>>>>>>>>>>>>>>>> 函数块 start >>>>>>>>>>>>>>>>>>>>>>>>>
exit_with_error(){
    echo "报警给订阅者"

    first=${project_name}"发布异常"
    keyword1="开始于["${start_time}"]的Jenkins自动构建任务中断,详情参见jenkin构建日志"
    keyword2="中断原因:"$1
    remark="具体异常原因需要查看构建日志"

    # send_msg "$first" "$keyword1" "$keyword2" "$remark" "#8B008B"(不想接收发布结果了)
    exit 1
}

exit_succ(){
    echo "构建成功,通知订阅者"

    local end_time=$(date "+%Y-%m-%d %H:%M:%S")
    local start_seconds=$(date --date="$start_time" +%s)
    local end_seconds=$(date --date="$end_time" +%s)
    local consume=$((end_seconds-start_seconds))

    local first=${project_name}"自动发布"
    local keyword1="Jenkins自动发布参数如下\ngit分支:"${in_branch}"\n环境:"${in_env}"\n开始于:"${start_time}"\n总耗时:"${consume}"秒"
    local keyword2="发布成功"
    local remark="访问地址:http://jsxtest.100bm.cn/login"

    # send_msg "$first" "$keyword1" "$keyword2" "$remark" "#3AB54A"(不想接收发布结果了)
    exit 0
}

send_msg(){
    local first=$1
    local keyword1=$2
    local keyword2=$3
    local keyword3=$(date "+%Y-%m-%d %H:%M:%S")
    local remark=$4
    local color=$5
    curl -X "POST" "http://xxx/wechat/v1/common/direct/template/msg" \
	     -H "Content-Type: application/json" \
	     -d@- \
<<EOF
    {
       "appId":"wx90817bbaf76cb773",
       "templateId":"ByCWa12weOi4W3iqGohhKdxNHtBxW8OYjJm5fBF_pmw",
       "toUsers":"${subscriber}",
       "data":{
         "first":{"color":"${color}","value":"${first}"},
         "keyword1":{"color":"${color}","value":"$2"},
         "keyword2":{"color":"${color}","value":"${keyword2}"},
         "keyword3":{"color":"${color}","value":"${keyword3}"},
         "remark":{"color":"${color}","value":"${remark}"}}
    }
EOF
	echo -e "\n"
}
# <<<<<<<<<<<<<<<<<<<<<<<< 函数块 end   <<<<<<<<<<<<<<<<<<<<<<<<<

# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>>>>>>>>> 主流程 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

echo "--------------------- 开始[${project_name}]的远程构建 ---------------------"
# jenkins编译项目前,启动环境变量(~/.bash_profile  ~/.bashrc  /etc/profile)
# 不继承linux环境变量,会导致在执行shell脚本,提示命令找不到
source /etc/profile

echo "1. 拉取最新的指定分支的代码..."
cd ${script_root} && git pull && git checkout ${in_branch} && git pull
if [[ "$?" != "0" ]]; then
    exit_with_error "git拉取代码异常!"
fi

echo "2. 更新依赖库..."
yarn install
if [[ "$?" != "0" ]]; then
    exit_with_error "依赖库更新异常!"
fi

echo "3. 开始构建..."
yarn build:"${env}"
if [[ "$?" != "0" ]]; then
    exit_with_error "构建异常!"
fi

echo "4. 远程备份原文件..."
ssh root@192.168.x.x <<EOF
    echo "远程后的当前机器ip:"
    ifconfig eth0 | grep -w "inet"
    if [ -d "$serv_path" ]; then
        echo "备份目录[${serv_path}]到[/root/jsxt-admin-test-bak]"
        mv ${serv_path} /root/jsxt-admin-test-bak/$(date "+%Y%m%d_%H%M%S")
        echo "备份完成"
    else
        echo "目录[${serv_path}]不存在,无需备份"
    fi
    exit
EOF

echo "5. 发布最新的静态文件到目标服务器..."
ifconfig eth0 | grep -w "inet" | awk '{ print "之后命令执行所在的IP为:"$2}'
scp -r $script_root"/dist" "root@192.168.x.x:"${serv_path}
if [[ "$?" != "0" ]]; then
    exit_with_error "发布异常!"
fi

echo "6. 构建完成!"
exit_succ

 

 配置的自定义参数:

 

 

posted @ 2022-02-17 15:10  流失的痕迹  阅读(81)  评论(0编辑  收藏  举报