2

I'm a newbie to jenkins/groovy and I'm lost at string interpolation.

We're trying to read a list of steps from a configuration file (stored in json format) and execute some actions bases on it in jenkins pipeline script.

Configuration file:

{
    "actions": [        {
            "operation": "create",
            "args": [
                { "path": "${env.SVNRoot}\\trunk\\ABC" },
                { "path": "${env.SVNRoot}\\trunk\\XYZ" }
            ]
        },      {
            "operation": "delete",
            "args": [
                { "path": "${env.SVNRoot}\\trunk\\ABC" },
                { "path": "${env.SVNRoot}\\trunk\\XYZ" }
            ]
        }
    ] }

Jenkins Pipeline code:

node('master') {

echo "${env.SVNRoot}" //String interpolation works here, giving the right value

stage('ReadConfig'){
  cfg = readJSON file: 'Cfg.json'
 }

stage('ExecuteConfigActions'){
 cfg.fileActions.each() {

 switch(it.operation) {
  case 'create':
   it.args.each() {

    echo it.path //String interpolation doesnt work here
    break;

    default:
    break;
    }
   }
  }
 }
}

How can I get string interpolation to work in such a scenario? Basically I want the environment variable value to be substituted in its placeholder and the path hence derived. I've tried single, double, escaped quotes to no avail.

Ash
  • 241
  • 4
  • 15
  • What would you like to accomplish with the value (because I am guessing that echo is just a placeholder for your real action)? Most jenkins actions/snippets accept a string as a variable (does not have to be interpolated). Also have you tried other forms of concatenation (like these: https://stackoverflow.com/questions/11359333/string-concatenation-with-groovy)? – jeubank12 Dec 17 '18 at 19:23
  • Well I actually intend to use the fileOperations plug-in for file and folder operations. It does accept string arguments but takes the environment variable placeholder as a raw string (instead of substituting the value) and fails – Ash Dec 18 '18 at 07:01
  • Oh, you want the strings in the configuration file to be re-interpolated? – jeubank12 Dec 18 '18 at 18:05

1 Answers1

0

The closest I can suggest is formulating a sprintf statement stored in the Configuration file and evaluated in your pipeline. Use at your own risk of someone being able to use the configuration file and substitute whatever they want. If you need to specify which environment variable in the config file and evaluate an interpolation still, check out this answer: evaluating a groovy string expression at runtime

Configuration file:

{
"actions": [        {
        "operation": "create",
        "args": [
            { "path": "%s\\trunk\\ABC" },
            { "path": "%s\\trunk\\XYZ" }
        ]
    },      {
        "operation": "delete",
        "args": [
            { "path": "%s\\trunk\\ABC" },
            { "path": "%s\\trunk\\XYZ" }
        ]
    }
] }

Pipeline code:

node('master') {

  echo "${env.SVNRoot}" //String interpolation works here, giving the right value

  stage('ReadConfig'){
    cfg = readJSON file: 'Cfg.json'
  }

  stage('ExecuteConfigActions'){
    cfg.fileActions.each() {

      switch(it.operation) {
        case 'create':
          it.args.each() {

            echo sprintf(it.path,env.SVNRoot)
          }
          break;

        default:
          break;
      }
    }
  }
}
jeubank12
  • 891
  • 9
  • 17