Upen
Upen

Reputation: 1438

Jenkins Pipeline - Switch node version dynamically

Is there a way we can set NodeJS version dynamically in Jenkins Pipeline. I am not able to make available nvm as well inside the pipeline.

sh 'export NVM_DIR=~/.nvm'
sh 'source ~/.nvm/nvm.sh

script.sh: line 2: nvm: command not found

None of this helped. Multiple teams using the pipeline need specific version of nodeJS. Earlier with non-pipeline jobs, this used to work using nvm.

Upvotes: 1

Views: 4684

Answers (1)

edbighead
edbighead

Reputation: 6334

You can create a choice parameter and use it in tools declaration.

pipeline {
    agent any
    parameters { 
        choice(name: 'NODE_VERSION', choices: ['NodeJS 9.6.1', 'NodeJS 7.7.0'], description: '') 
    }

    tools {
        nodejs params.NODE_VERSION
    }

    stages{
       stage("Run"){
            steps{
                sh 'node --version'
            }
        }
   }
}

Upvotes: 4

Related Questions