本文介绍了带有值列表的 Groovy Switch 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Jenkins 流水线作业中使用 Switch 语句.
I want to use Switch statement in Jenkins pipeline job.
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
- OKmaster
- 返回值为list
- OKsupport/1.2
- 返回值为sup
- 为什么不是list
?
develop
- returned value isdev
- OKmaster
- returned value islist
- OKsupport/1.2
- returned value issup
- why notlist
?
推荐答案
我的猜测是 GIT_BRANCH
的类型是 String
而 "support/${version}"
是一个 GString
.如果您将后者转换为 String
它应该可以工作:
My guess is that the type of GIT_BRANCH
is a String
whereas "support/${version}"
is a GString
. If you convert the latter to a String
it should work:
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
与 List
的元素进行比较.
The difference between the two string types doesn't matter when comparing them to each other, but it can matter for other types of comparison, e.g. in your code you're implicitly comparing a GString
with the elements of a List
.
这篇关于带有值列表的 Groovy Switch 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!