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:

$ npm install grunt-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) {
        rsync: {
            options: {
                // these are my preferred arguments when using rsync
                args: ['-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 dir
                    src: './',
                    // where should it be synced to on the remote host?
                    dest: '/path/to/server/location',
                    // what's the creds and host
                    host: 'user@hostname'


    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.

grunt.registerTask('deploy', ['jshint', 'requirejs', 'sass', 'rsync:prod']);

Now running

grunt deploy

Will run all the specified tasks and then rsync the code. The handy thing about this is that if any of the tasks before rsync fail, the deployment won’t happen.


blog comments powered by Disqus

About ParkJi


My name is Ben Parker, I'm a 31 year old front end web developer with a passion for web design, standards & new & innovative technologies.