title: Jenkins pipeline : select nodejs version (+ python version)
body: <p>I'm facing an issue with a Jenkins pipeline in a Jenkinsfile.
  I have 4 different nodeJs versions on my Jenkins instance. I would like to choose which one I'm going to use in my pipeline, but official plugin examples (<a href="https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin" rel="noreferrer">https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin</a>) simply don't work.</p>
  
  <p>I tried this first approach, failing because $PATH is overwritten by the <code>tools</code> section.</p>
  
  <pre><code>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
             }
         }
     }
  }
  </code></pre>
  
  <p>I tried this second approach, failing because the <code>tool</code> command seems to do nothing at all.</p>
  
  <pre><code>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
             }
         }
     }
  }
  </code></pre>
  
  <p>Finally, I tried this one, which works for NodeJS but... does not seem to be "very smart", and does not allow me to handle properly my specific version of "Python" --Yes I also have 2 different versions of Python that I would like to handle the same way I do for node--</p>
  
  <pre><code>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
                 }
             }
         }
     }
  }
  </code></pre>
  
  <p>I also have a 4th solution, not using <code>pipeline</code> syntax. It works for nodejs, but not for python (so far). And once again, it does not seems very elegant to manually define <code>env.PATH</code>...</p>
  
  <pre><code>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"
      }
  }
  </code></pre>
  
  <p>All in all, I'm just wondering which solution (certainly another one that I have not listed here) is the best? Which one do you advice and why?</p>
  
  <p>Cheers,
  Olivier</p>