Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

installing node on jenkins 2.0 using the pipeline plugin

I am running the following docker image jenkinsci/jenkins:2.0-rc-1 to try out jenkins 2.0, and the "pipeline" view.

I can't seem to install node. Here's my pipeline script:

node {
    //tool([name: 'node-5.10.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'])
    sh 'echo $(whoami)'
    sh 'node -v'
}

The response when this runs is:

[ci] Running shell script
+ whoami
+ echo jenkins
jenkins
[Pipeline] sh
[ci] Running shell script
+ node -v
/../durable-3b0b1b07/script.sh: 2: /../durable-3b0b1b07/script.sh: node: not found

Here's what i've tried:

  • the jenkins NodeJS tool (which works correctly when used with a freestyle job)

  • logging into the docker container and installing node manually, for the same user:

enter image description here

UPDATE:

Building on Jesse Glick's answer below, i added the result to my scripts PATH:

node { 
    def nodeHome = tool name: 'node-5.10.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
    env.PATH = "${nodeHome}/bin:${env.PATH}"
    sh 'npm install'
}
like image 472
royse41 Avatar asked Apr 12 '16 13:04

royse41


3 Answers

Either

node {
  withEnv(["PATH+NODE=${tool name: 'node-5.10.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'}/bin"]) {
    sh 'node -v'
  }
}

or

node {
  def nodeHome = tool name: 'node-5.10.1', type: 'jenkins.plugins.nodejs.tools.NodeJSInstallation'
  sh "${nodeHome}/bin/node -v"
}

should work. See JENKINS-28718 for further proposals.

By the way you can omit the type parameter and just use

tool 'node-5.10.1'

for brevity.

like image 60
Jesse Glick Avatar answered Nov 13 '22 08:11

Jesse Glick


For me work next code:

node(){
  def nodeHome = tool 'nodejs5'
  env.PATH="${env.PATH}:${nodeHome}/bin"
  ...
  sh 'npm install'
}

nodejs5 is the name of the tool specified in Jenkins configuration.

like image 31
Andrey Prokopiev Avatar answered Nov 13 '22 08:11

Andrey Prokopiev


If anyone happens to deal with this issue on Jenkins running on Windows. Do the following:

def nodeHome = tool 'Node.js 6.9.5'
bat "\"${nodeHome}\"\\node.exe -v"
bat "\"${nodeHome}\"\\npm -v"
like image 37
Glogo Avatar answered Nov 13 '22 08:11

Glogo