1
votes

I have a Jenkins job that spits out a json file with a bunch of values. Specifically, this file contains TCP port numbers from a virtual machine created using the Jenkins plugin.

I need to grab these port numbers from the json file and use their values in an ant build that I'll call as the next step of the jenkins job.

The json file looks somewhat like this:

{
  "vms": [{
    "interfaces": [{
      "services": [{
        "internal_port": 25,
        "external_port": 12345,
        "id": "smtp"
      },
      {
        "internal_port": 80,
        "external_port": 12346,
        "id": "http"
      }]
    }]
  }]
}

So I need to set a parameter for my ant script from this json to ${http.external.port} and ${smtp.external.port} properties.

Is is doable from a separate jenkins task? Do I have to do this within the same ant script? I prefer doing from a separate jenkins step because this same ant script is used from other jobs that don't generate the json file (I'm getting the parameters from a static .properties file).

1

1 Answers

1
votes

What you want is:

  • Run a shell build step (OS dependent) to parse the JSON file for those values.
  • Store those values in environment variables.

Unfortunately those only persist during that build step, but you need to re-use them in the next. One solution is to temporary store those values into a file, and then read that file in your next build step (Ant). Ant can easily ready properties from file, as long as they are in property=value format.

You can also read those same variable, and inject them into Jenkins environment variables for the build, using EnvInject plugin. This will go further in allowing you to reference those values as environment variables, not just in Ant.

As for how to parse the JSON for the values: you need to provide what OS you are running on and what scripting languages you are familiar with.