持续集成:自动部署打包发布代码
Jenkins工作流程
项目已经基于Jenkins实现了持续集成,每当我们push代码时,就会触发项目完成自动编译和打包。而需要运行某个微服务时,我们只需要经过两步:
- 第一步,访问jenkins控制台
- 第二步,点击对应微服务后面的运行按钮
进入nacos可以看到服务启动了。
1) 当代码提交时 触发web钩子(一个url地址的自动调用叫web钩子):
http://192.168.150.101:18080/gogs-webhook/?job=tjxt-dev-build
2) jenkins 的任务 tjxt-dev-build被触发
3) tjxt-dev-build 任务会自动拉取: git 代码:
jenkins 会自动把代码拉取到 /usr/local/src/jenkins/workspace/tjxt-dev-build (对应的任务文件夹下)
4) 自动构建 lesson-init 分支 (编译打包)
5) 构建完成后 执行了如下的指令
ssh root@192.168.150.101 "cd /usr/local/src/script && rm -rf * && cp ../jenkins/workspace/tjxt-dev-build/Dockerfile . && cp ../jenkins//tjxt-dev-build/startup.sh . && chmod a+x startup.sh"
5.1) 进入 /usr/local/src/script
5.2) 删除所有东西
5.3) 复制 ../jenkins/workspace/tjxt-dev-build/Dockerfile 到 上述目录
5.4) 复制 ../jenkins//tjxt-dev-build/startup.sh 到 上述目录
5.5) 并给予 startup.sh 可执行权限(所有用户)
测试流程
-
push代码 到远程git仓库
-
查看仓库的代码提交记录
- 查看web钩子上的Jenkins任务构建历史
查看虚拟机中Jenkins修改的文件
附Dockerfile和startup.sh
目前我看不太懂,只能请求某chat解释一下
FROM openjdk:11.0-jre-buster
LABEL maintainer="研究院研发组 <research-maint@itcast.cn>"
ENV JAVA_OPTS=""
# 设定时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
WORKDIR /app
ADD app.jar /app/app.jar
ENTRYPOINT ["sh","-c","java -jar $JAVA_OPTS /app/app.jar"]
#! /bin/sh
cd /usr/local/src/script || exit 1
BASE_PATH='/usr/local/src/jenkins/workspace/tjxt-dev-build'
PROJECT_NAME=""
PROJECT_PATH=''
CONTAINER_NAME=""
JAVA_OPTS="-Xms300m -Xmx300m"
PORT=8080
DEBUG_PORT=0
while getopts "c:n:d:p:o:a:" opt; do
case $opt in
c)
CONTAINER_NAME=$OPTARG
;;
n)
PROJECT_NAME=$OPTARG
;;
d)
PROJECT_PATH=$OPTARG
;;
p)
PORT=$OPTARG
;;
o)
[ -n "$OPTARG" ] && JAVA_OPTS=$OPTARG
;;
a)
[ -n "$OPTARG" ] && DEBUG_PORT=$OPTARG
;;
?)
echo "unkonw argument"
exit 1
;;
esac
done
if [ "$DEBUG_PORT" = "0" ]; then
JAVA_OPTS=$JAVA_OPTS
else
JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005"
fi
IMAGE_NAME="${CONTAINER_NAME}:latest"
echo "copy xx.jar from ${BASE_PATH}/${PROJECT_PATH}"
rm -f app.jar
cp ${BASE_PATH}/${PROJECT_PATH}/target/${PROJECT_NAME}.jar ./app.jar || exit 1
echo "begin to build ${PROJECT_NAME} image !!"
[ -n "`docker ps -a | grep ${CONTAINER_NAME}`" ] && docker rm -f ${CONTAINER_NAME}
[ -n "`docker images | grep ${CONTAINER_NAME}`" ] && docker rmi ${IMAGE_NAME}
docker build -t ${IMAGE_NAME} . || exit 1
echo "${PROJECT_NAME} image build success,java_opts = $JAVA_OPTS !!^_^"
echo "begin to create container ${CONTAINER_NAME},port: ${PORT} !!"
if [ "$DEBUG_PORT" = "0" ]; then
echo "run in normal mode"
docker run -d --name ${CONTAINER_NAME} \
-p "${PORT}:${PORT}" \
-e JAVA_OPTS="${JAVA_OPTS}" \
--memory 300m --memory-swap -1 \
--network heima-net ${IMAGE_NAME} \
|| exit 1
else
echo "run in debug mode"
docker run -d --name ${CONTAINER_NAME} \
-p "${PORT}:${PORT}" \
-p ${DEBUG_PORT}:5005 \
-e JAVA_OPTS="${JAVA_OPTS}" \
--network heima-net ${IMAGE_NAME} \
|| exit 1
fi
echo "container is running now !! ^_^"
exit 0