Reputation: 14189
How to use the same node version
across multiple stages in a debian node
in jenkins?
node('debian') {
sh """
source ~/.nvm/nvm.sh &> /dev/null
nvm install 8 &> /dev/null
npm install yarn -g --silent
node --version # correct 8
yarn --version # correct 1.6.*
"""
timestamps {
ansiColor('xterm') {
stage('Prepare') {
sh """
node --version # wrong 6.11
yarn --version # wrong 1.12
"""
}
stage('Build') {
sh """
node --version # wrong 6.11
yarn --version # wrong 1.2
"""
}
}
}
}
Upvotes: 1
Views: 1403
Reputation: 13972
I reeaaallly hate to be that guy, but....
Have you considered the Jenkins Node.js Plugin? This plugin lets you declare multiple versions of Node.js Jenkins tools, and then gives you some pipeline syntax to select the Node version + even an npmrc file for when you need to specify registry or something.
Example snippet from Jenkinsfile:
pipeline {
agent any
steps {
nodejs(nodeJSInstallationName: 'MY_NODEJS_TOOL_NAME_HERE', configId: 'ID_OF_THE_CONFIG_FILE') {
sh "node --version" // should be correct
}
}
}
As to why your nvm isn't working, I suspect (but it's almost entirely speculation) that nvm
is playing games with your PATH
, but when the sh
block ends so does the cleverly reset path. Thus when you try to use your Node version in another sh
call, PATH
is back to what it was before.
Upvotes: 3