基于 git、maven,通过 shell 脚本打包
#!/bin/bash
#初始化各类变量
HOME_DIR="/Users/zhaobo.lin/Documents/work/deploy"
# 参数处理
BRANCH_NAME=$1
GIT_URL=$2
TEMP_WAR=${GIT_URL##*/}
PROJECT_NAME=${TEMP_WAR%.*}
# 目录定义
PROJECT_BUILD_ROOT_DIR=$PROJECT_NAME"-build-root"
PROJECT_BUILD_GIT_DIR=$PROJECT_NAME
PROJECT_BUILD_LOG_DIR="log"
PROJECT_BUILD_WAR_DIR="war"
PROJECT_BUILD_INI_DIR="ini"
# 路径定义
PROJECT_BUILD_ROOT_PATH=$HOME_DIR/$PROJECT_BUILD_ROOT_DIR
PROJECT_BUILD_GIT_PATH=$HOME_DIR/$PROJECT_BUILD_ROOT_DIR/$PROJECT_BUILD_GIT_DIR
PROJECT_BUILD_LOG_PATH=$HOME_DIR/$PROJECT_BUILD_ROOT_DIR/$PROJECT_BUILD_LOG_DIR
PROJECT_BUILD_WAR_PATH=$HOME_DIR/$PROJECT_BUILD_ROOT_DIR/$PROJECT_BUILD_WAR_DIR
PROJECT_BUILD_INI_PATH=$HOME_DIR/$PROJECT_BUILD_ROOT_DIR/$PROJECT_BUILD_INI_DIR
# 打包日志路径
LOG_FILE_PATH=$PROJECT_BUILD_LOG_PATH/$PROJECT_NAME"-"$BRANCH_NAME"-build.log"
BUILD_VERSION_SERIAL_NUMBER_FILE=$PROJECT_BUILD_INI_PATH/$BRANCH_NAME".serial"
# 创建项目打包根路径
if [ ! -d $PROJECT_BUILD_ROOT_PATH ];
then
mkdir $PROJECT_BUILD_ROOT_DIR
fi
if [ ! -d $PROJECT_BUILD_GIT_PATH ];
then
git config --global credential.helper store
if [ $# -ne 2 ]; then
echo "usage:sudo ./auto_build.sh [branch] [gitUrl]"
exit 0
fi
echo "克隆项目"$PROJECT_NAME"代码,git 地址:"$GIT_URL
cd $PROJECT_BUILD_ROOT_PATH
git clone -b $BRANCH_NAME $GIT_URL $PROJECT_BUILD_GIT_DIR
else
echo "项目"$PROJECT_NAME"代码目录已经存在,拉取更新指定版本代码……"
cd $PROJECT_BUILD_GIT_PATH
# TODO 切换 branch,并更新代码
git checkout -b $BRANCH_NAME origin/$BRANCH_NAME
git pull
fi
echo "创建项目打包日志目录……"
cd $PROJECT_BUILD_ROOT_PATH
if [ -d $PROJECT_BUILD_LOG_PATH ];
then
rm -rf $PROJECT_BUILD_LOG_DIR
fi
mkdir $PROJECT_BUILD_LOG_DIR
cd $PROJECT_BUILD_GIT_PATH
mvn clean install -Dmaven.test.skip=true | tee -a $LOG_FILE_PATH
if [ `grep -c 'BUILD SUCCESS' $LOG_FILE_PATH` -gt '0' ];
then
echo "打包成功!"
else
echo "打包失败!退出,请查看日志,日志路径"$LOG_FILE_PATH
exit 0
fi
cd $PROJECT_BUILD_ROOT_PATH
# 创建项目打包配置文件目录
if [ ! -d $PROJECT_BUILD_INI_PATH ];
then
mkdir $PROJECT_BUILD_INI_DIR
fi
echo "读取指定版本的第四位版本号。"
# 创建项目打包版本第四位序列号文件
if [ ! -f $BUILD_VERSION_SERIAL_NUMBER_FILE ];
then
touch $BUILD_VERSION_SERIAL_NUMBER_FILE
fi
LAST_SERIAL_NUMBER=`tail -n 1 $BUILD_VERSION_SERIAL_NUMBER_FILE`
if [ ! $LAST_SERIAL_NUMBER ];
then
LAST_SERIAL_NUMBER="0"
else
if [[ $i =~ ^[0-9]*$ ]];
then
LAST_SERIAL_NUMBER=`expr $LAST_SERIAL_NUMBER + 1`
else
rm -f $BUILD_VERSION_SERIAL_NUMBER_FILE
LAST_SERIAL_NUMBER="0"
fi
fi
echo $LAST_SERIAL_NUMBER >> $BUILD_VERSION_SERIAL_NUMBER_FILE
# 创建项目包存储目录
if [ ! -d $PROJECT_BUILD_WAR_PATH ];
then
mkdir $PROJECT_BUILD_WAR_DIR
fi
cd $PROJECT_BUILD_WAR_PATH
VERSION_SERIAL_WAR_DIR=$BRANCH_NAME"."$LAST_SERIAL_NUMBER
if [ -d $PROJECT_BUILD_WAR_PATH/$VERSION_SERIAL_WAR_DIR ];
then
rm -rf $VERSION_SERIAL_WAR_DIR
fi
mkdir $VERSION_SERIAL_WAR_DIR
echo "拷贝包到项目包存储目录."
cp $PROJECT_BUILD_GIT_PATH/"target/"$PROJECT_NAME".war" $PROJECT_BUILD_WAR_PATH/$VERSION_SERIAL_WAR_DIR