简体   繁体   English

詹金斯:Active Choices参数+ Groovy基于REST响应对象构建列表

[英]Jenkins: Active Choices Parameter + Groovy to build a list based on REST responde

I have a REST client that returns me a list of systems. 我有一个REST客户端,可以向我返回系统列表。 I need this list to be as a parameter for a jenkins job. 我需要此列表作为詹金斯工作的参数。

I think I need Actice Choices Parameter plugin with Groovy and HTTPBuilder in order to do that. 我想我需要Groovy和HTTPBuilder的Actice Choices Parameter插件才能做到这一点。

What do you guys think? 你们有什么感想? I did not find a way to install HTTPBuilder into Jenkins. 我没有找到将HTTPBuilder安装到Jenkins中的方法。

Is there any other way that you guys think it is possible? 你们还有其他方法认为可行吗?

First paste the JSON body snapshot output -or whatever your REST client is going to return. 首先粘贴JSON主体快照输出-或您的REST客户端将要返回的任何内容。 That'll help. 那会有所帮助。

For ex: if it'll return a JSON object then you can use Active Choice Parameter's Groovy script step - OR Scriptler script (within the Active Choice Parameter plugin). 例如:如果它将返回JSON对象,则可以使用Active Choice Parameter的Groovy脚本步骤-或Scriptler脚本(在Active Choice Parameter插件中)。 PS: Scriptler script runs in the same JVM of Jenkins process so it has access to Jenkins/etc object for free. PS: Scriptler脚本在Jenkins进程的同一JVM中运行,因此它可以免费访问Jenkins / etc对象。 You don't need HTTPBuilder or anything. 您不需要HTTPBuilder或任何东西。 See the code sample below. 请参见下面的代码示例。

Assuming if your REST client is returning a JSON object and from that object if you want to list hostname of the system or some field name then replace the following variable with that and you'll get it listed while doing "Build with parameters" from Jenkins job's dashboard. 假设您的REST客户端正在返回JSON对象,并且如果您想列出系统的主机名或某个字段名 ,则从该对象返回该对象,然后用该变量替换以下变量,然后在执行Jenkins的“使用参数构建”时将其列出工作的仪表板。

import groovy.json.JsonSlurper

//this will be your URL which will return something, tweak it if you want to pass parameters or username/password acc.

def SOME_URL = "https://koba.baby.com/some_url"

// now connect to the URL and create a connection variable 'conn'
def conn = SOME_URL.toURL().openConnection()

// create a list variable 'servernames'
def servernames = []

// if connection response was successful i.e. http protocol return code was 200, then do the following
if( conn.responseCode == 200 ) {
    // get the results / output of the URL connection in a variable 'results'
    def results = new JsonSlurper().parseText(conn.content.text)

    // to see results variable output uncomment the next line
    //println results

    // now read each element in the 'results' variable and pick servername/somefield variable into the list variable 'servernames'
    results.each { id, data -> servernames.push(data.someField_or_HostName) }
}
return servernames.sort().unique()
// return servernames.sort()

I have run into the same problem trying to parse parameters via groovy script. 我试图通过groovy脚本解析参数时遇到了同样的问题。 Arun's answer did not work for me. 阿伦的答案对我不起作用。 However, I have managed to get it to work using the following: 但是,我设法使用以下方法使其工作:

import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.net.URL
import java.net.URLConnection
import groovy.json.JsonSlurper

def choices = []
def url = new URL("some.data.url")
def conn = url.openConnection()
conn.setDoOutput(true)

def reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))
def results = new JsonSlurper().parseText(reader.getText());
reader.close()

results.each { data -> choices.push(data.field) }
return choices.sort()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM