除了常规的CICD打包部署,有时候不得不自己写脚本打包部署应用,这两个脚本作为记录,避免后续需要时候重复编写shell脚本,浪费时间。
打包
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 #!/bin/bash -x APP_NAME=demo CODE_BASE=$(cd "$(dirname "$0 " ) " ;pwd ) TEMP_DIR=./target TAR_NAME=${APP_NAME} .tar.gz JAR_NAME=${APP_NAME} .jar clean (){ if [[ -d $TEMP_DIR ]]; then echo "[$(color 'blue' 'INFO') ] $(color 'cyan' '1. Delete target directory') " rm -rf $TEMP_DIR fi if [[ -a $TAR_NAME ]]; then echo "[$(color 'blue' 'INFO') ] $(color 'cyan' '2. Delete tar file') " rm $TAR_NAME fi if [[ -a $JAR_NAME ]]; then echo "[$(color 'blue' 'INFO') ] $(color 'cyan' '3. Delete jar file') " rm $JAR_NAME fi } maven (){ mvn clean package -f pom.xml -P pro -DskipTests } color () { case "$1 " in "red" ) echo "\033[31;1m$2 \033[0m" ;; "yellow" ) echo "\033[33;1m$2 \033[0m" ;; "blue" ) echo "\033[34;1m$2 \033[0m" ;; "cyan" ) echo "\033[36;1m$2 \033[0m" ;; *) echo $2 ;; esac } reCompress (){ cp ./core/target/$JAR_NAME ./ files=($JAR_NAME deploy.sh application.properties TablesCreator.sql README.md) tar -czf $TAR_NAME $(echo ${files[@]} ) for i in ${files[@]} do echo "[$(color 'blue' 'INFO') ] $(color 'cyan' 'Add file') $(color 'red' ${i}) to $(color 'yellow' ${TAR_NAME}) " done rm -rf ./$JAR_NAME } main (){ cd $CODE_BASE echo "[$(color 'blue' 'INFO') ] $(color 'cyan' 'Enter build directory') $(color 'red' $(pwd) })" clean maven reCompress } main
部署
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 # !/bin/bash # 这里可替换为你自己的执行程序,其他代码无需更改 BASE_DIR=`pwd` APP_PORT=8091 JAR_NAME=demo APP_NAME=$BASE_DIR/${JAR_NAME}.jar JVM="-XX:+PrintGCDetails -Djava.net.preferIPv4Addresses -DdisableIntlRMIStatTask=true -Djava.awt.headless=true "\ " -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$BASE_DIR/output/ "\ " -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=10M -Xloggc:$BASE_DIR/output/gc.log "\ " -XX:+PrintGCApplicationStoppedTime -XX:+PrintJNIGCStalls -Xmx8g -Xms8g -XX:ConcGCThreads=4 -XX:ParallelGCThreads=4 "\ " -XX:G1ReservePercent=10 -XX:InitiatingHeapOccupancyPercent=30 -server -XX:MaxMetaspaceSize=256m -Xss256k "\ " -XX:+UseG1GC -XX:MaxGCPauseMillis=100" # SERVER_PORT=$2 # APPFILE_PATH="-Dspring.config.location=/usr/local/demo/config/application-demo1.properties" # 使用说明,用来提示输入参数 usage() { echo "Usage: sh 执行脚本.sh [start|stop|restart|status]" exit 1 } # 检查程序是否在运行 is_exist(){ pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } # 启动方法 start(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is already running. pid=${pid} ." else echo "starting ${APP_NAME}" java $JVM -jar $APP_NAME --spring.config.location=application.properties sleep 10s jps fi } # 停止方法 stop(){ is_exist if [ $? -eq "0" ]; then echo "stopping ${APP_NAME}" RESULT=$(curl --connect-timeout 20 -s http://127.0.0.1:${APP_PORT}/ok.htm?down=true) echo "prehalt called with result $RESULT and wait 8 seconds before real kill" sleep 8 kill -9 $pid sleep 3s jps else echo "${APP_NAME} is not running" fi } # 输出运行状态 status(){ is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. Pid is ${pid}" else echo "${APP_NAME} is NOT running." fi } # 重启 restart(){ stop start } # 根据输入参数,选择执行对应方法,不输入则执行使用说明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
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 beautifyPrint (){ str=("${1} " ) valid_length=${#str} if [[ ${valid_length} -gt 2 ]] && [[ ${valid_length} -lt 72 ]] then pad_num=$(echo $[72 - $valid_length ]) half=$(echo $[$pad_num / 2]) mod=$(echo $[$pad_num % 2]) if [[ ${half} -gt 2 ]] then n=$(echo $[$half - 2]) $(echo $str | xargs -i {} echo {} > /dev/null 2>&1) if [[ $? -ne 0 ]] then pad=$(printf "%0${n} d%%s%0${n} d" 0 0 | tr '0' '-' | xargs -I {} printf \"{}\" "< ${str} >" | xargs echo ) else pad=$(printf "%0${n} d%%s%0${n} d" 0 0 | tr '0' '-' | xargs -i {} printf \"{}\" "< ${str} >" | xargs echo ) fi str=${pad} elif [[ ${half} -eq 2 ]] then str='< ' $str ' >' elif [[ ${half} -eq 1 ]] then str=' ' $str ' ' fi if [[ $mod -eq 1 ]] then if [[ ${half} -eq 1 ]] then str=${str} '>' else str=${str} '-' fi fi fi echo $str }