hero_xh
hero_xh

Reputation: 35

Groovy & Jenkins - capture curl output to a file

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!

Upvotes: 0

Views: 9115

Answers (1)

Gerold Broser
Gerold Broser

Reputation: 14762

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

Upvotes: 2

Related Questions