Jenkins在远程计算机上使用参数执行shell脚本

时间:2022-04-10 01:12:24

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.

3 个解决方案

#1


You can reference any environment variable, including Jenkins parameters, as any other variable in linux: ${VARNAME}

您可以将任何环境变量(包括Jenkins参数)引用为linux中的任何其他变量:$ {VARNAME}

#2


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'

#3


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}"

#1


You can reference any environment variable, including Jenkins parameters, as any other variable in linux: ${VARNAME}

您可以将任何环境变量(包括Jenkins参数)引用为linux中的任何其他变量:$ {VARNAME}

#2


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'

#3


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}"