Approx 5 minutes read

Grunt is a task runner powered by Node.JS. It can be used to automate day to day mundane tasks. Being a Node.JS module, you can define tasks in good old JavaScript. Grunt has plenty of plugins available for different type of tasks. In this post I’ll walk you through the process of automating GIT work-flow using grunt-git. So let’s get started.

Setup / Installation

Grunt 0.4.x requires Node.JS version >= 0.8.0 and hence make sure you download and install the same.

Installing the Grunt CLI

First we need to install Grunt CLI globally using npm (Node.JS package manager) using following command:

1
npm install -g grunt-cli

This puts the grunt command in system path so that it can be invoked from any directory.

Preparing new Grunt project for GIT

We need to create two files for our project: package.json and Gruntfile.js.

package.json

package.json contains the dependencies and is used by npm to install the required dependencies. Create a package.json file with following content in it:

1
2
3
4
5
6
7
8
9
{
  "name": "your-project-name",
  "version": "1.0.0",
  "devDependencies": {
    "grunt": "^0.4.5",
    "matchdep": "~0.3.0",
    "grunt-git": "rubenv/grunt-git"
    }
}

You can also use npm-init command to create a basic package.json file. Now we will install Grunt locally by executing following command:

1
npm install

Gruntfile.js

Gruntfile.js is where we define our tasks. We will define out GIT add, commit and push task as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module.exports = function(grunt) {
  // Load all NPM grunt tasks
  require('matchdep').filterAll('grunt-*').forEach(grunt.loadNpmTasks);

  // Project configuration
  grunt.initConfig({

    // git add -A
    gitadd: {
      task: {
        options: {
          force: true,
          all: true,
          cwd: 'MyGitProjectRepo/'
        }
      }
    },

    // git commit -m "Repository updated on <current date time>"
    gitcommit: {
      task: {
        options: {
          message: 'Repository updated on ' + grunt.template.today(),
          allowEmpty: true,
          cwd: 'MyGitProjectRepo/'
        }
      }
    },

    // git push origin master
    gitpush: {
      task: {
        options: {
          remote: 'origin',
          branch: 'master',
          cwd: 'MyGitProjectRepo/'
        }
      }
    }
  });

  // Create task
  grunt.registerTask('git', [
    'gitadd',
    'gitcommit',
    'gitpush'
  ]);

  // Register default task
  grunt.registerTask('default', ['git']);

};

Note that we are dynamically generating the commit message in line 23. Also cwd parameter points to the local GIT repository directory. You need to modify this and point to your local GIT project repository.

Running Grunt tasks

To run the task, we need to simply issue following command:

1
grunt git

Grunt will run the tasks defined in Gruntfile.js and once it finishes, you will have your changes commited and pushed to remote GIT branch.