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" }