0

I want to fill out a Dynamic Parameter box in Jenkins.

My Groovy script should do the following:

  1. Collect JSON output in a file.
  2. Parse JSON output in order to get some specific values .
  3. Shows those values in a list in Dynamic Parameter of Jenkins in order to choose one of them.

Can you help me with the Groovy script? No idea about Groovy :-(.

Thanks!

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
hero_xh
  • 25
  • 1
  • 6
  • Why do you want to do 1.? See [Groovy – Learn](http://groovy-lang.org/learn.html), [Groovy – Parsing and producing JSON](http://groovy-lang.org/json.html), [how to parse json using groovy](http://stackoverflow.com/q/6688090/1744774), etc. – Gerold Broser Feb 21 '17 at 13:57
  • I should run a curl command in order to get my json output file first and then I have to get some information from this json file. "jsonSlurper.parseText" support curl execution? my curl command has the following shape: curl -s ''$URL' -u $USERKEY – hero_xh Feb 21 '17 at 19:18

1 Answers1

2

You don't have to write the JSON data to a file to achieve this.

Taking Perfectly working curl command fails when executed in a groovy script and Parsing and producing JSON, 1. JsonSlurper as foundation do the following in a Dynamic Parameter's script:

import groovy.json.JsonSlurper

url= "http://user:pwd@jenkins/api/json"
process = [ 'bash', '-c',  "curl ${url}" ].execute()
process.waitFor()
//println process.err.text  // for debugging in Jenkins' Script Console
//println process.text
info = new JsonSlurper().parseText(process.text)
return info._class

Output at Build with Parameters:

This build requires parameters:

Dynamic Parameter hudson.model.Hudson

Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107