带有值列表的Groovy Switch语句

时间:2016-08-10 19:39:00

标签: groovy jenkins-pipeline

我想在Jenkins管道作业中使用Switch语句。

def version = "1.2"
switch(GIT_BRANCH) {
  case "develop":
    result = "dev"
    break
  case ["master", "support/${version}"]:
    result = "list"
    break
  case "support/${version}":
    result = "sup"
    break
  default:
    result = "def"
    break
}
echo "${result}"

GIT_BRANCH等于:

  • develop - 返回值为dev - 确定
  • master - 返回值为list - 确定
  • support/1.2 - 返回的值为sup - 为什么不list

1 个答案:

答案 0 :(得分:16)

我的猜测是frequency.c:19:2: error: invalid use of undefined type ‘struct _jobject’ char deletethis=(char)bits[0]; ^~~~ frequency.c:19:28: error: dereferencing pointer to incomplete type ‘struct _jobject’ char deletethis=(char)bits[0]; 的类型是GIT_BRANCH,而String"support/${version}"。如果您将后者转换为GString它应该有效:

String

两种字符串类型之间的差异在将它们相互比较时无关紧要,但它可能对其他类型的比较很重要,例如:在您的代码中,您隐含地将def version = "1.2" switch(GIT_BRANCH) { case "develop": result = "dev" break case ["master", "support/${version}".toString()]: result = "list" break case "support/${version}": result = "sup" break default: result = "def" break } echo "${result}" GString的元素进行比较。