8

我在Jenkins文件中面臨Jenkins管道的問題。 我在Jenkins實例上有4個不同的nodeJs版本。我想選擇在我的管道中使用哪一個,但官方插件示例(https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin)根本不起作用。Jenkins管道:選擇nodejs版本(+ python版本)

我試過這第一種方法,因爲$ 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 ...似乎並不「非常聰明」,並沒有讓我處理好我的「巨蟒」的具體版本 - 是的我也有2個不同版本的Python,我想處理同樣的方式我做node--

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 
        // /[email protected]/xx/script.sh: python27: not found 
       } 
      } 
     } 
    } 
} 

我也有一個4的解決方案,不使用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" 
    } 
} 

所有的一切,我只是想知道哪種方案(當然另外一個,我這裏沒有列出)是最好的?你建議哪一個?爲什麼?

乾杯, 奧利維爾

+0

嗨,我現在正在處理類似的問題。你有沒有想過別的什麼? – BumbleShrimp

+0

嗨。不幸的是,我的鋼鐵沒有比上面的更好的解決方案。我在插件上打開了一張票:https://issues.jenkins-ci.org/browse/JENKINS-43066 – Olivier

回答

1

所以。這是從「EnvInject」插件的問題:https://issues.jenkins-ci.org/browse/JENKINS-26583

我的解決方法#4上面是正確的解決方案,如果你想保持EnvInject。

env.NODE_HOME="${tool 'Node 6.x'}" 
env.PATH="${env.NODE_HOME}/bin:${env.PATH}" 
sh 'npm -version' 

否則,刪除EnvInject插件也是當一個很好的解決方案成爲可能。