8

JenkinsfileのJenkinsパイプラインに問題があります。 Jenkinsインスタンスに4つの異なるnodeJsバージョンがあります。パイプラインでどのプラグインを使用するかを選択したいのですが、公式のプラグインの例(https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin)は動作しません。Jenkinsパイプライン:select nodejs version(+ python version)

$ 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のために動作しますが...「非常にスマート」ではないようです、と私は「パイソン」の適切私の特定のバージョンを処理することはできませんこの1、--yesを試してみましたまた、私はpipeline構文を使用していない、私はまた、第四のソリューションを持っている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 
       } 
      } 
     } 
    } 
} 

のために私が同じように扱うしたいとのPythonの2つの異なるバージョンを持っています。 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

Soがこれは "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' 

そうしないと、可能であればEnvInjectプラグインを削除することも可能です。