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

Viewed 42

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”
1 Answers

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.

Related