Jenkins管道:选择nodejs版本(+ python版本)

Oli*_*ier 11 node.js python-2.7 jenkins jenkins-plugins jenkins-pipeline

我在Jenkinsfile中遇到Jenkins管道问题.我的Jenkins实例上有4个不同的nodeJs版本.我想选择我将在我的管道中使用哪一个,但官方插件示例(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
           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了第二种方法,因为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
           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

最后,我尝试了这个,它适用于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
               }
           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

我也有第四个解决方案,不使用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"
    }
}
Run Code Online (Sandbox Code Playgroud)

总而言之,我只是想知道哪个解决方案(当然还有另一个我没有在此列出的解决方案)是最好的?你建议哪一个?为什么?

干杯,奥利维尔

Oli*_*ier 7

所以。这是“EnvInject”插件的问题:https ://issues.jenkins-ci.org/browse/JENKINS-26583

如果您想保留 EnvInject,我上面的解决方法 #4 是正确的解决方案。

env.NODE_HOME="${tool 'Node 6.x'}"
env.PATH="${env.NODE_HOME}/bin:${env.PATH}"
sh 'npm -version'
Run Code Online (Sandbox Code Playgroud)

否则,如果可能的话,删除 EnvInject 插件也是一个很好的解决方案。