【问题标题】:cut choice parameter value in executable shell script of jenkins在詹金斯的可执行shell脚本中剪切选择参数值
【发布时间】:2021-12-29 18:06:26
【问题描述】:

我在 Jenkins FreeStyle Job Type 中有一个选择参数。

选择变量 ${IP}:

192.168.1.33-prod
192.168.1.34-qa
192.168.1.35-stage

在可执行Shell脚本中,我想在将值分配给命令之前,删除所选选择参数中“-”之后的值。

执行的命令:

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/

【问题讨论】:

  • 只需使用groovy拆分值并提取IP:${params.IP.split('-').first()}
  • 你在 jenkins 工作中在哪里执行 grovy 脚本? @NoamHelmer
  • 看看Groovy Plugin,但你的 sh 分离命令也应该可以工作。你能进一步解释什么不起作用吗?
  • 您的问题在The Final Command before execution should look like: 之后缺少一些文字。 the value is coming empty when i try the below way: 之后的输出看起来完全正确。

标签: linux shell jenkins


【解决方案1】:

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

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

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

详情请见the bourne shell manual page

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多