2017-03-06 78 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

相關問題