Open pvinis opened 5 years ago
I experimented the same issue when i needed to use nvm
in a Jenkins pipeline for a project with .nvmrc
file.
My first attempt was:
stage('Install Node.js') {
steps {
script {
sh '''
[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh" --install
'''
}
}
}
After digging i found:
nvm.sh
does not "see" the arguments, and it always runs in "use" mode ($#
always equals 0)./bin/sh
/bin/sh
is a symbolic link to dashI can reproduce the issue with the following steps:
.nvmrc
file, create a tmp.txt
file with the following content [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use
/bin/sh -xe tmp.txt
I changed the shell used to run my Jenkins step with bash and it works:
stage('Install Node.js') {
steps {
script {
sh '''#!/bin/bash -xe
[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh" --install
'''
}
}
}
BTW, I tried the workaround of temporarily renaming .nvmrc
, and it also worked.
When I source
nvm.sh
like. "$HOME/.nvm/nvm.sh"
for manual install and like. $(brew --prefix nvm)/nvm.sh
, the exit code is3
.Even though nvm works after that, in bash scripts with
set -e
, the script stops there, since the command exits with a non-zero value.Is there a reason this fails? Is there a reason it exits with
3
, if it's not an actual failure? What can we do about that?Some extra info:
output of
. nvm.sh
withset -x
for "tracing": https://pastebin.com/raw/1Tk9UUspOperating system and version: macOS 10.14.2 (mojave)
nvm debug
output:Details
nvm ls
output:Details
How did you install
nvm
? (e.g. install script in readme, Homebrew): Tried both. I mostly do from brew though, since it's easier.What steps did you perform? source the file
What happened? worked, but exits with non-zero value
What did you expect to happen? to exit with a 0 value since everything seemingly went well
Is there anything in any of your profile files (
.bashrc
,.bash_profile
,.zshrc
, etc) that modifies thePATH
? I ran this without any $PATH alterations. In general I don't have anything in there that touch nvm. Only to includebrew
stuff.