docker、git、Maven、jdk8安装略过。
使git pull或者git push不需要输入密码操作方法
约定:
路径:/opt/springbootdemo,
项目:springbootdemo,
打包:springbootdemo.jar,
docker容器名字:springbootdemo
Dockerfile
内存不够自行调整:"-Xmx6144m","-Xms6144m"
bash
FROM openjdk:8
MAINTAINER "hsj<2356899074@qq.com>"
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY ./target/springbootdemo.jar ./springbootdemo.jar
EXPOSE 55530
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dfile.encoding=utf-8","-jar","-Xmx6144m","-Xms6144m","/springbootdemo.jar","--spring.profiles.active=prod"]
pro_build.sh
注意:#!/bin/bash -ilex
,如果jenkins远程ssh登录执行sh脚本,并且jekins安装在docker容器里面
,这句代码很有必要。
bash
#!/bin/bash -ilex
#环境变量
who am i
set -e
# ./nginx -s reload
#当前springboot目录
cd /opt/springbootdemo
echo "拉取gitlib最新代码..."
git status && git pull
#if判断其实是多余的,set -e 当$? 不等于0时候已经退出了
if [[ $? -ne 0 ]]
then
echo "拉取gitlib最新代码失败,准备退出"
exit 1
fi
echo "拉取gitlib最新代码成功"
echo "准备构建jar包..."
mvn clean package
echo "准备停止docker容器并删除旧的docker镜像"
CID=$(docker ps -a | grep springbootdemo| xargs | awk '{print $1}')
IID=$(docker ps -a | grep springbootdemo| xargs | awk '{print $2}')
if [[ -n "$CID" ]]; then
echo "删除旧的容器,CID=$CID"
docker rm -f $CID
fi
if [[ -n "$IID" ]]; then
echo "删除旧的镜像,IID=$IID"
docker rmi -f $IID
fi
echo "准备构建新的镜像并运行新的容器"
docker build -t springbootdemo.
IID_NEW=$(docker images | grep springbootdemo | xargs | awk '{print $3}')
echo "运行的新镜像:${IID_NEW}"
docker run -d --name springbootdemo -p 55530:55530 ${IID_NEW}
echo "正在扫描docker容器启动状态..."
set +e
start_time=$(date -d "today" +%s);
#springbootdemo: Started Application
while true; do docker logs springbootdemo 2>&1 | grep "Started Application" > /dev/null;
if [[ $? -eq 0 ]]; then
echo "发布完毕";
break;
fi
end_time=$(date -d "today" +%s);
t=$(($end_time - $start_time));
if [[ $t -gt 360 ]]; then
echo "超过6分钟,发布失败" ;
exit 1;
fi;
#避免CPU轮询飙升
sleep 30s;
done