如何在Jenkins声明式pipe道中从docker中心推拉

我正在使用标准的“sh”命令成功地在我的jenkinsfilepipe道中build立一个从站的docker镜像。

我希望能够尝试从docker / hub中提取图像,如果失败(因为尚未保存),请将其构build并将其推送到docker集线器。

很明显,我需要存储docker中心的凭据,并提供他们的dockerlogin命令。

我的问题是我很困惑。 有dockerpipe道插件,但我认为他们是在docker集装箱内运行jenkins步骤 – 而不是我想要做的。 还有很多例子似乎是脚本化的,而不是声明式的。

用简单的“步骤”术语来说,我想我想做类似的事情

agent { label 'pi' } steps { sh 'docker login -u my-account -p xxxx' sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)' .... } 

但我也怀疑我可能需要做类似的事情

 steps { script { withDockerRegistry([credentialsId: 'something', url: 'docker.io/my-account']) { sh 'docker pull my-account/image:version || (docker build -f dockerfile -t my-account/image:version && docker push my-account/image:version)' .... } } 

但我不想犯任何错误,并拧上一个目前工作正常的pipe道,除了这一点点。

我沿着正确的路线吗?

我也在处理同样的问题。 我希望从我的Dockerhub仓库中拖出Docker镜像,并对它们执行一些testing。 我已经设法使我的pipe道工作按照这里显示的步骤。

https://gist.github.com/jglick/0aa389c053196e38e2a1

如果你有Dockerpipe道插件,你可以像这样创build和推送图像。

 dir(config.buildFolder){ newImage = docker.build(${imageTag}) docker.withRegistry("https://${registryAddress}", '${credentialsId}'){ newImage.push("${variables.version}") } 

你可以像这样在一个容器里拉和执行一些代码:

 testbed = docker.image('${imageName}') testbed.inside("-u root:root"){ echo 'executing build inside container' sh 'dotnet restore' } 

这会自动将创build该容器的文件夹挂载为容器的根目录,以便您可以使用该数据(如构build应用程序)