Skip to content

Instantly share code, notes, and snippets.

@fizerkhan
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fizerkhan/83c3750769268e5d566d to your computer and use it in GitHub Desktop.
Save fizerkhan/83c3750769268e5d566d to your computer and use it in GitHub Desktop.
Generate version.json using git hash, version number, and date
npm install grunt-git-describe --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-git-describe');

Add Grunt configuration

grunt.initConfig({
  pkg: grunt.file.readJSON('package.json'),

  'git-describe': {
    options: {
    },
    dist: {}
  },
});
grunt.registerTask('saveRevision', function() {
    grunt.event.once('git-describe', function (rev) {
        grunt.option('gitRevision', rev.object);
    });
    grunt.task.run('git-describe');
});

grunt.registerTask('tag-revision', 'Tag the current build revision', function () {
    grunt.task.requires('git-describe');

    grunt.file.write('dist/version.json', JSON.stringify({
        version: grunt.config('pkg.version'),
        revision: grunt.option('gitRevision'),
        date: grunt.template.today()
    }, null, 2));
});
grunt.registerTask('version', ['saveRevision', 'tag-revision']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment