Architect, Technologist, and gamer.

VP of Technology, Authenticom

Automate Nuget pack and push using Grunt and Node.js

Automate Nuget pack and push using Grunt and Node.js

Automation is fun, even when its simple. I try to automate as much as I can in my professional and personal life, if it makes sense. Even if it saves you five minutes in a work day, that's over 1.5 hours of time you get back every month. So in this case, automating the build and publish of the few nuget packages I maintain will allow me to spend that captured time somewhere else. Granted the return of the initial investment needs to make sense as well. Unless your like me and often just do things to learn them, then it's time well spent regardless.

Now, there are a lot of ways to automate a Nuget pack and publish. Traditionally, I've done most of my automation with my C# projects using msbuild and the Nuget.exe against a .csproj file. Use that method in combination with a build server like TeamCity or TFS and you have a very nice reliable way of automating your package builds in your own CI environment. My goals in this case however are a bit different. I wanted something that was portable and could run on the command line cross platform, preferably without any external dependencies. Grunt, being a hugely popular JavaScript task automation library, fits that bill perfectly. It's used in many popular open source projects on Github and is really quite useful for any tasks that can benefit from cross platform automation. It also has a very active community of plugin contributors to extend the system well beyond its base capabilities. For the most part, any task you can build in JS and can run in Node, you can automate with Grunt.

To get started with grunt on Windows, you may need to do a few things to get your environment ready. First, install Node.js. Easiest ways are to grab the installer from the website or if you have Chocolaty installed, run cinst nodejs.install from the command line. Finally you'll need to install the Grunt CLI tools via the Node Package Manger (npm). From the command prompt type npm install -g grunt-cli which will install the cli tools globally and add it to your system path. For more in depth details on getting started with Grunt, see the docs here http://gruntjs.com/getting-started.

Package.json

The package.json file will define your build dependencies via the devDependencies property. This is technically not required, but its very useful for quickly adding the grunt modules needed for your build via npm install. This will also keep you from committing the node_modules directory, which, just like with the nuget packages directory, is generally not something you want to do.

Gruntfile.js

The Gruntfile is essentially your build definition. This is where you'll define and register all the tasks in your build. In this example, I'm defining two tasks, the default task and the publish task. The aptly named default task is what will run by default when you execute the grunt command, which is equivalent to grunt default. The second task publish is nearly identical to the default task with the addition of the nugetpush configuration.

All in all, this is a very simple set of tasks, which is exactly what I was after. I'm utilizing the grunt-contrib-clean module to wipe the dist directory prior to the build. I'm then using the grunt-shell module to execute the mkdir command ensuring the dist directory exists. Followed finally by the grunt-nuget module which will allow me to both pack and push the nuget package. If you dig into the node_modules directory, you'll find that the grunt-nuget module is utilizing Nuget.exe and has wrapped most of its arguments for pack and push into the options parameter. As an alternative to this module, you could easily just include your own version of the Nuget.exe and use the shell module to accomplish the same thing. I personally think the module makes it a little cleaner since I won't have to carry the Nuget.exe in my repo, but everyone's requirements are different. Also, in the case of Nuget.exe, Mono would be required if running this on a non-windows platform.

Make it so

cmd screenshot

Taking back the cloud - Part 2: Plex is your next media server

Taking back the cloud - Part 2: Plex is your next media server

Why and how I'm taking back the cloud - Part 1 : Setup

Why and how I'm taking back the cloud - Part 1 : Setup