Using Grunt to deploy

This is a short post about how you can use Grunt to deploy code for you via rsync & also how to create a build process for tasks that should be run before deployment.

NOTE! I’ve only used this for deployment of small applications that I don’t mind too much about & am just looking to push often, I would certainly not recommend it any large, complex projects.

This post assumes you’ve used grunt before, so if you haven’t it’s best to go and check out the Getting Started guide before going any further.

The rsync task

There’s a great grunt-rsync plugin by Jed Richards that provides a task to run rsync via grunt so the first step is to install that:

$npminstallgrunt-rsync--save-dev

Now that the rsync plugin is installed it needs to be included in the Gruntfile & we need to set the task up:

'use strict';module.exports=function(grunt){grunt.initConfig({rsync:{options:{// these are my preferred arguments when using rsyncargs:['-avz','--verbose','--delete'],// an array of files you'd like to exclude; usual suspects...exclude:['.git*','cache','logs'],recursive:true},prod:{options:{// the dir you want to sync, in this case the current dirsrc:'./',// where should it be synced to on the remote host?dest:'/path/to/server/location',// what's the creds and hosthost:'[email protected]'}}}});grunt.loadNpmTasks('grunt-rsync');grunt.registerTask('default',['rsync:prod']);};

Most of these options are self-explanatory. You could also use the grunt-shell plugin to do this, but I find the rsync plugin to be easier to read & understand.

With that in place the task can be run like so:

$grunt

It’s as simple as that.

Build Process

Assuming the following tasks exist:

jshint

requirejs

sass

Adding the line of code below to the Gruntfile will create a deploy task that acts a build process & deployer.