1.目标
- 本文介绍PingsSys微服务脚手架的docker方式部署;
2.步骤
jenkins持续集成部署
- 步骤:
- 运行jenkins容器
- 手动触发使用jenkins从github获得pingsSys项目
- 如果有修改,则下载到本地
- 构建:
- 后台:通过maven构建为war包
- 前端:通过node构建为静态资源
- 通过ssh把war包/静态资源发送到远程服务器
- 执行shell脚本,删除原有的容器和镜像,生成最新的镜像,并运行
运行jenkins容器
- 创建dockerfile文件d-jenkins,运行jenkins容器
# 系统镜像CentOS7
FROM daocloud.io/centos:latest
# 维护者
MAINTAINER Pings 275598139@qq.com
# 环境变量
ENV LANG en_US.UTF-8
# 设置时区
RUN ln -s -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# 安装依赖
RUN yum install -y wget net-tools git
# 安装JDK
RUN mkdir -p /opt/java/jdk
RUN wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" -P /opt/java/jdk https://download.oracle.com/otn-pub/java/jdk/8u201-b09/42970487e3af4f5aa5bca3f542482c60/jdk-8u201-linux-x64.tar.gz
RUN tar xzf /opt/java/jdk/jdk-8u201-linux-x64.tar.gz -C /opt/java/jdk && rm -rf /opt/java/jdk/jdk-8u201-linux-x64.tar.gz
# 安装tomcat
RUN mkdir -p /opt/java/tomcat
RUN wget -P /opt/java/tomcat http://archive.apache.org/dist/tomcat/tomcat-8/v8.5.8/bin/apache-tomcat-8.5.8.tar.gz
RUN tar xzf /opt/java/tomcat/apache-tomcat-8.5.8.tar.gz -C /opt/java/tomcat && rm -rf /opt/java/tomcat/apache-tomcat-8.5.8.tar.gz
# 设置环境变量
ENV JAVA_HOME /opt/java/jdk/jdk1.8.0_201
ENV CATALINA_HOME /opt/java/tomcat/apache-tomcat-8.5.8
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
# 下载jenkins并部署到tomcat
RUN mkdir -p /opt/java/jenkins
RUN wget -P /opt/java/jenkins http://mirrors.jenkins.io/war/latest/jenkins.war
RUN cp /opt/java/jenkins/jenkins.war /opt/java/tomcat/apache-tomcat-8.5.8/webapps/
# 开启8080端口
EXPOSE 8080
# 启动tomcat服务器
CMD ["./opt/java/tomcat/apache-tomcat-8.5.8/bin/catalina.sh", "run"] && tail -f /opt/java/tomcat/apache-tomcat-8.5.8/logs/catalina.out
# 运行命令
# docker build -t pings/jenkins -f d-jenkins .
# docker run -p 8080:8080 --name jenkins-test pings/jenkins
配置jenkins管理页面
- 安装插件
- 安装默认的基础插件
- 安装Maven Integration plugin(用于新建maven任务)
- 安装Publish Over SSH(用于ssh远程发送打好的war包)
- 安装NodeJS plugin(用于编译前端js项目)
- 配置插件
- maven:选择自动安装
- node:选择自动安装
- git:默认,不配置(容器已经安装)
- jdk:默认,不配置(容器已经安装)
- 配置任务
- 后台
- 新建maven任务
- 配置
-
Build:
- Root POM:pom.xml
- Goals and options:clean install
- 构建后操作:Send build artifacts over SSH
配置项 配置值 ssh server 112服务器(远程服务器) source files pings-service-sys/target/pings-service-sys-0.0.1-SNAPSHOT.jar remove prefix pings-service-sys/target/(删除前缀,不设置会把pings-service-sys文件夹发送到112服务器) remote directory /root/pings-sys/ exec command BUILD_ID=DONTKILLME /root/pings-sys/start-service-sys.sh 配置项 配置值 ssh server 112服务器(远程服务器) source files pings-web-admin/target/pings-web-admin-0.0.1-SNAPSHOT.jar remove prefix pings-web-admin/target/(删除前缀,不设置会把pings-web-admin文件夹发送到112服务器) remote directory /root/pings-sys/ exec command BUILD_ID=DONTKILLME /root/pings-sys/start-web-admin.sh
- start-service-sys.sh
echo "开始启动pings-service-sys服务......" # 停止/删除容器 container = $(docker ps | grep 'pings-service-sys' | awk '{print $1}') if [ "$container" != "" ]; then docker stop $container docker rm $container fi # 删除镜像 image = $(docker images | grep 'pings/pings-service-sys' | awk '{print $1}') if [ "$image" != "" ]; then docker rmi $image fi docker build -t pings/pings-service-sys -f d-service-sys /root/pings-sys/ docker run -p 20880:20880 --net ping-sys --name pings-service-sys pings/pings-service-sys
- d-service-sys
# 系统镜像java8(安装了jdk8的镜像) FROM pings/java8 # 维护者 MAINTAINER Pings 275598139@qq.com # 添加项目 COPY pings-service-sys-0.0.1-SNAPSHOT.jar /opt/project/java/pings-service-sys-0.0.1-SNAPSHOT.jar # 开启20880端口 EXPOSE 20880 CMD java -jar -Dspring.profiles.active=prod /opt/project/java/pings-service-sys-0.0.1-SNAPSHOT.jar # 运行命令 # docker build -t pings/pings-service-sys -f d-service-sys /root/pings-sys/ # docker run -p 20880:20880 --net ping-sys --name pings-service-sys pings/pings-service-sys
echo "开始启动pings-web-admin服务......" # 停止/删除容器 container = $(docker ps | grep 'pings-web-admin' | awk '{print $1}') if [ "$container" != "" ]; then docker stop $container docker rm $container fi # 删除镜像 image = $(docker images | grep 'pings/pings-web-admin' | awk '{print $1}') if [ "$image" != "" ]; then docker rmi $image fi docker build -t pings/pings-web-admin -f d-web-admin /root/pings-sys/ docker run -P --net ping-sys --name pings-web-admin pings/pings-web-admin
- d-web-admin
# 系统镜像java8(安装了jdk8的镜像) FROM pings/java8 # 维护者 MAINTAINER Pings 275598139@qq.com # 添加项目 COPY pings-web-admin-0.0.1-SNAPSHOT.jar /opt/project/java/pings-web-admin-0.0.1-SNAPSHOT.jar # 开启20880端口 EXPOSE 8080 20880 CMD java -jar -Dspring.profiles.active=prod /opt/project/java/pings-web-admin-0.0.1-SNAPSHOT.jar # 运行命令 # docker build -t pings/pings-web-admin -f d-web-admin /root/pings-sys/ # docker run -P --net ping-sys --name pings-web-admin pings/pings-web-admin
- 前端
- 新建自由风格任务
- 配置
- 源码管理:https://github.com/pingszi/pingsSys.git
- 构建环境:选择Provide Node & npm bin/ folder to PATH,输入项默认
- 构建:执行shell
# 下载了整个pingsSys项目,pings-ui-admin是其中一个子项目 cd pings-ui-admin # 根据pings-ui-admin项目的package.json编译 npm run build
-
Build:
- Root POM:pom.xml
- Goals and options:clean install
- 构建后操作:Send build artifacts over SSH
配置项 配置值 ssh server 112服务器(远程服务器) source files pings-ui-admin/dist/** remove prefix pings-ui-admin/dist/ (删除前缀,不设置会把pings-ui-admin文件夹发送到112服务器) remote directory /root/pings-sys/pings-ui-admin exec command BUILD_ID=DONTKILLME /root/pings-sys/start-ui-admin.sh
- start-ui-admin.sh
echo "开始启动pings-ui-admin服务......" # 停止/删除容器 container = $(docker ps | grep 'pings-ui-admin' | awk '{print $1}') if [ "$container" != "" ]; then docker stop $container docker rm $container fi # 删除镜像 image = $(docker images | grep 'pings/pings-ui-admin' | awk '{print $1}') if [ "$image" != "" ]; then docker rmi $image fi docker build -t pings/pings-ui-admin -f d-ui-admin /root/pings-sys/ docker run -P --net ping-sys --name pings-ui-admin pings/pings-ui-admin
- d-ui-admin
FROM nginx # pings-ui-admin/docker/目录下 COPY ui-admin.conf /etc/nginx/conf.d/default.conf COPY pings-ui-admin /usr/share/nginx/html/pings-ui-admin EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] # 运行命令 # docker build -t pings/pings-ui-admin -f d-ui-admin /root/pings-sys/ # docker run -P --net ping-sys --name pings-ui-admin pings/pings-ui-admin
- 后台