Home > Enterprise >  nvm: command not found with Jenkins job
nvm: command not found with Jenkins job

Time:12-23

I am running a Jenkins job to kick off a deployment. This Jenkins job kicks off a Jenkins file within the app its trying to deploy. While its running it cannot seem to find nvm to continue. I found a couple cases on here.. mostly with sourcing out the .nvm location, but that doesnt seem to work for me. I can log onto the Jenkins server and verify nvm is installed and does work... with both my account and when I sudo in as proxy. Here is my latest Jenkinsfile script up to where it fails:

 def environment = ['develop', 'test', 'production']

 pipeline {
   agent {label 'APP-01'}

      stages {

        stage("Checkout") {
          when {anyOf {branch 'develop'; branch 'PR-*' } }
           steps {
             checkout scm
           }

       stage("Dependencies - npm installs") {
          when {anyOf {branch 'develop'; branch 'PR-* } }
            steps {
               sh 'pwd' 
               sh 'echo $PATH'
               sh '. ~/.nvm/nvm.sh'
               sh 'nvm list'

Jenkins Console Output:

Starting with failed section...

 [Pipeline] { (Dependencies - npm installs)
 [Pipeline] sh
   pwd
 /data/1/apps/jenkins/workspaces/workspace/jects_APP01_Repos_app-ui_develop

 [Pipeline] sh
   export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/proxy/.nvm
   PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/proxy/.nvm
 [Pipeline] sh
   . /home/proxy/.nvm/nvm.sh
    '[' '!' -d '' ']'
      dirname /home/proxy/.nvm/nvm.sh
     cd /home/proxy/.nvm
     pwd
    export NVM_DIR=/home/proxy/.nvm
    NVM_DIR=/home/proxy/.nvm
    '[' -x '' ']'
    nvm ls default
    nvm use default
    '[' 2 -lt 1 ']'
     nvm_version default
      nvm_ls default
      local PATTERN=default
       cd /home/proxy/.nvm
      echo N/A
      return
    VERSION=N/A
    echo N/A
     '[' N/A= N/A ']'
     return
    VERSION=N/A
    echo 'N/A version is not installed yet'
    return 1
    true
 [Pipeline] sh
   nvm list
 /data/1/apps/jenkins/workspaces/workspace/jects_APP01_Repos_app-ui_develop@tmp/durable-2d17a47f/script.sh: line 1: nvm: command not found

CodePudding user response:

I think that every time you run an sh command, it's as if you were opening a new terminal window. As a result, if you add the nvm location to your path with one sh step, your path will be changed only for that shell session, and when you run another sh command it will create a new session and use the old path.

A way to work around this is to use a single sh step for sourcing and using nvm, like this:

sh script: '''\
  pwd
  echo $PATH
  . ~/.nvm/nvm.sh
  nvm list'''.stripIndent()
  • Related