build: 💚 增加docker分层构建,自动发布功能

This commit is contained in:
施海荣 2022-09-15 09:15:54 +08:00
parent 76ad4ac04b
commit 18e950cda7
6 changed files with 176 additions and 3 deletions

10
.dockerignore Normal file
View File

@ -0,0 +1,10 @@
.idea/
.git/
.gitignore
*/**/target/
**/*.iml
*.md
.drone.yml
LICENSE
.dockerignore
Dockerfile

39
.drone.yml Normal file
View File

@ -0,0 +1,39 @@
kind: pipeline
type: docker
name: default
steps:
- name: build
image: docker:dind
volumes:
- name: dockersock
path: /var/run/docker.sock
- name: dockerconfig
path: /root/.docker
commands:
- docker build -t harbor.picaiba.com/kszny/mes-api:1.0.0-india ./ && docker push harbor.picaiba.com/kszny/mes-api:1.0.0-india
- name: deploy
image: harbor.picaiba.com/tools/kubectl
commands:
- echo "172.27.0.20 lb.kubesphere.local" >> /etc/hosts
- sleep 1
- kubectl scale --replicas=0 deployment/mes-api -n mes-india
- sleep 3
- kubectl scale --replicas=1 deployment/mes-api -n mes-india
depends_on:
- build
volumes:
- name: dockersock
host:
path: /var/run/docker.sock
- name: dockerconfig
host:
path: /root/.docker
trigger:
branch:
- porject/yd-monitor
event:
- push

56
Dockerfile Normal file
View File

@ -0,0 +1,56 @@
#FROM maven:3-jdk-8-alpine as builder
#WORKDIR /app
#COPY settings.xml /usr/share/maven/ref/repository/settings.xml
#COPY pom.xml .
#COPY gateway/pom.xml gateway/
#COPY core/pom.xml core/
#COPY common/pom.xml common/
#COPY passport/pom.xml passport/
#COPY upms/pom.xml upms/
#COPY empty/pom.xml empty/
#COPY material/pom.xml material/
#COPY order/pom.xml order/
#COPY monitor/pom.xml monitor/
#COPY equipment/pom.xml equipment/
#COPY basic/pom.xml basic/
## COPY report/pom.xml report/
#
#RUN mvn -B -s /usr/share/maven/ref/repository/settings.xml dependency:resolve-plugins dependency:resolve clean package
#
#COPY ./ ./
#
#RUN mvn -B -s /usr/share/maven/ref/repository/settings.xml -Dmaven.test.skip=true -Ptest clean package
FROM maven:3-jdk-8-alpine as builder
WORKDIR /app
COPY ./ ./
RUN mvn -s settings.xml clean package -Dmaven.test.skip=true
FROM anapsix/alpine-java:8_server-jre_unlimited as layers
LABEL mantainer=shihairong@zimonet.com
WORKDIR /app
COPY --from=builder app/ym-gateway/target/ym-gateway.jar /app/app.jar
#ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-jar","app.jar", "--spring.profiles.active=dev"]
# 通过工具spring-boot-jarmode-layertools从application.jar中提取拆分后的构建结果
RUN java -Djarmode=layertools -jar app.jar extract
# 正式构建镜像
FROM anapsix/alpine-java:8_server-jre_unlimited
# 指定工作目录,目录不存在会自动创建
WORKDIR /app
# 前一阶段从jar中提取除了多个文件这里分别执行COPY命令复制到镜像空间中每次COPY都是一个layer
COPY --from=layers app/dependencies ./
COPY --from=layers app/spring-boot-loader ./
COPY --from=layers app/snapshot-dependencies ./
COPY --from=layers app/company-dependencies ./
COPY --from=layers app/application ./
# 指定时区
ENV TZ="Asia/Shanghai"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 定义一些环境变量,方便环境变量传参
ENV JVM_OPTS=""
ENV JAVA_OPTS="-Dspring.profiles.active=dev"
# 指定暴露的端口,起到说明的作用,不指定也会暴露对应端口
EXPOSE 8080
# 启动 jar 的命令
ENTRYPOINT ["sh","-c","java $JVM_OPTS $JAVA_OPTS org.springframework.boot.loader.JarLauncher"]

19
settings.xml Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/usr/share/maven/ref/repository</localRepository>
<pluginGroups>
<pluginGroup>com.spotify</pluginGroup>
</pluginGroups>
<mirrors>
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>

View File

@ -68,10 +68,32 @@
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.12</version>
<configuration>
<!--指定运行main函数的包-->
<mainClass>com.cnbm.YmApplication</mainClass>
<!--开启分层编译支持-->
<layers>
<enabled>true</enabled>
<configuration>${project.basedir}/src/main/resources/layers.xml</configuration>
</layers>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -0,0 +1,27 @@
<layers xmlns="http://www.springframework.org/schema/boot/layers"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/boot/layers
https://www.springframework.org/schema/boot/layers/layers.xsd">
<application>
<into layer="spring-boot-loader">
<include>org/springframework/boot/loader/**</include>
</into>
<into layer="application" />
</application>
<dependencies>
<into layer="snapshot-dependencies">
<include>*:*:*SNAPSHOT</include>
</into>
<into layer="company-dependencies">
<include>com.cnbm:*</include>
</into>
<into layer="dependencies"/>
</dependencies>
<layerOrder>
<layer>dependencies</layer>
<layer>spring-boot-loader</layer>
<layer>snapshot-dependencies</layer>
<layer>company-dependencies</layer>
<layer>application</layer>
</layerOrder>
</layers>