2017-03-06 82 views
0

我有一个Jenkins管道脚本。别名没有任何影响

在它,这个工程:

sh("/my/path/to/git status") 

但是,如果我尝试:

sh("alias git='/my/path/to/git' && git status") 

OR

sh("alias git='/my/path/to/git'") 
sh("git status") 

这些不工作:script.sh: line 2: git: command not found

我想使第二和第三段代码也可以工作。我怎样才能做到这一点?

回答

1

它不被授予连续的sh调用保持状态(包括环境变量)。

在项目中创建一个脚本,并把它在一个单一的sh指令,要么或使用:

sh """ 
    alias git='/my/path/to/git' 
    git status 
""" 
1

这些线

sh("alias git='/my/path/to/git'") 
sh("git status") 

创建子shell。第一个创建你的别名,然后立即退出。第二个启动时不知道以前的shell或其别名。

以前的版本

sh("alias git='/my/path/to/git' && git status") 

不会在本地交互shell工作,要么,即使&&被替换; - 明确别名根本不生效,直到当前命令列表的末尾。

如果您必须使用别名,则应将其添加到启动shell时源文件(.bashrc,.profile等)的任何一个。但请注意,除非您使用shopt -s expand_aliases,否则别名可能无法在非交互式shell中展开。

否则,通常的解决方案是将/my/path/to添加到您的$PATH

相关问题