简体   繁体   English

Jenkins无法访问shell别名

[英]Jenkins can't access shell alias

I have configured a Jenkins job to source a bash script that sources another bash script which adds an alias to the .bashrc of its user and sources the .bashrc itself, and then original script tries to use that alias (set by the second). 我已经配置了一个Jenkins作业,以获取一个bash脚本,该脚本可以获取另一个 bash脚本,该脚本为其用户的.bashrc添加一个别名并获取.bashrc本身,然后原始脚本尝试使用该别名(由第二个设置)。 However, it cannot seem to find the alias it has just created. 但是,它似乎无法找到它刚刚创建的别名。 I am not using any scripting plugins aside from using a "Send files or execute commands over SSH" build step to source the script. 我没有使用任何脚本插件,除了使用“通过SSH发送文件或执行命令”构建步骤来源脚本。

The job does this: 这项工作是这样的:

source ./test_script.sh

test_script.sh looks like this: 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

And finally, setup_env.sh looks like this: 最后, setup_env.sh看起来像这样:

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

The output I receive from the Jenkins job looks like this: 我从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

I don't understand why this is happening, when I can happily run it myself on the command-line and watch it succeed. 我不明白为什么会发生这种情况,当我可以在命令行上自己运行它并观察它是否成功时。 Why can't Jenkins use the new alias, when it can obviously find it (as demonstrated by the output of the alias foo command)? 为什么詹金斯不能使用新的别名,当它显然可以找到它时(如alias foo命令的输出所示)?

For anyone else who's having this problem, you may need to set the expand_aliases shell option, which seems to be off by default with Jenkins: 对于遇到此问题的其他任何人,您可能需要设置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

The \\n that is showing in the output of your echo commands echo命令的输出中显示的\\n
suggest this is not running under bash , as you may be expecting. 建议这不是在bash下运行,正如你可能期待的那样。

Please check the setting of your jenkins user 请检查您的jenkins用户的设置
(or any other user that you run Jenkins with) - (或您运行Jenkins的任何其他用户) -
especially the setting of the default shell . 特别是默认shell的设置。

To test this, add one of those lines at the beginning of your script: 要对此进行测试,请在脚本开头添加其中一行:

env

or 要么

env | grep -i shell

Should also consider making sure your scripts run under the correct shell, 还应该考虑确保你的脚本在正确的shell下运行,
by adding the " shebang " line as the first line in each script. 通过在每个脚本中添加“ shebang ”行作为第一行。
In the case of 'bash', for example, you should add the following first line: 例如,在'bash'的情况下,您应该添加以下第一行:

#!/bin/bash

(it is not a comment, despite what the auto-syntax-highlighter may think...) (这不是评论,尽管自动语法 - 荧光笔可能会想到......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM