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



问题

我想修改docker容器中的路径以控制工具选择,而无需修改现有的管道代码。我有一个名为runAnsible的共享库和客户端构建,然后通过docker工作流插件在docker容器中运行管道DSL。

但是,当我使用withEnvdocker.inder时,我无法修改路径

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

当结果显示PATH=旧路径值并且不包含我的修改时。根据JENKINS-45916的说法,这不是一个错误,而是它的工作原理,我们都告诉过——不要这样做,使用不同的图像等

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

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

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

最新更新