前面的文章有介绍 docker安装jenkins 和 docker+jenkins发布spring项目;这里就不做过多的介绍,直接说明构建步骤。
1、选择构建一个自由风格的项目
2、 选择丢弃旧的构建
3、配置Git信息
4、构建触发器 和 构建环境可以直接跳过
5、直接来到Build Steps这步骤,先新增一个调用顶层Maven目标配置编译项目(如果项目属于微服务架构,可添加多个调用顶层Maven目标步骤,先编译基础依赖包→在编译发布的项目)
6、在Build Steps这步骤,再添加一个 执行Shell
7、查看构建效果
附件脚本:
#!/bin/bash
echo "开始构建===============================>"
JenkinsHost=192.168.254.129
JenkinsPort=22
PROJECT_NAME='springboot-test'ssh -p $JenkinsPort root@$JenkinsHost nohup pkill -15 -f $PROJECT_NAME.jar &
sleep 10secho "COPY JAR TO SERVER START===============================>"
ssh -p $JenkinsPort root@$JenkinsHost mkdir -p /work/project/$PROJECT_NAME
#scp -r /var/jenkins_home/workspace/fat2-springboot-test/target/$PROJECT_NAME.jar root@$JenkinsHost:/work/project/$PROJECT_NAME/
ssh -p $JenkinsPort root@$JenkinsHost /bin/cp -rf /work/docker/data/jenkins/workspace/fat2-springboot-test/target/$PROJECT_NAME.jar /work/project/$PROJECT_NAME/
echo "COPY JAR TO SERVER END===============================>"
echo "CHMOD START===============================>"
ssh -p $JenkinsPort root@$JenkinsHost chmod 755 /work/project/$PROJECT_NAME/$PROJECT_NAME.jar
echo "CHMOD START===============================>"echo "SET LOGS START===============================>"
ssh -p $JenkinsPort root@$JenkinsHost mkdir -p /work/project/logs/$PROJECT_NAME
ssh -p $JenkinsPort root@$JenkinsHost touch /work/project/logs/$PROJECT_NAME/$PROJECT_NAME.log
ssh -p $JenkinsPort root@$JenkinsHost chmod 777 /work/project/logs/$PROJECT_NAME/$PROJECT_NAME.log
echo "SET LOGS START===============================>"echo "RUN JAR START===============================>"
ssh -p $JenkinsPort root@$JenkinsHost << EOFnohup java -Xms256m -Xmx256m -Xmn256m -jar /work/project/$PROJECT_NAME/$PROJECT_NAME.jar > /work/project/logs/$PROJECT_NAME/$PROJECT_NAME.log 2>&1 &
EOF
echo "RUN JAR END===============================>"