简体   繁体   English

带有 docker-workflow-plugin.inside 的 Jenkins 管道 DSL 不允许通过 withEnv 设置/修改 PATH

[英]Jenkins pipeline DSL with docker-workflow-plugin .inside doesn't allow setting/modification of PATH via withEnv

Problem问题

I want to modify the path in a docker container to control tool selection without the need to modify existing pipeline code.我想修改 docker 容器中的路径来控制工具选择,而无需修改现有的管道代码。 I've a shared library and client builds call runAnsible which then runs pipeline DSL within a docker container via the docker-workflow-plugin.我有一个共享库和客户端构建调用 runAnsible,然后通过 docker-workflow-plugin 在 docker 容器中运行管道 DSL。

However, when I use withEnv docker.inside, I cannot modify path但是,当我使用withEnv docker.inside 时,我无法修改路径

docker.inside() {
   withEnv("PATH=${env.PATH}:/ansible2.10/bin") {
    sh 'echo PATH=$PATH'
   }
 }

When results in PATH= the old path value and not containing my modification.当导致 PATH= 旧路径值且不包含我的修改时。 According to JENKINS-45916 that's not a bug but how it works and we all we told - don't do that, use different images etc根据JENKINS-45916 ,这不是错误,而是它是如何工作的,我们都告诉过 - 不要那样做,使用不同的图像等

So, what options do I have to alter the path beyond making a bunch of very similar images with different paths?那么,除了用不同的路径制作一堆非常相似的图像之外,我还有什么选择来改变路径呢?

Docker workflow plugin allows args to be passed to the docker run command which creates the container. Docker 工作流插件允许将参数传递给创建容器的 docker 运行命令。 These args are just a string and its integrity appears respected by the pluign whereas withEnv has some kind of filter in this context.这些 args 只是一个字符串,它的完整性似乎受到 pluign 的尊重,而withEnv在这种情况下具有某种过滤器。

Therefore, this works but it does assume that I know or can determine the original path.因此,这是可行的,但它确实假设我知道或可以确定原始路径。 I could run the container without path modification and use a sh(script: 'echo $PATH', returnStdout: true).trim() via docker.inside to get the knownOriginalPath in an earlier step to fee the code below我可以在不修改路径的情况下运行容器,并通过 docker.inside 使用sh(script: 'echo $PATH', returnStdout: true).trim()在前面的步骤中获取knownOriginalPath以支付下面的代码

// hardcoded because I own the image or have determined what the image's path normally is
def knownOriginalPath="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

// my change to the path - in this case, a custom tool location used because I don't want to have to modify a bunch of existing pipeline code.
def pathModification="/ansible2.10/bin"
def desiredPath="${pathModification}:${knownOriginalPath}"


    docker.withRegistry(...) {
       // for now, the plugin seems to respect the integrity of the inside option string.
        docker.image(..)inside("-e PATH=${desiredPath}") {
            sh 'echo PATH = $PATH'        
        }
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM