Grunt deploy, fabric-style

October 3, 2013

Fabric is a great little deployment gem from the Python world. It enables you to define simple tasks aimed towards a smooth deployment process. What I like the most is that it is all about bash commands: tar -xvf ..., mv .., cp ... It is not exactly ‘low-level’, but it doesn’t hide anything in abstract wrapper functions either.

Although ..

There are some functionalities (operations) that abstract away some of the regular tasks.

In comes Grunt-fabric-deploy

Heavily inspired on the concept of Fabric I build grunt-fabric-deploy. It contains the same sort of no-nonsense operations and functionality, but wrapped in a descriptive style and as a Grunt plugin.

Install the plugin via npm

$ npm install grunt-fabric-deploy

Create your Gruntfile

module.exports = function(grunt) {
  grunt.initConfig({

    deploy: {
      options: {
        stdout: true,
        failOnError: true,
        variables: {
          packed: 'filename'
          host: 'server.com',
          port: 22,
          user: 'deploy_user'
        }
      },

      pack: {
        local: '/usr/bin/gnutar -zcf /tmp/{packed}.tar.gz -C ./ .'
      },

      upload: {
        put: {
          src: '/tmp/{packed}.tar.gz',
          dest: '/tmp/{packed}.tar.gz'
        },
        run: [
          'rm -rf /tmp/{packed}',
          'mkdir -p /tmp/{packed}',
          'tar -zxf /tmp/{packed}.tar.gz -C /tmp/{packed}',

          'rm -rf /srv/www/server.com/public',
          'mkdir -p /srv/www/server.com/public',
          'mv /tmp/{packed}/* /srv/www/server.com/public/'
        ]
      }
    }

  });
}

Result of a ‘grunt deploy’

recorded via asciinema

Configuration

More information can be found on the Github page of grunt-fabric-deploy.

Discussion, links, and tweets