如何从无人机 ci 管道中的私有 docker 注册表中提取图像

How to pull images from private docker registry in drone ci pipeline

问题

情况

os 版本:ubuntu 18.04lts
无人机版本:1.0.0
docker版本:18.09.4
docker-compose 版本:1.23.2

我 host drone ci 自己 docker 和 docker-compose(一个主人,两个代理人)。

管道配置示例:

---

kind: pipeline
name: integration

steps:
  - name: tests
    image: private-registry.example.com/nodejs/special
    commands:
      - npm i
      - npm run test
    when:
      event:
        - push
        - pull_request
...

无人机文档

drone 的文档指的是在没有提供 link 的情况下配置私有注册表,也没有在图片部分的 the documentation page about pipeline steps 上提供有关如何完成此操作的更多详细信息:

If the image does not exist, Drone instructs Docker to pull it. If the image is private you will need to configure registry credentials.


我发现这个问题,其中操作指的是文档中的 this page,但该文档已不存在。

问题的作者和接受的答案的作者都在使用 drone cli 通过 registry 命令向 drone 添加注册表。但不幸的是,此命令不再可用。

当前的无人机 cli 使用消息如下所示:

NAME:
   drone - command line utility

USAGE:
   drone [global options] command [command options] [arguments...]

VERSION:
   1.0.7

COMMANDS:
     build      manage builds
     cron       manage cron jobs
     log        manage logs
     encrypt    encrypt a secret
     exec       execute a local build
     info       show information about the current user
     repo       manage repositories
     user       manage users
     secret     manage secrets
     server     manage servers
     queue      queue operations
     autoscale  manage autoscaling
     fmt        format the yaml file
     convert    convert legacy format
     lint       lint the yaml file
     sign       sign the yaml file
     jsonnet    generate .drone.yml from jsonnet
     script     generate .drone.yml from script
     plugins    plugin helper functions
     help, h    Shows a list of commands or help for one command

GLOBAL OPTIONS:
   -t value, --token value   server auth token [$DRONE_TOKEN]
   -s value, --server value  server address [$DRONE_SERVER]
   --autoscaler value        autoscaler address [$DRONE_AUTOSCALER]
   --help, -h                show help
   --version, -v             print the version

我分别找到了github issue referring to a pull request adding a docker section from 2014. That all seems to have been moved to the docker and docker:dind个插件

问题

  1. 那么最后,有没有一种方法可以为无人机添加私有 docker 注册表,如何实现?
  2. 是否有详细说明如何操作的文档?

解决方案

确实有一个 documentation section 专门用于私人 docker 镜像注册中心。

Image Pull Secrets

In order to download a private image you will need to provide a docker registry config file, which embeds the authentication credentials to the registry.

Example .docker/config.json file:

{
  "auths": {
    "https://index.docker.io/v1/": {
      "auth": "b2N0b2NhdDpjb3JyZWN0LWhvcnNlLWJhdHRlcnktc3RhcGxl"
    }
  }
}

The contents of the file should be stored as a secret, where the named secret is referenced in the image_pull_secrets section of the yaml.

kind: pipeline
name: default
type: docker

steps:
- name: build   
  image: testing/test-image   
  commands:
  - go build
  - go test

image_pull_secrets:
- dockerconfigjson

因此,我们要做的是执行 docker login <registry host> 并将从 ~/.docker/config.json 生成的凭据保密,在名为 dockerconfigjson 的示例中。

这是 AWS ECR 的完整工作流程。 假设

  • 你有一个有效的aws CLI 安装
  • 你有一个有效的drone CLI 安装
  1. 为您的注册表获取令牌:aws ecr get-login-password --region <your region>

  2. 将代币添加到 Drone CI 中的组织:drone orgsecret add <organization> my-pull-secret <secret from step 1>

  3. 将以下内容添加到您的.drone.yml

    image_pull_secrets:
      - my-pull-secret
    

对于 Google Cloud,秘密不能是令牌,而是 JSON 文件,如 described in the GCloud docs