背景
现在项目中拥有多种技术栈的服务,如Go,Java,Web服务,以及其他类服务。在做CI/CD时,不得不根据技术栈不同而建立不同的jenkins的job来实现,而每种技术栈里编译打包的动作各不相同,对运维要求很高。自从docker 17.06版开始支持multi-stage build以后,我们可以不分技术栈,所有服务统一使用docker build来实现编译打包。
Dockerfile以及相关配置
- Go
- 由于国内无法拉取依赖,这里省略掉了拉取镜像的动作,默认代码库里包含有依赖。
- Dockerfile
FROM golang:1.10.2-alpine3.7 as BUILD
COPY . /go/src/PACKAGE.COM/SERVICE_NAME
WORKDIR /go/src/PACKAGE.COM/SERVICE_NAME
RUN go build -o app .
FROM alpine:3.7
WORKDIR /app/
COPY --from=BUILD /go/src/PACKAGE.COM/SERVICE_NAME/app .
CMD ["./app"]
- Web
- node-base为基于node:10.9.0-alpine做的一个基础镜像,其中配置了内部npm repo,并且内置了启动shell
entrypoint.sh
。 - 内部npm配置文件为.npmrc。
-
entrypoint.sh
会生成nginx配置并启动,其中root路径配置为/usr/share/nginx/html。 - Dockerfile
- node-base为基于node:10.9.0-alpine做的一个基础镜像,其中配置了内部npm repo,并且内置了启动shell
FROM node-base:0.0.10 as BUILD
ADD ./ /app/
WORKDIR /app
RUN yarn && yarn run build
FROM nginx:1.13.1-alpine
WORKDIR /app
COPY --from=BUILD /app/dist/ /usr/share/nginx/html
COPY --from=BUILD /app/entrypoint.sh /app/
RUN chmod +x /app/entrypoint.sh
CMD ["./entrypoint.sh"]
- Java
- java-base为基于maven:3.5.4-jdk-8-alpine的基础镜像,配置了maven。
- 配置了dev模式和release模式,通过build参数控制。
+ maven配置例
<?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>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>repo</id>
<username>li.bai</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<mirror>
<id>repo</id>
<name>Maven Repository</name>
<url>http://MAVEN_HOST</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>repo</id>
<name> Maven Repository</name>
<url>http://MAVEN_HOST/artifactory/repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>repo</id>
<name>Maven Repository</name>
<url>http://MAVEN_HOST/artifactory/repo</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
- Dockerfile
FROM java-base:0.0.1 as BUILD
ARG release_mode
ARG build_version
ADD . /app
WORKDIR /app
RUN if [ "${release_mode}" = "dev" ] ; then \
echo "Dev Mode" && mvn -B -f /app/pom.xml -s /usr/share/maven/ref/settings.xml clean install -Dmaven.test.skip=true -U ; \
else \
echo "Release Mode" && mvn -B -f /app/pom.xml -s /usr/share/maven/ref/settings.xml versions:update-parent && \
mvn -B -f /app/pom.xml -s /usr/share/maven/ref/settings.xml versions:set -DnewVersion=${build_version} -DgenerateBackupPoms=false && \
mvn -B -f /app/pom.xml -s /usr/share/maven/ref/settings.xml clean deploy -Dmaven.test.skip=true -Pmy-product -U ; \
fi
FROM frolvlad/alpine-oraclejdk8:latest
COPY --from=BUILD /app/starter/application/target/*-release.zip /app.zip
WORKDIR /application
RUN unzip /app.zip
CMD ["sh","-c","bin/app console"]
RUN rm -f /app.zip
Jenkins Job配置
- 拉取git代码。
- 执行shell。
docker build IMAGE:${VERSION} --build-arg release_mode=release --build-arg build_version=${VERSION} .
docker push IMAGE:${VERSION}
- 调用发布镜像到服务器的job。