Jenkinspipe道:selectnodejs版本(+ python版本)
我正在Jenkins文件中面临Jenkinspipe道的问题。 我的Jenkins实例上有4个不同的nodeJs版本。 我想select我要在我的pipe道中使用哪一个,但官方插件示例( https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin )根本不起作用。
我尝试了第一种方法,失败了,因为$ PATH被tools
部分覆盖。
pipeline { agent any tools { // I hoped it would work with this command... nodejs 'nodejs6' } stages { stage('Example') { steps { sh 'npm --version' // Failed saying : // Running shell script //nohup: failed to run command 'sh': No such file or directory } } } }
我尝试了第二种方法,失败了,因为tool
命令似乎什么都不做。
pipeline { agent any stages { stage('Example') { steps { // ... or this one tool name: 'nodejs6' sh 'node --version' sh 'npm --version' // Does not use this version of node, but the default one... 7.5.0 with npm 4.3.0 } } } }
最后,我尝试了这个,它适用于NodeJS,但似乎不是“非常聪明”,并不允许我正确处理我的具体版本的“Python” – 是我也有2个不同版本的Python,我想要像处理节点一样处理 –
pipeline { agent any stages{ stage ('Which NodeJS'){ steps{ withEnv(["PATH+NODEJS=${tool 'nodejs6'}/bin","PATH+PYTHON27=${tool 'python27'}"]) { // Check node version sh 'which node' // Works properly sh 'node -v' // Expected 6.9.x version sh 'npm -v' // Expected 3.x version sh 'python27 -v' // Failed with // /nd-jenkinsfile_XXX@tmp/xx/script.sh: python27: not found } } } } }
我也有第四个解决scheme,不使用pipeline
语法。 它适用于nodejs,但不适用于python(到目前为止)。 再次,手动定义env.PATH
似乎不是很优雅…
node { // Setup tools... stage ('Setup NodeJs'){ def nodejsHome = tool 'nodejs6' env.NODE_HOME = "${nodejsHome}" env.PATH = "${nodejsHome}/bin:${env.PATH}" sh 'which node' sh 'node -v' sh 'npm -v' } stage ('Setup Python 2.7'){ def pythonBin = tool 'python27' // Jenkins docker image has Jenkins user's home in "/var/jenkins_home" sh "rm -Rf /var/jenkins_home/tools/python ; mkdir -p /var/jenkins_home/tools/python" // Link python to python 2.7 sh "ln -s ${pythonBin} /var/jenkins_home/tools/python/python" // Include link in path --don't use "~" in path, it won't be resolved env.PATH = "~/tools/python:${env.PATH}:~/tools/python" // Displays correctly Python 2.7 sh "python --version" } }
总而言之,我只是想知道哪个解决scheme(当然还有一个我没有在这里列出的)是最好的? 你build议哪一个?为什么?
干杯,奥利维尔
所以。 这是“EnvInject”插件的一个问题: https ://issues.jenkins-ci.org/browse/JENKINS-26583
如果您想保留EnvInject,我的解决方法#4是正确的解决scheme。
env.NODE_HOME="${tool 'Node 6.x'}" env.PATH="${env.NODE_HOME}/bin:${env.PATH}" sh 'npm -version'
否则,删除EnvInject插件也是一个很好的解决scheme。