在jenkins的可执行shell脚本中剪切choice参数值



我在Jenkins FreeStyle作业类型中有一个选择参数。

变量${IP}的选择:

192.168.1.33-prod
192.168.1.34-qa
192.168.1.35-stage

在可执行外壳脚本中,我想删除"之后的值-"在选定的选择参数中,然后将值指定给命令。

执行的命令:

rsync --owner=ec2-user --group=ec2-user -O --no-p  -arzh --exclude ".git/" --perms --chmod=a+rwx /tmp/some-value/ ec2-user@${IP}:/some-folder/

Linux命令是:

echo ${IP} | cut -f1 -d"-"

结果应该是结果:

192.168.1.33

执行前的最终命令应该看起来像:

然而,当我尝试以下方式时,该值变为空:

rsync --owner=ec2-user --group=ec2-user -O --no-p  -arzh --exclude ".git/" --perms --chmod=a+rwx $WORKSPACE/ ec2-user@192.168.1.33:/some-folder/

问题可能与重新分配IP变量值的步骤有关——问题中缺少该步骤。

但是,在您的情况下,使用shell参数扩展而不是cut可能更为优雅。使用%%,您可以在替换时删除最长的匹配模式,所以这应该可以做到:

rsync [...] ec2-user@${IP%%-*}:/some-folder/

有关详细信息,请参阅bourneshell手册页面。

最新更新