'Fill loaded variable with nested environment variable

I have a groovy script inside my pipeline that load some text from an json file. Now I want to combine this loaded text with existing environment variable, but it will not fill the placeholder. What I do:

json
{
  "resource": {
    "SERVICE_API_URL":"http://my-service-svc.${KUBERNETES_HOST}:5000"
  }
}

script {
  env.KUBERNETES_HOST = "myjenkins"
  env.JSONSTRING = readFile(file: 'pipeline/pipeline.config')
  def propsen = readJSON text: "${env.JSONSTRING}"

  env.test = propsen.resource.SERVICE_API_URL
  echo "${test}"
}

I hoped that this will echo "http://my-service-svc.myjenkins:5000" but it still echo "http://my-service-svc.${KUBERNETES_HOST}:5000"

So is there a trick how to do fill this "nested" variable in a dynamic loaded variable?



Solution 1:[1]

Try adding env to your variable in *.json file, like this:

{
  "resource": {
    "SERVICE_API_URL":"http://my-service-svc.${env.KUBERNETES_HOST}:5000"
  }
}

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 Moro