bash 如何在 Jenkins 管道脚本中使用 source 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38664765/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:57:06  来源:igfitidea点击:

How to use source command within Jenkins pipeline script

linuxbashshelljenkinsjenkins-pipeline

提问by Neil

I recently rewrite bash execution command into Jenkins pipeline. The old code is like

我最近将 bash 执行命令重写到 Jenkins 管道中。旧代码就像

...
source environment.sh
//Build
//Test
...

Now I use pipeline script to wrap the command, like this

现在我使用管道脚本来包装命令,就像这样

sh '''
    ...
    source environment.sh
    //Build
    //Test
    ...
'''

However, I got an error, as.../.jenkins/script.sh: line 9: source: environment.sh: file not found. When I try to less environment.sh, it display correctly. So I suspect something wrong with source commandwithin sh wrap.

但是,我遇到了一个错误,因为.../.jenkins/script.sh: line 9: source: environment.sh: file not found. 当我尝试时less environment.sh,它显示正确。所以我怀疑source command里面有问题sh wrap

Before using pipeline, source environment.shcommand is working fine in shell execution. So source is install at Jenkins server, it seems pipeline script don't know what is the source command.

在使用管道之前,source environment.sh命令在 shell 执行中工作正常。所以源代码安装在 Jenkins 服务器上,似乎管道脚本不知道源命令是什么。

How could I do to run source command within sh wrapped block?

我怎么能在 sh 包裹的块中运行 source 命令?

回答by Steephen

Replace source environment.shwith

替换source environment.sh

. ./environment.sh

Please note there is a space after first dot.

请注意第一个点后有一个空格。

回答by chepner

sourceis a bash/ksh/etc extension, provided as a more "substantial" synonym for ..

source是一个bash/ ksh/etc 扩展,作为..

In sh, you need to use .in case the underlying shell is one (such as dash) that does not support the command source.

在 中sh,您需要.在底层 shell 是一个(例如dash)不支持该命令的情况下使用source

sh '''
    ...
    . environment.sh
    //Build
    //Test
    ...
'''