Jenkins 管道:选择 nodejs 版本(+ python 版本) [英] Jenkins pipeline : select nodejs version (+ python version)

查看:188
本文介绍了Jenkins 管道:选择 nodejs 版本(+ python 版本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Jenkinsfile 中遇到了 Jenkins 管道问题.我的 Jenkins 实例上有 4 个不同的 nodeJs 版本.我想选择在我的管道中使用哪一个,但官方插件示例(https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin) 根本不起作用.

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 (https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin) simply don't work.

我尝试了第一种方法,但失败了,因为 $PATH 被 tools 部分覆盖.

I tried this first approach, failing because $PATH is overwritten by the tools section.

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 命令似乎什么也没做.

I tried this second approach, failing because the tool command seems to do nothing at all.

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,我想以与 node 相同的方式处理--

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--

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
               }
           }
       }
   }
}

我还有第四个解决方案,不使用 pipeline 语法.它适用于nodejs,但不适用于python(到目前为止).再一次,手动定义 env.PATH...

I also have a 4th solution, not using pipeline syntax. It works for nodejs, but not for python (so far). And once again, it does not seems very elegant to manually define 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"
    }
}

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

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?

干杯,奥利维尔

推荐答案

所以.这是来自EnvInject"插件的问题:https://issues.jenkins-ci.org/browse/JENKINS-26583

So. This is a problem from "EnvInject" plugin: https://issues.jenkins-ci.org/browse/JENKINS-26583

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

My workaround #4 above is the correct solution if you want to keep EnvInject.

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

否则,尽可能删除 EnvInject 插件也是一个不错的解决方案.

Otherwise, removing EnvInject plugin is also a good solution when possible.

这篇关于Jenkins 管道:选择 nodejs 版本(+ python 版本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆