Project deployment of server

First, create two scripts build.sh and control.sh under the root directory of the project. Build.sh is used to build the project, and control.sh is used to start and stop the project:

build.sh:

#!/bin/bash
export JAVA_HOME=/usr/local/jdk1.8.0_65
export PATH=$JAVA_HOME/bin:$PATH
mvn clean package -Dmaven.test.skip=true
ret=$?
if [ $ret -ne 0 ];then
    echo "===== maven build failure ====="
    exit $ret
else
    echo -n "===== maven build successfully! ====="
fi
rm -rf output
mkdir output
unzip privacy-api/target/privacy-api-*.zip -d output
cp control.sh output/

control.sh:

#!/bin/sh
APP_PID=privacy.portal.api
function start() {
    PID=`ps -ef|grep java|grep $APP_PID|awk '{print $2}'`
    if [ $PID ]; then
        echo "------>$PID need killed-------"
        kill -9 $PID
    fi
    nohup sh run.sh 1>>/dev/null 2>>/dev/null &
    echo "$APP_PID started ..."
}
function stop() {
    PID=`ps -ef|grep java|grep $APP_PID|awk '{print $2}'`
    if [ $PID ]; then
      echo "------>$PID need killed-------"
      kill -9 $PID
    fi
}
action=$1
case $action in
    "start" )
        start
        ;;
    "stop" )
        stop
        ;;
    * )
        echo "unknown command"
        exit 1
        ;;
esac

run.sh, which is used to set some variables of java operation:

#!/bin/sh
#Define a file that defines the environment
envPath=/data/env
if [ -f "$envPath" ]; then
    #Read the environment configuration if the file defining the environment exists
    env = `cat $envPath`
fi
if [ -z "$env" ]
then
   #No online environment by default
   env="prd"
fi
APP_ID="privacy.portal.api"
MAIN_CLASS="com.xxx.sec.privacy.PrivacyApiApplication"
SCRIPTS_DIR=`dirname "$0"`
PROJECT_DIR=`cd $SCRIPTS_DIR && pwd`
DT=`date +"%Y%m%d_%H"`
COMMAND_STR=$1
PROFILE_OPSTS="-Djava.security.egd=file:/dev/./urandom"
GC_OPTS="$GC_OPTS -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:CMSInitiatingOccupancyFraction=60 -XX:CMSTriggerRatio=70"
GC_OPTS="$GC_OPTS -Xloggc:${PROJECT_DIR}/logs/gc_${DT}.log"
GC_OPTS="$GC_OPTS -XX:+PrintGCDateStamps -XX:+PrintGCDetails -XX:+PrintHeapAtGC"
GC_OPTS="$GC_OPTS $COMMAND_STR"
GC_OPTS="$GC_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${PROJECT_DIR}/tmp/heapdump_${DT}.hprof"
START_OPTS="$START_OPTS -Djava.io.tmpdir=$PROJECT_DIR/tmp/"
START_OPTS="$START_OPTS -Duser.dir=$PROJECT_DIR"
START_OPTS="$START_OPTS  -DAPPID=${APP_ID} -DENV=${env}"
START_OPTS="$START_OPTS -Dfile.encoding=UTF-8"
START_OPTS="$START_OPTS -Dspring.profiles.active=${env}"
CLASS_PATH="$PROJECT_DIR/conf:$PROJECT_DIR/lib/*:$CLASS_PATH"

mkdir -p "$PROJECT_DIR/tmp/"
mkdir -p "$PROJECT_DIR/logs/"
java -server $GC_OPTS $START_OPTS -classpath $CLASS_PATH $MAIN_CLASS

Note: by creating a file / data/env on the server that records the current environment ID, you can read different springboot configuration files according to different environments. I created this file in the test environment and indicated that the environment is "test". According to the above script, the configuration file of the test should be loaded, but the configuration of "prd" is actually loaded. When debugging the script, I found that env=`cat $envPath 'did not get the result. There are more spaces on both sides of the reason and so on. Just remove the spaces: env=`cat $envPath'.

Deployment script (build, download code from git, and start the project):

#!/bin/bash
PROJ_DIR=/home/xxx
project_name="privacy-portal-api"
git_branch="master"
if [ $# = 1 ] ; then
git_branch=$1
fi
echo 'git_branch:'$git_branch
git_addr="-b $git_branch git@git.xxx.com:sec/$project_name.git"
echo 'projetc dir:'$PROJ_DIR;

if [[ -d $PROJ_DIR/ROOT ]]; then
  echo "ROOT exist";
else
  mkdir $PROJ_DIR/ROOT
fi

cd $PROJ_DIR/ROOT
pwd

if [[ -d $project_name ]]; then
  rm -rf $project_name;
fi
echo "-------clone $project_name-------"
git clone $git_addr
echo "@@@@ build $project_name @@@@";
cd $project_name;
pwd;

sh build.sh
echo "@@@@ build $project_name end @@@@";
echo "@@@@ copy to $project_name /home/xiaoju @@@@"
rm -rf $PROJ_DIR/$project_name
cp -rp $PROJ_DIR/ROOT/$project_name/output $PROJ_DIR/$project_name
echo "@@@@ copy end @@@@"
cd /home/xiaoju/$project_name/
echo "::::: stop $project_name :::::"
sh /home/xiaoju/$project_name/control.sh stop
echo "::::: start $project_name :::::"
sh /home/xiaoju/$project_name/control.sh start
cd $PROJ_DIR/ROOT

Tags: Programming git Java Maven encoding

Posted on Tue, 24 Mar 2020 07:41:01 -0700 by 7awaka