编程语言/Drone/plugins/docker

来自康健生活

标题文本

kind: pipeline
type: docker
name: zkblog

steps:
  # 构建
  - name: build # 流水线名称
    image: node:14-alpine # 定义创建容器的Docker镜像
    volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
      - name: node-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
      - name: node_modules
        path: /drone/src/node_modules
    commands: # 定义在Docker容器中执行的shell命令
      - pwd # 查看当前目录 `/drone/src`
      - ls -alt
      - npm config set registry https://registry.npm.taobao.org # 切换淘宝镜像
      - npm install canvas@2.8.0 --ignore-scripts
      - npm install # 安装依赖
      - npm run build # 执行构建指令
      - cp -r dist /app/build/
      - cp Dockerfile /app/build/
      - cp default.conf /app/build/
      - cp run.sh /app/build/
  # 部署
  - name: deploy # 流水线名称
    image: plugins/docker
    volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置
      - name: node-build
        path: /app/build # 将应用打包好的Jar和执行脚本挂载出来
      - name: docker
        path: /var/run/docker.sock # 挂载宿主机的docker
    settings:
      dockerfile: /app/build/Dockerfile
    commands: # 定义在Docker容器中执行的shell命令
      - cd /app/build
      - chmod +x run.sh
      - sh run.sh
      - docker ps
volumes: # 定义流水线挂载目录,用于共享数据
  - name: node-build
    host:
      path: /home/docker/drone/node/build # 从宿主机中挂载的目录
  - name: docker
    host:
      path: /var/run/docker.sock
  - name: node_modules
    host:
      path: /home/docker/drone/node/node_modules
 - name: push-docker-staging  
    image: plugins/docker  
    settings:  
      repo: allovince/xxx
      username: allovince  
      password: 
        from_secret: DOCKER_PASSWORD  
      tag:  
        - staging  
        - sha_${DRONE_COMMIT_SHA} 
    when:  
      branch: master  
      event: push  
  
  - name: semantic-release  
    image: gtramontina/semantic-release:15.13.3  
    environment:  
      GITHUB_TOKEN:  
        from_secret: GITHUB_TOKEN  
    entrypoint:  
      - semantic-release  
    when:  
      branch: master  
      event: push  
  
  - name: push-docker-production  
    image: plugins/docker  
    environment:  
      DOCKER_PASSWORD:  
        from_secret: DOCKER_PASSWORD  
    commands:   
      - docker -v  
      - nohup dockerd &  
      - docker login -u allovince -p ?{DOCKER_PASSWORD}  
      - docker pull allovince/xxx:sha_?{DRONE_COMMIT_SHA}  
      - docker tag allovince/xxx:sha_?{DRONE_COMMIT_SHA} allovince/xxx:?{DRONE_TAG}  
      - docker push allovince/xxx:?{DRONE_TAG}  
    when:  
      event: tag  
    privileged: true

标题文本

kind: pipeline
name: api
steps:
  # api unit test
  - name: api-test
    image: node:11
    depends_on: [clone]
    commands:
      - cd ./api/
      - npm install --registry https://registry.npm.taobao.org
      - cd ./dgc_sdk && npm install --production
      - cd .. && npm run test
  # web lint
  - name: web-lint
    image: node:10.15.1
    depends_on: [clone]
    commands:
      - cd ./web
      - npm install --registry https://registry.npm.taobao.org
      - npm run lint
trigger:
  branch:
    - master
  event:
    - pull_request
    - push


# 这是一个空的 pipeline
# 当 pull_request事件被触发,并且不是提忘 master 分支时
# 或者出去 tag 的其他的事件时会触发
# 触发后不进行任何操作
# 这个 pipeline 的主要目的是,我们在 gitlba 中定义了如果 CI 不成功,pr 不允许合入分支
# 所以当我们往非 master提交 pr 时,不想让 CI 执行任何步骤,但也不能不触发 CI,因为不触发 CI 那上面的规则就会限制 pr 无法合入
kind: pipeline
type: docker
name: pipeline

clone:
  disable: true

trigger:
  event:
    - pull_request
  branch:
    exclude:
      - master
  ref:
    exclude:
      - refs/tags/*


kind: pipeline
type: docker
name: build

# 自定义 clone 的 git 操作
# image 指定使用 drone 提供的 git 插件,当然我们也可以使用我们自定义的或者其他的 git 镜像
# tags: true 指定在 clone 代码时也将仓库的 tag同步到本地
clone:
  git:
    image: plugins/git
    network_mode: host
    tags: true

# 下面可以定义你的编译步骤和升级操作


kind: pipeline
type: docker
name: build

steps:
  # get tag
  - name: tag
    image: bashell/alpine-bash:latest
    network_mode: host
    depends_on: [clone]
    commands:
        # 当tag 事件触发后,drone 会默认将 tag 名字写入到环境变量DRONE_TAG中,更多默认的环境变量请查看文档:https://docs.drone.io/pipeline/environment/reference/
        # 将当前的 tag 名称输出到.tags文件中,如果tag 名不存在那么输出latest 到 .tags文件中
      - /bin/bash -c '[ -z $DRONE_TAG ] && echo "latest" > .tags || echo $DRONE_TAG > .tags'

  # build api
  - name: api-build
    image: node:12.18.1
    network_mode: host
    depends_on: [tag]
    volumes:
      - name: cache_api_node
        path: /drone/src/api/node_modules
    commands:
      - cd ./api/
      - npm install --registry http://mirrors.cloud.tencent.com/npm
      - npm run build

  # build web
  - name: web-build
    image: node:12.18.1
    network_mode: host
    depends_on: [tag]
    volumes:
      - name: cache_web_app_node
        path: /drone/src/web/app/node_modules
    commands:
      - cd ./web/app
      - npm install --registry http://mirrors.cloud.tencent.com/npm
      - npm run build


kind: pipeline
type: docker
name: unit test


# 将宿主机的路径挂载到 pipeline 中
# 将宿主机的路径/drone/cache/star/golang-app/go挂载到 pipeline 中,并命名为 cache_golang_app_go,用作 golang 程序编译时的缓存文件夹
# 将宿主机的路径/drone/cache/star/api/node_modules挂载到 pipeline 中,并命名为 cache_api_node,用作 node 程序编译时的缓存文件夹
# 将宿主机的路径 /var/run/docker.sock挂载到 pipeline 中,并命名为 docker,让 pipeline 中 docker 镜像使用宿主机的 docker socket
# 将宿主机的路径 /etc/docker/daemon.json挂载到 pipeline 中,并命名为 docker-daemon,让 pipeline 中 docker 镜像使用宿主机的 docker配置
volumes:
 - name: cache_golang_app_go
   host:
     path: /drone/cache/star/golang-app/go
 - name: cache_api_node
   host:
     path: /drone/cache/star/api/node_modules
 - name: docker
   host:
     path: /var/run/docker.sock
 - name: docker-daemon
   host:
     path: /etc/docker/daemon.json

# master pull requset
steps:
 - name: clean
   image: docker:19.03.1
   network_mode: host
   # 将宿主机的 docker和配置挂载到运行的 docker 容器中,那么在容器中运行 docker 命令时,等同于在宿主机中运行该docker 命令
   volumes:
     - name: docker
       path: /var/run/docker.sock
     - name: images
       path: /images
     - name: docker-daemon
       path: /etc/docker/daemon.json
   commands:
       # 清理 docker 服务中不再使用的镜像、挂载、网络等资源
     - docker system prune --force --volumes || true


 # api unit test
 - name: api-test
   image: node:12.18.1
   depends_on: [clean]
   network_mode: bridge
   # 将宿主机中文件夹挂载到容器中,宿主机文件夹中的文件会被容器访问并修改,起到使用缓存的作用,避免每次运行都要重现下载依赖,提高运行速度
   volumes:
     - name: cache_api_node
       path: /drone/src/api/node_modules
   commands:
     - cd ./api/
     - npm install --registry http://mirrors.cloud.tencent.com/npm
     - npm run test

   when:
     branch:
       - master

 # golang-app test
 - name: golang-app-test
   image: library/golang:1.13.3-alpine3.10
   network_mode: host
   depends_on: [clean]
   # 将宿主机中文件夹挂载到容器中,宿主机文件夹中的文件会被容器访问并修改,起到使用缓存的作用,避免每次运行都要重现下载依赖,提高运行速度
   volumes:
     - name: cache_golang_app_go
       path: /drone/src/golang-app/go
   # 由于 golang 是通过GOPATH寻找本地包路径的,因此除了将依赖文件挂载到容器中之外,还要指定GOPATH到这个目录下
   # 设置GOPROXY为腾讯元镜像,提高 golang 依赖下载速度
   environment:
     GOPATH: /drone/src/golang-app/go
     GOPROXY: http://mirrors.cloud.tencent.com/go/
   commands:
     - cd ./golang
     - CGO_ENABLED=0 go test -v
   when:
     branch:
       - master

trigger:
 branch:
   - master
 event:
   - pull_request


kind: pipeline
type: docker
name: build

# 这里使用image_pull_secrets指定拉取docker镜像时的 secret
image_pull_secrets:
  - dockerconfigjson

clone:
  git:
    image: plugins/git
    network_mode: host
    tags: true

steps:
  # build api
  - name: api-build
    image: my-private-node:12.18.1
    network_mode: host
    depends_on: [tag]
    volumes:
      - name: cache_api_node
        path: /drone/src/api/node_modules
    commands:
      - cd ./api/
      - npm install --registry http://mirrors.cloud.tencent.com/npm
      - npm run build


steps:

  # push docker-image
  - name: docker-push
    # docker 插件会根据配置将指定的镜像编译完成,并推送到给定的仓库中去
    image: plugins/docker:18.09.0
    depends_on: [your-app-build]
    settings:
      # 配置推送镜像时连接镜像仓库的用户名
      username:
        # 从配置的 Secret 中获取
        from_secret: docker_username
      # 配置推送镜像时连接镜像仓库的密码
      password:
        # 从配置的 Secret 中获取
        from_secret: docker_password
      # 配置私有镜像仓库地址
      repo: hub.tencentyun.com/demo/your-image-name
      # 指定 context,默认git 仓库根路径
      context: ./
      # 指定 dockerfile
      dockerfile: ./Dockerfile
     
      # 配置编译的镜像的 tag,可以指定多个
      # 也可以在其他步骤(如your-app-build)中,将想要使用的 tag 名写入 .tags 文件中
      # 如: echo ‘1.0.01’ > .tags
      # 那个下面的 tags 项可以不写,默认会使用.tags 文件中的内容
      tags:
        - latest
        - ‘1.0.0'
steps:

# 这里用到了上面说的挂载,忘记了可以看一下本篇文章上面的挂载与环境变量的内容
# 将宿主机的路径挂载到 pipeline 中
# 将宿主机的路径 /var/run/docker.sock挂载到 pipeline 中,并命名为 docker,让 pipeline 中 docker 镜像使用宿主机的 docker socket
# 将宿主机的路径 /etc/docker/daemon.json挂载到 pipeline 中,并命名为 docker-daemon,让 pipeline 中 docker 镜像使用宿主机的 docker配置
volumes:
  - name: docker
    host:
      path: /var/run/docker.sock
  - name: docker-daemon
    host:
      path: /etc/docker/daemon.json

  # push docker-image
  - name: docker-push
    image: docker:19.03.1
    depends_on: [your-app-build]
    volumes:
      - name: docker
        path: /var/run/docker.sock
      - name: docker-daemon
        path: /etc/docker/daemon.json
    environment:
      IMAGE: hub.tencentyun.com/demo/your-image-name
    commands:
      - export DRONE_TAG=$(cat .tags)
      - docker build -f ./Dockerfile -t $IMAGE:$DRONE_TAG ./
      - docker push $IMAGE:$DRONE_TAG