0

I have a job to push some values to consul based on user parameters and 2 values are generated when the pipeline is run like shutdown_date and termination_date:

def now, shutdown_date, termination_date
pipeline {

        parameters {
            string(name: 'env', defaultValue: 'abc')
            string(name: 'owr', defaultValue: 'abc')
            string(name: 'de', defaultValue: 'abc')
            string(name: 'tct', defaultValue: 'abc-123')
        }


        agent  { label 'abc' }
        stages {
            stage ('Update ENV'){
              steps {
                    script {
                        now = new Date()
                        println now.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))
                        shutdown_date = now + 170
                        shutdown_date = (shutdown_date.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))).trim()
                        println shutdown_date
                        termination_date = now + 365
                        termination_date = (termination_date.format("yyyy-MM-dd", TimeZone.getTimeZone('UTC'))).trim()
                        println termination_date
                        step([$class: 'ConsulKVBuilder', aclToken: '', apiUri: '', debugMode: 'DISABLED', envVarKey: 'env_status', hostUrl: '', key: 'env_status/${env_name}', keyValue: '{    "owr":"${owr}",    "de":"${de}",    "tct":"${tct}",    "shutdown_date": "${shutdown_date}",    "termination_date": "${termination_date}" }', requestMode: 'WRITE'])

                     }
                }
            }


        }
}

Expected result:

{ "owr":"abc", "de":"abc", "tct":"abc-123", "shutdown_date": "2020-02-15", "termination_date": "2020-08-15" }

Actual result:

{ "owr":"abc", "de":"abc", "tct":"abc-123", "shutdown_date": "${shutdown_date}", "termination_date": "${termination_date}" }

user312307
  • 153
  • 6
  • 21

1 Answers1

0

As mentioned in this answer, single-quoted strings won't interpolate variables.

You need to change your step to use double quotes and escape the ones in json.

step([$class: 'ConsulKVBuilder', aclToken: '', apiUri: '', debugMode: 'DISABLED', envVarKey: 'env_status', hostUrl: '', key: "env_status/${env_name}", keyValue: "{    \"owr\":\"${owr}\",    \"de\":\"${de}\",    \"tct\":\"${tct}\",    \"shutdown_date\": \"${shutdown_date}\",    \"termination_date\": \"${termination_date}\" }", requestMode: 'WRITE'])

String interpolation

edbighead
  • 5,607
  • 5
  • 29
  • 35
  • If I use the above one, I get below messages and it goes in loop..```2020-08-15 expected to call WorkflowScript.step but wound up catching org.jenkinsci.plugins.workflow.cps.CpsClosure2.call; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/``` – user312307 Aug 16 '19 at 13:16
  • And it is able to interpolate first 3 values, only for shutdown and termination, I see that issue – user312307 Aug 16 '19 at 13:28
  • your termination/shutdown variables are not in `yyyy-MM-dd` format, you only `println` them this way. try converting them – edbighead Aug 16 '19 at 13:51
  • Converted them and passed, still same issue. Updated the code above. – user312307 Aug 16 '19 at 14:18