ruby RVM 和 Jenkins 设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10209242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RVM and Jenkins setup
提问by Zeck
I am new to Jenkins CI. I'm install RVM in my remote Jenkins and when I execute below shell.
我是 Jenkins CI 的新手。我在我的远程 Jenkins 中安装了 RVM,当我在 shell 下执行时。
#!/bin/bash -x
source ~/.bashrc
rvm use [email protected]
I get following errors.
我收到以下错误。
+ source /var/lib/jenkins/.bashrc
++ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/var/lib/jenkins/.rvm/bin:/var/lib/jenkins/.rvm/bin
+ rvm use [email protected]
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal settings to allow shell login.
Please visit https://rvm.io/workflow/screen/ for example.
What does it mean? I don't have any idea. Please help me.
这是什么意思?我不知道。请帮我。
UPDATED:I'm tried below script but I still get errors:
更新:我在脚本下面尝试过,但我仍然遇到错误:
#!/bin/bash -x
source /home/zeck/.bashrc
[[ -s ".rvmrc" ]] && source .rvmrc
export RAILS_ENV=test
bundle install
Errors:
错误:
/tmp/hudson457106939700368111.sh: line 5: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Jenkins build shell can't detect RVM, gemsets and gems. What should I do?
Jenkins 构建 shell 无法检测到 RVM、gemsets 和 gems。我该怎么办?
UPDATED 2:Therefore jenkins can't detect ruby.
更新 2:因此 jenkins 无法检测到 ruby。
+ ruby -v
/tmp/hudson2505951775163045158.sh: line 5: ruby: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILUR
I'm not using any jenkins plugn and I'm just run script from Build->Execute shell section.
我没有使用任何 jenkins 插件,我只是从 Build->Execute shell 部分运行脚本。
采纳答案by mpapis
try:
尝试:
. $(/home/RVM_USER/.rvm/bin/rvm env [email protected] --path)
make sure you run the stable RVM:
确保您运行稳定的 RVM:
rvm get stable
NOTE: Last Jenkins version does not always accept "source", but ".". RVM_USER is the user that installed RVM. Alternatively you can also export the RVM command in the main PATH.
注意:最后一个 Jenkins 版本并不总是接受“源代码”,而是“.”。RVM_USER 是安装 RVM 的用户。或者,您也可以在主 PATH 中导出 RVM 命令。
回答by disrupt
As the error message suggests, RVM expects an login shell. Changing the hashbang line to #!/bin/bash -xlshould resolve this.
正如错误消息所暗示的那样,RVM 需要登录 shell。将 hashbang 行更改为#!/bin/bash -xl应该可以解决此问题。
回答by everyday productive
Yes, apparently you miss the $HOME/.rvm/binin your PATH. I am using rvm successfully from Hudson on Mac OS X. First thing to notice is that, unless you define BASH_ENVenvironment variable (ENVfor sh), .bashrcis called automatically only with interactive non-login shell. Such a shell is started when you do - for example - the following from the command line:
是的,显然你错过了$HOME/.rvm/bin你PATH。我在 Mac OS X 上从 Hudson 成功使用了 rvm。首先要注意的是,除非您定义BASH_ENV环境变量(ENV用于 sh),否则.bashrc仅使用交互式非登录 shell 自动调用。例如,当您从命令行执行以下操作时,将启动此类 shell:
$ /bin/bash
When you use #!/bin/bashin your script, .bashrcwill not be called.
当你#!/bin/bash在你的脚本中使用时,.bashrc不会被调用。
To make rvm work with Hudson, I have the following in my .bash_profile:
为了让 rvm 与 Hudson 一起工作,我在我的文件中有以下内容.bash_profile:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Thanks to [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"I have rvm enabled every time I start new terminal window (interactive, login shell).
感谢[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"我每次启动新的终端窗口(交互式登录外壳)时都启用了 rvm。
I do not put anything in my .bashrc, especially I am not sourcing rvm scripts there. Nothing wrong with that, but if any other scripts makes something stupid like setting `export BASH_ENV=$HOME/.bashrc' and then invoke non-interactive shell, you see what may happen - it is actually easy to forget.
我没有把任何东西放在我的.bashrc,特别是我没有在那里采购 rvm 脚本。这没什么不对,但是如果任何其他脚本做出了一些愚蠢的事情,例如设置 `export BASH_ENV=$HOME/.bashrc' 然后调用非交互式 shell,您就会看到可能发生的情况 - 实际上很容易忘记。
Therefore, instead of loading things to your .bashrc, it is better to keep your script independent from any shell startup file and make sure that the correct environment is set up within the script. I still keep $HOME/.rvm/binin my .bash_profile, but then I include the following at the beginning of my script:
因此,与其将内容加载到您的 .bashrc 中,不如让您的脚本独立于任何 shell 启动文件,并确保在脚本中设置了正确的环境。我仍然保留$HOME/.rvm/bin在我的.bash_profile,但随后我在脚本的开头包含以下内容:
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
rvm use 1.9.3-head@MyGemSet
set -ex
cucumber # just an example
Notice the -eoption which forces the script to exit with error code if any command following set -exfails. This is behavior you may want when using the script with Hudson.
It is incorrect to say that RVM expects a login shell. Although using #!/bin/bash -lin your script will work, it does not seem like the best approach.
请注意-e如果以下任何命令set -ex失败,该选项会强制脚本退出并显示错误代码。这是在 Hudson 中使用脚本时您可能想要的行为。说 RVM 需要登录 shell 是不正确的。尽管#!/bin/bash -l在您的脚本中使用会起作用,但它似乎不是最好的方法。
回答by Mahattam
Just add this code in your shell script, i think rvm is loading from your source so it should work else need to export PATH variable
只需将此代码添加到您的 shell 脚本中,我认为 rvm 正在从您的源加载,因此它应该可以工作,否则需要导出 PATH 变量
#!/bin/bash -l
source ~/.bashrc
rvm use [email protected]
l is for login shel, if you include x then it would be for debugging too.
l 用于登录 shell,如果包含 x,那么它也用于调试。
回答by Gregory Ostermayr
adding a shebang to the build commands in jenkins fixed this for me
在 jenkins 的构建命令中添加一个 shebang 为我解决了这个问题
#!/usr/bin/env bash
rvm use 2.0.0
bundle install
rake test
...
回答by Ethan
Jenkins nodes don't load paths the same way, so it's not using the proper path to find rvm's version of ruby. You can set the path for a given agent.
Jenkins 节点不会以相同的方式加载路径,因此它没有使用正确的路径来查找 rvm 的 ruby 版本。您可以为给定代理设置路径。
- Find your current PATH by doing
echo $PATH - Assuming you've set up rvm properly, find where rvm's version of ruby is located by running
which ruby - There's a setting in the Configuration of your agent where you can set environmental variables. Set PATH to be 1 and 2 concatenated.
- 通过执行找到您当前的路径
echo $PATH - 假设您已经正确设置了 rvm,请通过运行找到 rvm 的 ruby 版本所在的位置
which ruby - 代理的配置中有一个设置,您可以在其中设置环境变量。将 PATH 设置为 1 和 2 连接。

