【问题标题】:Jenkins can't access shell aliasJenkins 无法访问 shell 别名
【发布时间】:2013-08-03 20:50:16
【问题描述】:

我已经配置了一个 Jenkins 作业来获取一个 bash 脚本,该脚本获取 另一个 bash 脚本,该脚本将别名添加到其用户的 .bashrc 并获取 .bashrc 本身,然后是原始脚本尝试使用该别名(由第二个设置)。但是,它似乎找不到它刚刚创建的别名。除了使用“通过 SSH 发送文件或执行命令”构建步骤来获取脚本之外,我没有使用任何脚本插件。

这项工作是这样做的:

source ./test_script.sh

test_script.sh 看起来像这样:

echo "In test_script.sh"
echo $USER
echo $HOME
source ./setup_env.sh
echo "\nBack in test_script.sh"
alias foo
foo

最后,setup_env.sh 看起来像这样:

echo "\nIn setup_env.sh"
echo "alias foo=\"echo foobar\"" >> $HOME/.bashrc
source $HOME/.bashrc 2>/dev/null
cat $HOME/.bashrc

我从 Jenkins 工作中收到的输出如下所示:

In test_script.sh
my_user
/home/my_user
\nIn setup_env.sh
...all of my bashrc...
alias foo="echo foo"
\nBack in test_script.sh
alias foo='echo foo'
./test_script.sh: line 7: foo: command not found

我不明白为什么会发生这种情况,当我可以自己在命令行上愉快地运行它并看到它成功时。为什么 Jenkins 不能使用新别名,而它显然可以找到它(如 alias foo 命令的输出所示)?

【问题讨论】:

标签: bash jenkins hudson


【解决方案1】:

\n 显示在您的 echo 命令的输出中
建议这不是在bash 下运行,正如您所期望的那样。

请检查您的 jenkins 用户的设置
(或与您一起运行 Jenkins 的任何其他用户)-
尤其是默认shell的设置。

要对此进行测试,请在脚本开头添加以下行之一:

env

env | grep -i shell

还应考虑确保您的脚本在正确的 shell 下运行,
通过添加“shebang”行作为每个脚本的第一行。
例如,在 'bash' 的情况下,您应该添加以下第一行:

#!/bin/bash

(这不是评论,尽管自动语法高亮器可能会认为...)

【讨论】:

    【解决方案2】:

    对于遇到此问题的其他人,您可能需要设置 expand_aliases shell 选项,该选项似乎在 Jenkins 中默认关闭:

    shopt expand_aliases # check if it's on
    shopt -s expand_aliases # set expand_aliases option to true
    shopt expand_aliases # it should be on now
    
    # test with a simple alias, should print 1
    alias x="python -c 'print 1'"
    x
    

    【讨论】:

    • 我自己无法找到此解决方案。非常感谢!
    猜你喜欢
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2021-11-29
    相关资源
    最近更新 更多