Jenkins CI/CD脚本示例

Dockerfile镜像构建脚本


基础镜像构建脚本

以下示例是一个集成了skywalking组件的基础镜像的构建脚本

FROM openjdk:8-jdk-alpine
 
LABEL "Describe"="skywalking-agent"
LABEL "Author"="CRS Micro-service Group"
 
ENV TZ=Asia/Shanghai
 
COPY agent/ /opt/agent

使用docker build命令构建

docker build -t micro-service/skywalking-agent .

业务服务镜像脚本

以下示例是一个继承基础镜像的构建脚本

FROM 172.16.5.33:3300/micro-service/skywalking-agent
 
LABEL "Application_Environment"="Dev"
LABEL "Author"="CRS Micro-service Group"
 
WORKDIR /
COPY app-service.jar /service/app-service.jar
 
ENTRYPOINT ["sh","-c","java ${JAVA_OPTS} -Denv=${APOLLO_ENV} -javaagent:/opt/agent/skywalking-agent.jar=agent.service_name=${SW_AGENT_NAME},collector.backend_service=${SW_AGENT_COLLECTOR_BACKEND_SERVICES} -jar /service/app-service.jar"]
 
EXPOSE 9056

构建镜像


构建docker镜像,并推送到镜像私服

# 脚本变量定义
HARBOR_ADDRESS=172.16.5.33:3300
HARBOR_PROJECT_NAME=micro-service
HARBOR_USER=admin
HARBOR_PASSWORD=Harbor@12345
 
# 登陆远程仓库
docker login $HARBOR_ADDRESS -u $HARBOR_USER -p $HARBOR_PASSWORD
 
# 构建app-service镜像,并推送至harbor
 
echo '==================== APP-SERVICE Build Start! ===================='
cd $WORKSPACE/microsvc-app-provider/target
mv *.jar app-service.jar
# 复制Dockerfile文件
cp /opt/dockerfiles/app-service/Dockerfile ./
# 构建镜像
docker build -t app-service:$POM_VERSION .
# 定义版本标签
docker tag app-service:$POM_VERSION $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service:$POM_VERSION
# 推送到远程仓库
docker push $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service:$POM_VERSION
# 定义latest版本标签
docker tag app-service:$POM_VERSION $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service
# 推送到远程仓库
docker push $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service
 
# 推送成功后删除本地镜像
docker rmi app-service:$POM_VERSION
docker rmi $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service:$POM_VERSION
docker rmi $HARBOR_ADDRESS/$HARBOR_PROJECT_NAME/app-service
echo '==================== APP-SERVICE Build End! ===================='

启动脚本


使用docker-compose方式启动

编辑docker-compose.yml文件,配置镜像启动参数

version: '3'
services:
  app-service:
    container_name: app-service
    image: '172.16.5.33:3300/micro-service/app-service:latest'
    restart: always
    volumes:
      - /var/log/app-service/:/service/logs/
    ports:
      - "9056:9056"
    environment:
      - JAVA_OPTS=-Xmx200m -Xms200m
      - SW_AGENT_NAME=app-service
      - SW_AGENT_COLLECTOR_BACKEND_SERVICES=172.16.5.31:11800
      - APOLLO_ENV=dev

上一篇:win7下android开发


下一篇:DevOps 与 CICD 详解