Jenkins – dynamic choice parameter from Nexus repository

Embarrassingly, it took me couple of days to get it sorted but finally managed to get what i wanted.

Lets start from the problem:

Have Nexus repositories with maven artifacts and would like to configure Jenkins job to look into the repo metadata and retrieve dropdown list with available versions. 

Option 1:
Use Extensible Choice parameter from the Add Parameter menu and select System Groovy Choice Parameter. Then add the script below, click on Groovy Sandbox and theoretically this should work – well it works for Artifactory repository and only with files being available under Artifactory local  storage – for remote repositories, i.e Nexus remote repo, this did not work for me:


import groovy.json.JsonSlurper
def sortableVersion(v) {
    v.tokenize(‘.’).collect({ it.padLeft(10, ‘0’) }).join()
}

 def content = new URL(“//http://<artifactory_host>:<port>/artifactory/api/storage/libs-release-local/<dir>/”).text

def jsonObj = new JsonSlurper().parseText( content )
[“-“] + jsonObj.children

  .findAll { it.folder }
  .findAll { it.uri =~ /\/\d+\.\d+\.\d+/ }
  .collect { it.uri[1..-1] }
  .sort { a, b -> sortableVersion(a) <=> sortableVersion(b) }
  .reverse()


 

Option 2:

Follow steps from the link below to identify correct Nexus URL and search classifiers:

/search?g=jboss&a=jboss-j2ee&r=releases&p=jar"

https://support.cloudbees.com/hc/en-us/articles/217958928-How-to-populate-Choice-Parameter-with-artifact-information-using-Nexus-REST-API-

This options unfortunately failed on me too due to Jenkins Script Security Plugin being enabled and current outstanding bug blocking “toURL” class to work. The groovy class does not appear on the list to be approved by the Admin in Jenkins making it almost impossible to skip. I did not give it too much though as there always has to be some other way to get what you want without too much hacking involved.

Option 3:

There is a plugin called Repository Connector

https://wiki.jenkins.io/display/JENKINS/Repository+Connector+Plugin

It adds Maven Repository Artifact to the drop down menu of the parameters. This one started quite a promising. However joy did not last too long. After quite some time of scratching my head it was coming back with error: artefact versions cannot be found. And rightly so, it did connect to Nexus but, the plugin itself expected pom file with metadata that contains all the versions in the repository and Nexus does not create one in this case. Instead i had metadata xml file which in no way could be read by the plugin.

Do then when the bright light shined above my head and found…

Option 4:

Maven Metadata Plugin for Jenkins CI server!!! ok – maybe it is not so exciting and very pretty but does the job for sure. It basically reads Nexus repository file and return values. It even has an option of descending the numbers and limit drop down menu to maximum versions to display. On top of that it can be nicely dressed with Artefact Info Label and few more filter options. So it looks great and works pretty well for my needs. The parameter config is quite long  which causes an overhead of scrolling down if you have many values to choose from in your job, but nevertheless it does what it says on the package so job well done!!!

https://wiki.jenkins.io/display/JENKINS/Maven+Metadata+Plugin

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>