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

cer*_*wen 5 jenkins docker jenkins-pipeline

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

docker login -u user -p password
docker pull user/foo:bar
Run Code Online (Sandbox Code Playgroud)

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

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

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

Error response from daemon: pull access denied for user/foo, repository does not exist or may require 'docker login'.
Run Code Online (Sandbox Code Playgroud)

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

Mat*_*Coy 9

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

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

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

凡<registry-url>在此情况下,对于Dockerhub注册表中的URL.我相信Dockerhub注册表URL是https://registry.hub.docker.com

这<credential-id>是存储在Jenkins中的Dockerhub凭据的ID.

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

进入此页面后,您需要选择Kindas Username with password.范围应该是Global.该username和password字段是你的Dockerhub用户名和密码.

该ID字段是您想要的任何文本<credential-id>.例如,如果您创建需要作为第二个参数的ID字段.docker-hub-credentialswithRegistry

以下是添加凭据的页面的示例. 在此输入图像描述