Nexus Pro: Automating Staging Workflow with Gradle using the Nexus REST APIs


July 10, 2012 By Tim O'Brien

I recently had a request from a customer for some guidance on how to automate Staging in Nexus Professional from Gradle. Here was his core problem: he had a series of builds that needed to deploy to a staging URL and he was wondering if it was possible to automate the closing of a repository from Gradle. It is. While we’ve made it easy to do this in Maven with the Nexus Maven Plugin we didn’t have the equivalent example in Groovy. This post gives some guidance to anyone who needs to call out to our REST services from Groovy.

As Nexus Professional exposes every feature as a REST endpoint it is very easy to automate these interactions in just about any language. This sample demonstrates who to incorporate calls to Nexus REST APIs directly from your build. It also provides a model for parsing JSON responses from Nexus and posting JSON requests. If you are interested in more of these examples, please let us know in the comments of this post. (One thing is sure, this particular example could use some improvement, please be harsh.)

This example assumes that you are using a local instance of Nexus Professional running on port 8081. It also assumes that you are using the default administrative credentials. The workflow for this example would be someone staging a release, listing the staged repositories, and than closing one of those repositories. This example is a first cut, a simple example to demonstrate that integrating Nexus REST into your Gradle build is straightforward:

$ gradle upload
$ gradle stage_list
:stage_list
sample_profile-004, closed
sample_profile-005, closed
sample_profile-006, open
$ gradle stage_close -DrepoId="sample_profile-006"
:stage_close
Repository Closed

Here’s the sample Gradle build that demonstrates how to call out to Nexus REST services. First, note that stage_list and stage_close correspond to two in-line Gradle tasks StagingCloseTask and StagingListTasks. Both of these tasks simply set up authentication and the appropriate request headers. They then make the appropriate HTTP call to Nexus REST endpoints. StagingListTask shows you how to receive JSON from Nexus REST APIs and iterate through the results, StagingCloseTask shows you how to POST a JSON document (albeit very inelegantly, it does get the job done).

%CODE1%

Note: I didn’t use HTTP Builder in the example for a reason. While HTTP Builder would have made these examples smaller, I wanted to make sure that people had a sample Grade build they could use without having to worry about extra dependencies. This script works with the latest Gradle 1.0 release.

I’m also very aware that there are likely 100 different ways to write this script and this this isn’t the most elegant solution. The point of this post is to demonstrate the possible, expect more iterations as we continue to focus on the interoperability of Nexus to expand support beyond Maven to the rich variety of build tools our customers are using.

  • Brian Saville

    I’ve started a simple gradle plugin that uses the sample code given above and improves on it with build promotion, multiple tasks, parameterized url, username, and password, and filtering staged repositories by the username. See it here: https://github.com/adaptivecomputing/plugins-gradle/tree/master/nexus-workflow

    …or grab it from maven – groupId: com.adaptc.gradle, artifactId: nexus-workflow, version: 0.4