如何在 Jenkins 管道中使用多个 docker 存储库

HHH*_*HHH 7 jenkins docker jenkins-pipeline

我有一个 Jenkins 管道,我需要在其中登录到两个不同的 docker 存储库。我知道如何使用以下命令对一个 repo 进行身份验证

docker.withRegistry('https://registry.example.com', 'credentials-id')
Run Code Online (Sandbox Code Playgroud)

但不知道如何处理超过 1 个回购?

Ale*_*lin 5

嵌套docker.withRegistry调用实际上按预期工作。每次调用都会/home/jenkins/.dockercfg使用提供的凭据添加一个条目。

// Empty registry ('') means default Docker Hub `https://index.docker.io/v1/`
docker.withRegistry('', 'dockerhub-credentials-id') {
  docker.withRegistry('https://private-registry.example.com', 'private-credentials-id') {
    // your build steps ...
  }
}
Run Code Online (Sandbox Code Playgroud)

这允许您使用提供的凭据从 Docker Hub 拉取基础镜像,以避免最近引入的拉取限制,并将结果推送到另一个 docker 注册表。

  • 这不再起作用,对 withRegistry 的调用会创建一个临时的 docker 配置文件,该文件会随着每次调用而更改。例如,“警告!您的密码将以未加密的方式存储在 /home/ubuntu/workspace/test@tmp/af4e4211-ee8e-4510-9efe-734d6329990d/config.json 中。” https://issues.jenkins.io/browse/JENKINS-58837 (3认同)

Lee*_*dor 2

这是部分答案,仅适用于您使用两个注册表但只需要其中一个注册表的凭据时。您可以嵌套这些调用,因为它们大多只是进行 docker 登录,该登录在关闭范围内保持活动状态,并将注册表域名添加到 docker 推送等中。

在脚本化 Jenkins 管道或声明性 Jenkins 管道的脚本 { } 部分中使用它:

docker.withRegistry('https://registry.example1.com') { // no credentials here
    docker.withRegistry('https://registry.example2.com', 'credentials-id') { // credentials needed
        def container = docker.build('myImage')
        container.inside() {
            sh "ls -al" // example to run on the newly built 
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有时,您可以一个接一个地使用对 docker.withRegistry() 的两个非嵌套调用,但构建就是一个不能使用的示例,例如,如果 Dockerfile 中第一个 FROM 的基础映像需要一个注册表,并且第二个 FROM 的基本映像位于另一个注册表中。