NVM 不适用于 Jenkins execute shell

NVM is not working on Jenkins execute shell

我正在尝试从 Jenkins 安装和使用 nvm 在 Ubuntu 服务器上执行 shell 脚本,但我收到此错误:

16:00:21 /tmp/hudson5983664925305072739.sh: line 8: nvm: command not found

这是我迄今为止尝试过但没有成功的方法:

#!/bin/bash

touch ~/.profile && source ~/.profile;
nvm current || echo "SSH NVM is being installed" &&  touch ~/.profile && curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh -o install_nvm.sh && bash install_nvm.sh && source ~/.profile

echo "checking nvm"
bash ~/.nvm/nvm.sh;
nvm --version || exit 1;

Jenkins 执行shell截图:

您需要记住,Jenkins 是 运行 非交互式命令 shell,因此 PATH 与普通用户的路径不同。解决此问题的一种方法是使用绝对路径调用 nvm。

添加这些解决了问题:

. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc

没有完全得到我在这里寻找的答案,但两者都让我找到了明显的解决方案,甚至在 nvm 的自述文件中。该路径不在 Jenkins 的 shell 脚本中,因此找不到可执行文件。

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

要弄清楚你的 $HOME 是什么,你可以 运行 echo $HOME。 例如 $HOME 可能看起来像这样

export NVM_DIR="/var/lib/jenkins/.nvm"

在努力使上述建议生效后,我尝试了 NodeJS Jenkins 插件,它非常有效。

https://plugins.jenkins.io/nodejs/