1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
| #!/bin/sh
declare -A terminal terminal["pc"]="PC端" terminal["h5"]="h5端"
declare -A jar_or_front jar_or_front["jar"]="后端服务" jar_or_front["front"]="前端页面"
cmd_log() { echo "脚本:$1"; echo "终端:$2"; echo "环境:$3"; echo "前后:$4";
echo -n "回滚:"; if [[ $5 = "rb" ]]; then echo "回滚发布"; else echo "不回滚发布"; fi echo -n "依赖:"; if [[ $5 = "i" || $6 = "i" ]]; then echo "更新前端依赖包"; else echo "不更新前端依赖包"; fi }
start_log() { project_path=/root/xxx-$1-$2/ current_port=`cat ${project_path}run-jars.sh | grep 'port=' | cut -d '=' -f 2` log_file=${project_path}${current_port}/logs/xxx-$1-$2.log if [ ! -e "$log_file" ];then echo "[ERROR]日志文件不存在!!!请检查是否选错了发布端! log_file="$log_file exit 1 fi echo log_file:${log_file} echo .jar starting...
search_string="JVM running" start_time=$(date +%s) timeout=180
while true; do line_number=`grep -n "$search_string" $log_file | awk -F ':' '{print $1}'` if [ -n "$line_number" ]; then line_number=$((line_number + 10)) head -n $line_number $log_file break fi current_time=$(date +%s) elapsed_time=$((current_time - start_time)) if [ $elapsed_time -lt $timeout ]; then sleep 5 else echo "[ERROR]Timeout. Could not find the string within 3 minutes. Project start failed!!!" exit 1 fi done }
release_jar() { cd java-project/ echo ./run-$2-$1.sh ./run-$2-$1.sh
if [[ $1 = "pc" || $1 = "h5" ]]; then echo cd ../xxx-$1-$2/ cd ../xxx-$1-$2/ else echo "终端名称有误!" fi if [[ $3 = "rb" ]]; then echo ./run-jars-rb.sh ./run-jars-rb.sh else echo ./run-jars.sh ./run-jars.sh fi
start_log $1 $2 }
static_check() { project_path=/root/xxx-$1-$2/ current_port=`cat ${project_path}run-jars.sh | grep 'port=' | cut -d '=' -f 2` dist_dir=${project_path}${current_port}/front/$1/ echo dist_dir:${dist_dir}
if [ ! -d "$dist_dir" ]; then echo "[ERROR]Static dist not exists. Please check!!!" exit 1 fi }
release_front() { cd web-project/ echo ./run-$1-$2.sh if [[ $3 = "i" || $4 = "i" ]]; then ./run-$1-$2.sh i else ./run-$1-$2.sh fi
if [[ $1 = "pc" || $1 = "h5" ]]; then echo cd ../xxx-$1-$2/ cd ../xxx-$1-$2/ else echo "终端名称有误!" fi if [[ $3 = "rb" ]]; then echo ./run-$1-rb.sh ./run-$1-rb.sh else echo ./run-$1.sh ./run-$1.sh fi
static_check $1 $2 }
push_webhook_msg() { echo "push webhook msg."
env_val=$2 current=$1 zd=${terminal[$1]} gitLog=`cat /tmp/.gitLogTop3-${current}-${env_val}-$3` nr=${jar_or_front[$3]}"\\n"$gitLog ip=`curl -s inet-ip.info`
echo env_val=$env_val, current=$current, zd=$zd, ip=$ip, nr=$nr if [[ $env_val = "test" || $env_val = "pre" ]]; then CURL_URL='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx' else CURL_URL='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx' fi
CURL_DATA=$(printf '{"msgtype": "markdown", "markdown": {"content": "<font color=\\\"warning\\\">xxx-%s-%s</font>发布完成,请相关同事注意。\\n>终端:<font color=\\\"comment\\\">%s</font>\\n>环境:<font color=\\\"info\\\">%s</font>\\n>主机:<font color=\\\"comment\\\">%s</font>\\n>内容:<font color=\\\"comment\\\">%s</font>"}}' "$current" "$env_val" "$zd" "$env_val" "$ip" "$nr")
CURL_CMD="curl \"$CURL_URL\" -H \"Content-Type: application/json\" -d '$CURL_DATA'" echo "CURL_CMD="$CURL_CMD
CURL_RES=$(eval $CURL_CMD) echo "CURL_RES="$CURL_RES }
if [[ ($1 = "pc" || $1 = "h5") && ($2 = "test" || $2 = "pre" || $2 = "prod") ]]; then cmd_log $0 $1 $2 $3 $4 $5 if [[ $3 = "jar" ]]; then release_$3 $1 $2 $4 elif [[ $3 = "front" ]]; then release_$3 $1 $2 $4 $5 else echo "前后端名称有误!" fi
sleep 10 push_webhook_msg $1 $2 $3
echo echo "Success!" cd ~ else echo "命令格式: ./release.sh 终端名 环境名 前后端 [回滚参]" echo "命令示例:./release.sh pc test jar rb i" echo "参数解释:" echo "- 终端名:pc-${terminal['pc']}, h5-${terminal['h5']}" echo "- 环境名:test-测试环境, pre-预发布环境(内部测试), prod-生产环境" echo "- 前后端:jar-后端, front-前端" echo "- 回滚参:可选参数,rb-回滚并重启" echo "- 更新参:可选参数,i-即npm i更新安装前端依赖包" fi
|