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}" 

When GIT_BRANCH is equal to:

  • develop - returned value is dev - OK
  • master - returned value is list - OK
  • support/1.2 - returned value is sup - why not list?
2

1 Answer

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}" 

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.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy