How to populate Choice Parameter with artifact information using Nexus REST API?

Issue

  • I would like to populate a Choice Parameter drop-down with artifact information from Nexus, using the Nexus REST API. This is an alternative approach to using the Repository Connector Plugin. Some customers have reported having issues using this plugin. I would investigate using this plugin first before reading on.
  • Optionally, Id like to use the Choice selection in Pipeline.

Environment

  • CloudBees Jenkins Enterprise
  • Pipeline (Optional)
  • Nexus REST API
  • Extensible Choice Parameter Plugin
  • Repository Connector Plugin (Alternative)

Resolution

First, add the Extensible Choice and select “System Groovy Choice Parameter” - This build is parameterized - Extensible Choice - System Groovy Choice Parameter

Now lets focus on the Nexus REST API query.

The query can be broken up into the following pieces:

${NEXUS_BASE_URL}/service/local/lucene/search?g=${GROUP_ID}&a=${ARTIFACT_ID}&r=releases&p=${TYPE}

Modify these ${} pieces to suit your needs.

For example - https://repository.jboss.org/nexus/service/local/lucene/search?g=jboss&a=jboss-j2ee&r=releases&p=jar will return artifact information for group id jboss, artifact id jboss-j2ee and type jar, from the JBoss Nexus Repository https://repository.jboss.org/nexus/. You can test the query in your browser to see the XML results returned by the API.

Now we are going to use some simple Groovy to query the API using the HTTP query we just described, extract the results and populate the drop-down. The code goes in the Groovy System Script field that the System Groovy Choice Parameter provides.

The following example creates a drop-down list of artifact versions:

def xml = "https://repository.jboss.org/nexus/service/local/lucene/search?g=jboss&a=jboss-j2ee&r=releases&p=jar".toURL().text

def root = new XmlParser().parseText(xml)

return root.data.artifact.collect {
  it.version.text()
}

To explain this in more detail:

The first line defines and runs the HTTP query, saving the XML results as text.

The second line uses Groovy’s built in XmlParser to parse the XML.

The third line traverses the XML, collecting the artifact version from each artifact, finally calling return which populates the drop-down with the resulting collection.

The following example extends on the first, creating a drop-down list of artifacts, each item containing group id, artifact id and version, formatted using colon ( :) separation:

def xml = "https://repository.jboss.org/nexus/service/local/lucene/search?g=jboss&a=jboss-j2ee&r=releases&p=jar".toURL().text

def root = new XmlParser().parseText(xml)

return root.data.artifact.collect {
  "${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}"
}

Pipeline Integration (Optional)

Let’s assume that when setting up a new parameter using the Extensible Choice Parameter Plugin that you gave the parameter you created the Name test. This name is important for referencing the drop-down value selected in your Pipeline.

Below is a usage example:

node {
    // example simply prints the drop-down value selected when triggering the build.
    echo "value ${test}"
}

Below is an advanced usage example, putting everything together in order to download an artifact to the current directory.

Imagine in this example that test holds the artifact version value 4.2.3.GA:

node {
    sh "curl -O https://repository.jboss.org/nexus/service/local/repositories/releases/content/jboss/jboss-j2ee/${test}/jboss-j2ee-${test}.jar"
}
Have more questions? Submit a request

5 Comments

  • 0
    Avatar
    Buddi Hemanthkumar

    Hi i tried this procedure , got an exception. Really appreciate your help on this. 

    and we have groupid,artifactid,version as well. 

     

    Failed to execute script

    java.io.FileNotFoundException: https://nexus.com/mother/content/repositories/snapshots/search?g=com.aper.rims&a=rims-scheduler&r=releases&p=xml
    Edited by Buddi Hemanthkumar
  • 0
    Avatar
    Denys Digtiar

    Hi Buddi,

    The fetching of the URL content failed. The path section is wrong. You need to figure out if you are using the right URL. You can try to open it in the browser to see if it resolves to the expected thing. If it does, it is likely that your local environment is different from Jenkins, e.g. corporate DNS or Proxy servers.

  • 0
    Avatar
    Buddi Hemanthkumar

    Hi,

    Denys Digtiar 

     

    Thanks for reply, Am able to fix the issue, and am able to populate the artifacts in choice parameter.

    I have other question, script worked fine with maven files, but i have other repository with .7z files , can you please help me to populate those files to choice parameter 

     

     

    Example : groupid- 

    groupid- com.apri.portfolio
    artifactid- pt-ap
    version: 17.01


  • 0
    Avatar
    Tigran Mnatsakanyan

    Hi guys, is there an easy way to modify the "Accept" header to use application/json and configure basic authentication for the rest call below?

    $YOUR_NEXUS_URL.toURL().text

    Seems like the httpBuilder is not available within pipeline and i could not find toUrl() examples with custom hearders and basic auth.

     

  • 0
    Avatar
    Vijay Deshmukh

    I altered the URL to suit our needs and it just does not work. When i checked the network traffic in chrome i see in most cases the url includes "service/extdirect" and do not see the "service/local/lucene". Should it not be the same? I am using Repo Manager Nexus 3.x

Please sign in to leave a comment.