Jenkins脚本
#!/bin/bash
project_name=$1 #项目名字
branch=$2 #分支
host=$3
port=$4
currentTime=`date "+%Y%m%d_%H%M%S"`
#代码路径
dir="/root/.jenkins/workspace/$project_name"
#远端项目路径
remote_dir="/data/server_home/$project_name"
cd $dir && git pull && git checkout $branch && git pull
echo "################开始编译##########################"
sleep 2
cd $dir && mvn clean -Dmaven.test.skip=true kotlin:compile package
echo "####################开始执行备份######################"
#执行备份
ssh root@$host -p$port "[ ! -d $remote_dir/backup ] && mkdir -p $remote_dir/backup || echo '存在' "
ssh root@$host -p$port "mv $remote_dir/*.jar $remote_dir/backup/"
echo "#################开始拷贝jar包#######################"
#拷贝jar包到远程主机
scp -P$port -r $dir/target/*.jar root@$host:$remote_dir #不带日志的jar包
#查看新的jar包名字
jarname=`ssh root@$host -p$port "ls -lt /data/server_home/$project_name/*.jar" | awk '{print $9}' |awk -F '[/]' '{print $5}'`
#启动服务
echo "#################开始启动服务#######################"
ssh root@$host -p$port "cd $remote_dir && sh common_restart.sh $jarname $project_name"
-----------------------------------
©著作权归作者所有:来自51CTO博客作者huningfei的原创作品,如需转载,请注明出处,否则将追究法律责任
利用jenkins自由风格用shell脚本进行所有项目发版(java项目)
#!/bin/sh
source /etc/profile
JAVA_HOME=/usr/bin/java
project_name=$2
ARTIFACT_ID=$project_name
CURRENT_PATH=/data/server_home/$project_name
APP_NAME=$1
SERVER_PID=0
DATETIME=`date +%F-%H-%M-%S`
start()
{
checkpid
if [ "$SERVER_PID" != "" ]
then
echo "$APP_NAME is running!please use restart command."
exit 1;
fi
if [ "$JAVA_HOME" != "" ]
then
/usr/bin/nohup java -Xms512m -Xmx1g -jar $CURRENT_PATH/$APP_NAME --spring.profiles.active=zgy-pre --server.port=8084 > $CURRENT_PATH/${DATETIME}-running.log &
sleep 5
checkpid
if [ "$SERVER_PID" != "" ]
then
echo -e "\e[0;35m$APP_NAME\e[0m start over in path: \033[46;31m$CURRENT_PATH\033[0m,and running pid is \033[46;31m$SERVER_PID\033[0m"
echo -e "Please see the \033[46;31mrunning.log\033[0m for detatil"
else
echo -e "\e[0;35m$APP_NAME\e[0m start \033[46;31mFAILURE\033[0m"
echo -e "Please see the \033[46;31mrunning.log\033[0m for detatil"
exit 1;
fi
else
echo -e "JAVA_HOME can't acquire"
exit 1;
fi
}
stop()
{
checkpid
if [ "$SERVER_PID" != "" ]
then
echo -e "\e[0;35m$APP_NAME\e[0m running pid is \033[46;31m$SERVER_PID\033[0m, kill it at once"
kill -9 $SERVER_PID
fi
}
checkpid(){
SERVER_PID=$(ps -aef |grep java |grep "$ARTIFACT_ID" |grep -v grep | awk '{print $2}')
}
restart()
{
stop
sleep 5
start
}
restart
-----------------------------------
©著作权归作者所有:来自51CTO博客作者huningfei的原创作品,如需转载,请注明出处,否则将追究法律责任
利用jenkins自由风格用shell脚本进行所有项目发版(java项目)
https://blog.51cto.com/825536458/3657279