git Jenkins 在远程机器上执行带有参数的 shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30052730/
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
Jenkins execute shell scripts with parameters on remote machine
提问by Swaroop Kundeti
I have a Jenkins job which executes a shell script on remote server. Now I would like to send some parameters along with the shell script. Below example might get you more idea.
我有一个 Jenkins 工作,它在远程服务器上执行一个 shell 脚本。现在我想和 shell 脚本一起发送一些参数。下面的例子可能会让你有更多的想法。
variable1={git-URL}
variable2={branch}
I want this parameters to be passed on to shell script which is on remote machine and should execute like below
我希望将此参数传递给远程计算机上的 shell 脚本,并应如下执行
#/usr/local/bin/check_out_code.sh <git-url> <branch>
Would be great if anyone can point me some work around for this
如果有人能为我指出一些解决方法,那就太好了
Thanks.
谢谢。
回答by Slav
You can reference any environment variable, including Jenkins parameters, as any other variable in linux: ${VARNAME}
您可以像 linux 中的任何其他变量一样引用任何环境变量,包括 Jenkins 参数: ${VARNAME}
回答by Janus Wen
I have the same problem with it.
我有同样的问题。
The parameters/${xx}/$xxx
are undefined in remote machine. So when your shell script executes in remote machine cannot get the correct value.
参数/${xx}/$xxx
在远程机器中未定义。所以当你的 shell 脚本在远程机器上执行时无法得到正确的值。
This way will fix the problem:
这种方式将解决问题:
ssh -t server.example.com 'export MYVAR='"'$LOCALVAR'"'; mycommand'
回答by Mohammad Azim
As mentioned by @Slav you can use environment/build arguments like any other environment variable in Jenkins. One thing to remember is that they get expanded before the script is executed. If you happen to be using pipeline groovy scripts, make sure you use double quotes "
instead of single '
:
正如@Slav 所提到的,您可以像 Jenkins 中的任何其他环境变量一样使用环境/构建参数。要记住的一件事是它们在脚本执行之前被扩展。如果您碰巧使用管道 groovy 脚本,请确保使用双引号"
而不是单引号'
:
following will not work:
以下将不起作用:
sh '/usr/local/bin/check_out_code.sh ${giturl} ${branch}'
following will work:
以下将起作用:
sh "/usr/local/bin/check_out_code.sh ${giturl} ${branch}"