jar包部署
通过 java -jar 方式运行 jar包.
创建项目
创建自由风格项目
参数化构建
源码管理
构建
jar包构建的代码
主义脚本中 jar包名字 是构建之后的名字
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[root@node2 jenkins]# cat deploy_java_jar.sh #!/bin/sh # 工作目录 CODE_DIR=$WORKSPACE # 压缩包存放目录 TAR_DIR="/data/jenkins/${JOB_NAME}/" # 文件夹及文件名 FILE_DIR="${TAG_VERSION}_${JOB_BASE_NAME}" # 压缩文件全称 PACKAGE="${FILE_DIR}.tar.gz" # jar 包名字 JAR_NAME='hello-world.jar' # 构建命令 MVN_ORDER='/usr/local/maven/bin/mvn clean package' # 远程部署服务器地址 REMOTE_IP='172.16.0.64 172.16.0.63' # 远程部署服务器账号 REMOTE_USER=root # 远程端口 REMOTE_PORT=22 # 远程部署目录 REMOTE_DIR='/data/' if [ ! -d ${TAR_DIR} ];then mkdir ${TAR_DIR} -p echo "创建文件夹: ${TAR_DIR}" fi build_mvn() { echo "--------------------------[ mvn clean package ]---------------------------" cd ${CODE_DIR} ${MVN_ORDER} if [ $? -eq 0 ]; then echo "构建成功: ${CODE_DIR}" else echo "构建失败: ${CODE_DIR}" exit 1 fi } cmp_package() { echo "--------------------------[ compress jar ]---------------------------" cd ${CODE_DIR}/target/ && tar zcf ${TAR_DIR}${PACKAGE} ${JAR_NAME} if [ $? -eq 0 ]; then echo "打包成功: ${TAR_DIR}${PACKAGE}" else echo "打包失败: ${TAR_DIR}${PACKAGE}" exit 1 fi } scp_package() { echo "--------------------------[ remote rsync jar ]---------------------------" for IP in ${REMOTE_IP};do rsync -arq --port=${REMOTE_PORT} ${TAR_DIR}${PACKAGE} ${REMOTE_USER}@${IP}:${REMOTE_DIR} if [ $? -eq 0 ]; then echo "远程拷贝成功: $IP - ${TAR_DIR}${PACKAGE}" else echo "远程拷贝失败: $IP - ${TAR_DIR}${PACKAGE}" exit 1 fi done } remote_untar() { echo "--------------------------[ remote untar jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "mkdir ${REMOTE_DIR}${FILE_DIR}" ssh $IP "cd $REMOTE_DIR && tar xf ${PACKAGE} -C ${REMOTE_DIR}${FILE_DIR}" ssh $IP "cd $REMOTE_DIR && rm -rf ${PACKAGE}" if [ $? -eq 0 ]; then echo "解压成功: $IP - ${PACKAGE}" else echo "解压失败: $IP - ${PACKAGE}" exit 1 fi done } remote_ln() { # 确保软件连目录原本就是软连接目录 # ssh $REMOTE_IP "cd $REMOTE_DIR && ln -snf ${REMOTE_DIR}/${TAR_NAME} html" echo "--------------------------[ remote ln jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "cd $REMOTE_DIR && rm -rf ${JAR_NAME} && ln -s ${REMOTE_DIR}${FILE_DIR}/${JAR_NAME} ${JAR_NAME}" if [ $? -eq 0 ]; then echo "软连接成功: $IP - ${JAR_NAME}" else echo "软连接失败: $IP - ${JAR_NAME}" exit 1 fi done } run_jar() { echo "--------------------------[ remote start jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP " nohup java -jar ${REMOTE_DIR}$JAR_NAME >> ${REMOTE_DIR}${JAR_NAME}.out 2>&1 &" if [ $? -eq 0 ]; then echo "启动成功: $IP - ${JAR_NAME}" else echo "启动失败: $IP - {JAR_NAME}" exit 1 fi done } stop_jar() { echo "--------------------------[ remote stop jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "ps -ef | grep ${JAR_NAME} | grep -v grep" if [ $? -eq 0 ]; then ssh $IP "ps -ef | grep ${JAR_NAME} | grep -v grep | awk '{print $2}' | xargs kill -s 9" if [ $? -eq 0 ]; then echo "停止成功: $IP - ${JAR_NAME}" else echo "停止失败: $IP - ${JAR_NAME}" exit 1 fi else echo "越过${IP}服务器: 没有启动${JAR_NAME}进程!" continue fi done } main() { build_mvn; cmp_package; scp_package; remote_untar; stop_jar; remote_ln; run_jar; } case $pattern in deploy) # if [ "$GIT_COMMIT" == "$GIT_PREVIOUS_SUCCESSFUL_COMMIT" ];then echo "构建失败该 ${TAG_VERSION} 版本号已部署" # exit 1 # else echo "---------- 开始发布 版本:${TAG_VERSION} --------" main echo "---------- 完成发布 版本:${TAG_VERSION} --------" # fi ;; rollback) echo "---------- 开始回滚 版本:${TAG_VERSION} --------" stop_jar; remote_ln; run_jar; echo "---------- 完成回滚 版本:${TAG_VERSION} --------" ;; *) echo "Usage: $0 {deploy | rollback }" exit 1 ;; esac
war包构建
war包基于tomcat jenkins 配置一样,区别代码
脚本代码
[root@node2 jenkins]# cat deploy_java_war.sh #!/bin/sh # 工作目录 CODE_DIR=$WORKSPACE # 压缩包存放目录 TAR_DIR="/data/jenkins/${JOB_NAME}/" # 文件名字 FILE_DIR="${TAR_VERSION}_${JOB_BASE_NAME}" # 压缩文件全称 TAR_NAME="${FILE_DIR}.tar.gz" # jar 包名字 JAR_NAME='hello-world-war-1.0.0.war' # 构建命令 MVN_ORDER='/usr/local/maven/bin/mvn clean package' # 远程部署服务器地址 REMOTE_IP='172.16.0.64 ' # 远程部署服务器账号 REMOTE_USER=root # 远程端口 REMOTE_PORT=22 # 远程部署目录 REMOTE_DIR='/usr/local/tomcat/webapps/' if [ ! -d ${TAR_DIR} ];then mkdir ${TAR_DIR} -p echo "创建文件夹: ${TAR_DIR}" fi build_mvn() { echo "--------------------------[ mvn clean package ]---------------------------" cd ${CODE_DIR} ${MVN_ORDER} if [ $? -eq 0 ]; then echo "构建成功: ${CODE_DIR}" else echo "构建失败: $ls {CODE_DIR}" exit 1 fi } cmp_package() { echo "--------------------------[ compress jar ]---------------------------" cd ${CODE_DIR}/target/ && tar zcf ${TAR_DIR}${TAR_NAME} ${JAR_NAME} if [ $? -eq 0 ]; then echo "打包成功: ${TAR_DIR}${TAR_NAME}" else echo "打包失败: ${TAR_DIR}${TAR_NAME}" exit 1 fi } scp_package() { echo "--------------------------[ remote rsync jar ]---------------------------" for IP in ${REMOTE_IP};do rsync -arq --port=${REMOTE_PORT} ${TAR_DIR}${TAR_NAME} ${REMOTE_USER}@${IP}:${REMOTE_DIR} if [ $? -eq 0 ]; then echo "远程拷贝成功: $IP - ${TAR_DIR}${TAR_NAME}" else echo "远程拷贝失败: $IP - ${TAR_DIR}${TAR_NAME}" exit 1 fi done } remote_untar() { echo "--------------------------[ remote untar jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "mkdir ${REMOTE_DIR}${FILE_DIR}" ssh $IP "cd $REMOTE_DIR && tar xf ${TAR_NAME} -C ${REMOTE_DIR}${FILE_DIR}" ssh $IP "cd $REMOTE_DIR && rm -rf ${TAR_NAME}" ssh $IP "cd $REMOTE_DIR${FILE_DIR} && unzip -q ${JAR_NAME}" ssh $IP "cd $REMOTE_DIR${FILE_DIR} && rm -rf ${JAR_NAME}" if [ $? -eq 0 ]; then echo "解压成功: $IP - ${TAR_NAME}" else echo "解压失败: $IP - ${TAR_NAME}" exit 1 fi done } remote_ln() { # 确保软件连目录原本就是软连接目录 # ssh $REMOTE_IP "cd $REMOTE_DIR && ln -snf ${REMOTE_DIR}/${TAR_NAME} html" echo "--------------------------[ remote ln jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "cd $REMOTE_DIR && rm -rf ROOT && ln -s ${REMOTE_DIR}${FILE_DIR} ROOT" if [ $? -eq 0 ]; then echo "软连接成功: $IP - ${JAR_NAME}" else echo "软连接失败: $IP - ${JAR_NAME}" exit 1 fi done } run_jar() { echo "--------------------------[ remote start jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "/usr/local/tomcat/bin/startup.sh" if [ $? -eq 0 ]; then echo "启动成功: $IP - ${JAR_NAME}" else echo "启动失败: $IP - {JAR_NAME}" exit 1 fi done } stop_jar() { echo "--------------------------[ remote stop jar ]---------------------------" for IP in ${REMOTE_IP};do ssh $IP "ps -ef | grep tomcat | grep -v grep" &>/dev/null if [ $? -eq 0 ]; then ssh $IP "/usr/local/tomcat/bin/shutdown.sh" if [ $? -eq 0 ]; then echo "停止成功: $IP - ${JAR_NAME}" else echo "停止失败: $IP - ${JAR_NAME}" exit 1 fi else echo "越过${IP}服务器: 没有启动tomcat!" continue fi done } main() { build_mvn; cmp_package; scp_package; remote_untar; stop_jar; remote_ln; run_jar; } case $pattern in deploy) if [ "$GIT_COMMIT" == "$GIT_PREVIOUS_SUCCESSFUL_COMMIT" ];then echo "构建失败该 ${TAR_VERSION} 版本号已部署" exit 1 else echo "---------- 开始发布 版本:${TAR_VERSION} --------" main echo "---------- 完成发布 版本:${TAR_VERSION} --------" fi ;; rollback) echo "---------- 开始回滚 版本:${TAR_VERSION} --------" stop_jar; remote_ln; run_jar; echo "---------- 完成回滚 版本:${TAR_VERSION} --------" ;; *) echo "Usage: $0 {deploy | rollback }" exit 1 ;; esac