drone + gitlab 前端部署(2)

1,169 阅读4分钟

上一节中说了drone + gitlab搭建

这一节承接上一节,讲一下前端如何部署

1、.docker.yml文件

参考drone官方文档,简单介绍一下文件中属性

kind: pipeline  # kind 脚本属于那个种类,包括 pipeline管道、secret、signature,数量不限
type: docker # the type of pipeline,这是docker,each pipeline step is executed inside a Docker container,每一个管道步骤都会运行在一个docker容器,还可以选择其他
name: default # 名称 自拟
steps: # 步骤
    - name: greeting # 第一步名称 
      image: alpine # docker镜像, 在这个镜像中commonds将执行
      commands: # 命令
        - echo hello # 一系列执行命令 
        - echo world
# 等...

2、第一步打包配置

项目中根目录创建.docker.yml文件

kind: pipeline
type: docker
name: default

steps:
  - name: frontend # 第一步
    image: node:slim # 需要node环境
    commands:
      - yarn config set registry https://registry.npm.taobao.org/
      - yarn 
      - yarn run build # 执行打包

3、第二步将打包文件,生成docker镜像

项目根目录,添加 Dockerfile 文件

FROM nginx:alpine
COPY dist/ /usr/share/nginx/html/
COPY nginx.conf /etc/nginx/conf.d/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
  • 1、拉取nginx镜像(前端只需要一个nginx服务器即可);
  • 2、将 dist/ 打包文件 copy 到 /usr/share/nginx/html/;
  • 3、将 nginx.conf copy 到 /etc/nginx/conf.d/;

nginx.conf文件

 server {
    listen  80;  # 暴露80访问端口 
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

4、.docker.yml文件处理 Dockerfile 生成镜像

要处理Dockerfile需要引入docker plugin

docker plugin 文档查询

kind: pipeline
type: docker
name: default

steps:
- name: frontend
  image: node:slim
  commands:
      - yarn config set registry https://registry.npm.taobao.org/
      - yarn 
      - yarn run build 
# 这里添加 Dockerfile 的处理
- name: docker 
  image: plugins/docker
  settings:
     dockerfile: ./Dockerfile
     repo: 1234/abc # 镜像库名
     tags: #分支
       - dev
     dry_run: false # boolean if the docker image should be pushed at the end 是否提交docker镜像 
     purge: false # boolean if cleanup of the docker image should be done at the end # 是否清理镜像
     no_cache: false # do not use cached intermediate containers 不使用缓存中间容器

  • 配置完毕
  • 因为生成的镜像,想在服务器直接部署,不考虑发布docker-hub,做了如上的配置
  • 但是遇到了一个问题,就是当前无法查找到打包的1234/abc镜像

drone中排查日志,发现每一次生成的镜像,docker build is always run with the --rm flag,生成完成之后就直接删除了...

配置中没有找到可以控制--rm的选项,purge选项应该是使用的镜像

dorne github issues 没有开...

官网找找找...

drone 有自己的社区

找到一个同样问题的deper 提问: I notice in the source 1 for the drone plugin, docker build is always run with the --rm flag.

I have docker files that are structured to utilize layer caching where possible, with things like:

FROM golang:1.13 as builder
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -o myApp ./

If run repeatedly on the same agent, I would hope that the module downloading would take advantage of the docker layer cache, and it does if I run docker build myself. But in drone, the --rm flag causes docker to clean up the intermediate images, and every build takes pretty constant time.

I’d like to have an option to disable that feature. There is already the purge argument to the plugin, and I assumed that would do what I want. Took some digging in the logs to realize purge still doesn’t preserve the intermediate containers.

I’d prefer just re-using the purge flag, but I could see maybe adding a new one if that is problematic. Would you accept a pull request to that effect?

作者回复:

The plugins/docker plugin is using docker-in-docker to build the image with an isolated filesystem, which means the plugin is not using the host machine cache. This particular plugin tries to protect the host machine cache from being mutated which could cause race conditions or present security issues (e.g. user overwrites a well-known docker image in the host cache with a malicious replacement).

Although the plugins/docker plugin is optimized for isolation, one could create a plugin that was optimized for performance and caching. Another option would be to interact directly with the host machine docker daemon, as opposed to using the plugin. See an example at docs.drone.io/pipeline/do…

plugins/docker 插件是 使用docker-in-docker去构建镜像,在一个封闭的文件系统中,插件不会使用主机的缓存,保护主机缓存不会被修改覆盖,而造成严重问题(覆盖一个已知的docker 镜像)

要不自己创建一个插件,另一种选择是直接与主机docker守护进程交互, 例子地址docs.drone.io/pipeline/do…

示例代码
kind: pipeline
name: default 

steps: 
    - name: test 
      image: docker:dind 
      volumes: 
        - name: dockersock 
          path: /var/run/docker.sock 
      commands: 
        - docker ps -a 
volumes: 
- name: dockersock 
  host: path: /var/run/docker.sock

5、照此,改写符合自己的.docker.yml

kind: pipeline
type: docker
name: default

steps:
  - name: frontend
    image: node:slim
    commands:
      - yarn config set registry https://registry.npm.taobao.org/
      - yarn
      - yarn run build
  - name: docker
    image: docker
    volumes:
    - name: dockersock
      path: /var/run/docker.sock
    commands:
    - docker build -t 123/abc:dev .
    - docker stop abc
    - docker rm abc
    - docker run -d -p 80:80 --name abc 123/abc:dev
volumes:
- name: dockersock
  host:
    path: /var/run/docker.sock
trigger:
  branch:
    - dev
  event:
    - push