【问题标题】:How to use groovy variable in jenkins sh command如何在 jenkins sh 命令中使用 groovy 变量
【发布时间】:2019-01-22 00:00:08
【问题描述】:

我正在尝试在我的 Jenkins 文件中使用一个 groovy 变量,如下所示

def since='2018-06-01';
count = sh(returnStdout: true, script: 'git rev-list --all --count master --since="${since}"').trim();

变量 since 不在 Jenkins 输出中进行评估。

+ git rev-list --all --count master --since=
[Pipeline] echo

看起来我在语法上做错了什么。有人可以帮忙吗?

【问题讨论】:

    标签: git shell jenkins groovy continuous-integration


    【解决方案1】:

    如果要将 Groovy 变量插入到字符串中,则必须使用双引号(单引号创建 String,而双引号创建 GString):

    count = sh(returnStdout: true, script: "git rev-list --all --count master --since=\\\"${since}\\\"").trim();
    

    另外,如果你想传递--since="2018-06-01",你必须像上面的例子一样转义"\

    更新后的代码产生以下 git 命令:

    + git rev-list --all --count master --since="2018-06-01"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2020-12-12
      • 2018-01-01
      • 1970-01-01
      相关资源
      最近更新 更多