skip to Main Content

I’ve launched Jenkins to my Virtual machine agent. My virtual machine is Ubuntu 20.04. When I run node -v on VM, I get v16.20.1. But when I run this pipeline in Jenkins:

pipeline {

    agent {
        label 'ubuntu-server'
    }
    stages {
        stage('Hello World') {
            steps {
                sh '/home/jenkins/.nvm/versions/node/v16.20.1/bin/node -v' //1
                sh 'node -v' //2
            }
        }
    }
}

The first "sh" command returns node version, but the second gives an error message:

+ /home/jenkins/.nvm/versions/node/v16.20.1/bin/node -v
v16.20.1
+ node -v
/home/jenkins/jenkins_slave/workspace/forgejo-app@tmp/durable-90f345a5/script.sh: 1: node: not found

I thought it was a problem with PATH variable, so I found this solution:

pipeline {

    agent {
        label 'ubuntu-server'
    }
    stages {
        stage('Hello World') {
            steps {
                withEnv( ["PATH=/home/jenkins/.nvm/versions/node/v16.20.1/bin/node:$env.PATH"]){
                    sh 'echo $PATH'
                    sh 'node -v'
                }
            }
        }
    }
}

But it’s non helping and I still get the same error:

+ node -v
/home/jenkins/jenkins_slave/workspace/forgejo-app@tmp/durable-a9e9321a/script.sh: 1: node: not found

I’ve also checked what is inside of durable-a9e9321a/script.sh file and it’s empty.

2

Answers


  1. Chosen as BEST ANSWER

    I've changed my pipeline like this and it's working now: pipeline {

    agent {
        label 'ubuntu-server'
    }
    environment{
        PATH = "/home/jenkins/.nvm/versions/node/v16.20.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
    }    
    stages {
        stage('Hello World') {
            steps {
                sh 'node -v'
                sh 'npm -v'
                sh 'pwd'
                sh 'go version'
                cleanWs()
            }
        }
    }
    

    }


  2. Your Jenkins executor might not have the authorization to run such command. I can’t give you more details since I work mainly on Windows but try using sudo node -v.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search