强制Jenkins管道作业从Dockerhub中提取新的私有映像

问题描述 投票:4回答:1

我想使用Jenkins Docker管道插件在Docker Hub上提取具有特定标记的特定私有映像的新版本。 Docker shell命令看起来像:

docker login -u user -p password
docker pull user/foo:bar

像这样的东西似乎应该工作:

node () {
    image = docker.image('user/foo:bar')
    image.pull()
    image.inside {
        // Commands to run in the container

但是没有办法执行登录步骤,所以我总是得到错误:

Error response from daemon: pull access denied for user/foo, repository does not exist or may require 'docker login'.

我已经浏览了documentationcode,但文档甚至没有提到拉动,代码中的示例没有显示如何登录以拉出私有图像。我可以手动编写脚本,但使用Docker管道插件的全部目的是避免直接编写Docker命令脚本。

docker jenkins jenkins-pipeline
1个回答
6
投票

我相信你需要的是withRegistry功能。它可以如下使用

docker.withRegistry('<registry-url>', '<credential-id>') {
    image = docker.image('user/foo:bar')
    image.pull()
}

withRegistry块中的所有内容都将使用该注册表和身份验证来提取图像。

这种情况下<registry-url>是Dockerhub注册表的URL。我相信Dockerhub注册表URL是https://registry.hub.docker.com

<credential-id>是存储在Jenkins中的Dockerhub凭证的ID。

要添加这些凭据,请从Jenkins索引页面导航到Credentials -> System -> Global credentials -> Add Credentials

进入此页面后,您需要选择Kind作为Username with password。范围应该是Globalusernamepassword字段是Dockerhub的用户名和密码。

ID字段是你想成为<credential-id>的任何文本。例如,如果你使ID字段docker-hub-credentials需要成为withRegistry的第二个参数。

以下是添加凭据的页面的示例。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.