'Want to parse multiple key=value string to Map and then serialize it but map to string serialization fails

I am converting the key=value string Groovy Map. Then after some changes , returning back the serialize object. Example String --> dlpxDcTags = "OWNER=test,PROJECT=test2,COSTCENTER=1234,TEAM=devops"

parseDcenterTags(dlpxDcTags){
  Map tags = [:]
  tags += dlpxDcTags.replaceAll('\\[|\\]', '').split(',').collectEntries { entry ->
    def pair = entry.split('=')
    [(pair.first().trim()): pair.last().trim()]
    return tags
  }
}

def createDcenterTags(dlpxDcTags=null) {
  // Values passed from the environment of the user takes precedence
  tags = parseDcenterTags(dlpxDcTags)
  if (tags) {
    if (!(tags.get('PROJECT'))) {
        tags['PROJECT'] = env.JOB_NAME
      }

    if (!tags.get('OWNER')) {
      // not supplied via dlpxDcTags
      tags['OWNER'] = env.BUILD_USER_EMAIL
      }
    }
  else {
    tags['PROJECT'] = env.JOB_NAME
    tags['OWNER'] = env.BUILD_USER_EMAIL
  }
  return serializeDcenterTags(tags)
 }

def serializeDcenterTags(tags){
  dlpxDcTags = {
    tags.collect { /$tags.key="$tags.value"/ } join ","
  }
  return dlpxDcTags
}

Facing issue:

  • The variable when called from main jenkins job using this helper script is getting value “org.jenkinsci.plugins.workflow.cps.CpsClosure2@64ed2e4b”


Solution 1:[1]

After lot of reading and trying different ways , the very simple thing which worked for me was doing the serialization in the same method instead of defining and calling it from another method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepali Mittal