Automating the Choice Parameter on a job

35 views
Skip to first unread message

Kari Cowan

unread,
Oct 15, 2020, 6:01:51 PM10/15/20
to jenkins...@googlegroups.com
I am working on a Jenkins job to rollback a Docker container to the previous version.  This on its own is pretty straight-forward, but what I want to do is get my list of available tags from the registry. As opposed to going to the registry-page and seeing what's available, then copy/paste a value into a form on Jenkins, I'd like to automate that step somehow - by grabbing the data from the registry page and displaying the options in a choice parameter in Jenkins on the build screen.

For example, call in the registry url by ip, example, 10.11.12.199:5000/v2/some-api/tags/list might display some JSON like:
{"name":"some-api","tags":["qa_api-11","qa_api-12","qa_api-13"]}

What's the best tool for this?  I'm no Groovy expert but would the Extensible Choice Parameter plugin coupled with a Groovy script be useful here? I'm not really clued up yet if this is possible in Groovy, but am open to suggestions.  Does anyone have any tips on that or other methods that are better?


Jan Monterrubio

unread,
Oct 16, 2020, 12:32:33 AM10/16/20
to jenkins...@googlegroups.com
I’ve used the extended choice parameter to pull a list of files from a directory in github and display a set of checkboxes. I’ll remember to grab you a snippet tomorrow during work! 



--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CAE7w4hg_RpwfVYAh7m9FLc1oxCPZQuqwd-eMpj6kFgBdMsJoOA%40mail.gmail.com.

janmont...@gmail.com

unread,
Oct 16, 2020, 4:14:44 PM10/16/20
to Jenkins Users
 import groovy.json.JsonSlurperClassic
 def jsonSlurper = new JsonSlurperClassic()
def response = jsonSlurper.parse(new URL("SOME REPO HERE"))
return response

It was something like that ^

You can always start with a "return [ 'a', 'b'] " , we used the ActiveChoice reactive parameter

Kari Cowan

unread,
Oct 19, 2020, 10:27:28 AM10/19/20
to jenkins...@googlegroups.com
This is clever - thanks.

I don't have a directory of files in my git project- but I am guessing I could write something to add - like a deployment tag list that matches the docker registry?

Ideally I would pull the docker register tag list and use that.


Jan Monterrubio

unread,
Oct 19, 2020, 7:36:09 PM10/19/20
to jenkins...@googlegroups.com
Yep, if docker tags can return you json, you can just slurp stuff and transform it. 

Kari Cowan

unread,
Oct 19, 2020, 8:07:45 PM10/19/20
to jenkins...@googlegroups.com
Nice. That’s a good tip. Thanks 😊 

Kari Cowan

unread,
Oct 22, 2020, 5:26:14 PM10/22/20
to jenkins...@googlegroups.com
I couldn't get that to work right but got a little but further using the JsonSlurper (as opposed to JsonSlurperClassic)

Assuming my data structure is represented in json like:

{
  "name": "templates",
  "tags": [
    "qa_tmpl-92",
    "qa_tmpl-93",
    "qa_tmpl-94"
  ]
}

I was trying something like this:

import groovy.json.JsonSlurper
try {
    List<String> artifacts = new ArrayList<String>()
    def artifactsUrl = "http://localhost:5000/v2/templates/tags/list"          
    def artifactsObjectRaw = ["curl", "-s", "-H", "accept: application/json", "-k", "--url", "${artifactsUrl}"].execute().tags
    def jsonSlurper = new JsonSlurper()
    def artifactsJsonObject = jsonSlurper.parseText(artifactsObjectRaw)
    def dataArray = artifactsJsonObject.data
    for(item in dataArray){
        if (item.isMetadata== false)
        artifacts.add(item.tags)
    }
    return artifacts
} catch (Exception e) {
    print "There was a problem fetching the artifacts"
}

Running in groovysh, it didn't show me an error, but I ended up in the catch, so I think the way I am trying to do curl, this bit in particular (${artifactsUrl}"].execute().tags) is wrong.

There was a problem fetching the artifacts ===> null


Can anyone point me in the right direction here?

Danny Trunk

unread,
Oct 22, 2020, 6:01:16 PM10/22/20
to jenkins...@googlegroups.com
Hello Kari

As far as I know JsonSlurper isn't Serializable and defining it for re-use will raise a NotSerializableException and this end up in the catch block.

You could either move the Code insider a @NonCPS annotated method or doing everything in one statement.

Kari Cowan

unread,
Oct 23, 2020, 12:30:02 PM10/23/20
to jenkins...@googlegroups.com
Not sure, I was following the example detailed here, https://medium.com/@rijoalvi/jenkins-dynamic-parameters-using-extended-choice-parameter-plugin-and-groovy-1a6ffc41063f

But my data model isn't as complex, so I think the way I am doing it - I need to change how to get the data I want.  I just want a list of the tags to return in the choice parameter.

Reply all
Reply to author
Forward
0 new messages