Resources Blog Nexus Pro: Automating Staging Workflow with Gradle using ...

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

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:

<b>$ gradle upload</b>
<b>$ gradle stage_list</b>
sample_profile-004, closed
sample_profile-005, closed
sample_profile-006, open
<b>$ gradle stage_close -DrepoId="sample_profile-006"</b>
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).


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.

Picture of Tim OBrien

Written by Tim OBrien

Tim is a Software Architect with experience in all aspects of software development from project inception to developing scaleable production architectures for large-scale systems during critical, high-risk events such as Black Friday. He has helped many organizations ranging from small startups to Fortune 100 companies take a more strategic approach to adopting and evaluating technology and managing the risks associated with change.