Jenkinsfile中的"与Run"和"Inside&Quot;有什么不同? [英] what is the difference between "withRun" and "inside" in Jenkinsfile?

查看:31
本文介绍了Jenkinsfile中的"与Run"和"Inside&Quot;有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jenkinsfile,我试图在容器内执行npm run test。 当我使用inside运行时,它会失败,但当我使用withRun运行时,它会按我希望的那样运行。

参考代码 使用inside

stage('Test') {
    docker.image('justinribeiro/chrome-headless').inside ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") { 
        sh label: 
        'Running npm test', 
        script: '''
        npm run test
        '''
      }
    }

withRun

stage('Test') {
    docker.image('justinribeiro/chrome-headless').withRun ("-p 9222:9222 --security-opt seccomp=$WORKSPACE/chrome.json") { 
        sh label: 
        'Running npm test', 
        script: '''
        npm run test
        '''

    }
  }

现在我想了解一下它们之间的区别。

我观察到inside添加卷并在容器上运行cat,而withRun没有。

我也阅读了文档https://jenkins.io/doc/book/pipeline/docker/,但理解不够清楚。

更详细的解释将不胜感激。

谢谢。

推荐答案

Image.run([args,command])
使用docker run运行映像,并返回一个容器,您可以稍后停止该容器。可以添加额外的参数,例如‘-p8080:8080--memory-exchange=-1’。可选命令相当于镜像后指定的docker命令。在生成中记录运行指纹。

Image.with Run[(args[,command])]{…)
run类似,但在容器主体退出时立即停止,因此您不需要try-finally挡路。

图像内部[(Args)]{…)
withRun类似,这会在Body持续时间内启动容器,但是Body启动的所有外部命令(Sh)都在容器内部运行,而不是在主机上运行。这些命令在相同的工作目录(通常是Jenkins代理工作区)中运行,这意味着Docker服务器必须位于localhost上。

从上面可以看出,您的sh方法命令(特别是npm命令)将在withRun的主机上执行,但在inside的容器内执行。

文档链接:https://opensource.triology.de/jenkins/pipeline-syntax/globals

这篇关于Jenkinsfile中的"与Run"和"Inside&Quot;有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆