繁体   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

问题

我想修改 docker 容器中的路径来控制工具选择,而无需修改现有的管道代码。 我有一个共享库和客户端构建调用 runAnsible,然后通过 docker-workflow-plugin 在 docker 容器中运行管道 DSL。

但是,当我使用withEnv docker.inside 时,我无法修改路径

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

当导致 PATH= 旧路径值且不包含我的修改时。 根据JENKINS-45916 ,这不是错误,而是它是如何工作的,我们都告诉过 - 不要那样做,使用不同的图像等

那么,除了用不同的路径制作一堆非常相似的图像之外,我还有什么选择来改变路径呢?

Docker 工作流插件允许将参数传递给创建容器的 docker 运行命令。 这些 args 只是一个字符串,它的完整性似乎受到 pluign 的尊重,而withEnv在这种情况下具有某种过滤器。

因此,这是可行的,但它确实假设我知道或可以确定原始路径。 我可以在不修改路径的情况下运行容器,并通过 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