13

I build my webapp with gruntjs and yeoman.io.

I'd like to be able to record the git revision/commit/sha that a build come from, so that i can look in the deployed version and double check where it came from and what has changed with the new release.

Stephen
  • 4,228
  • 4
  • 29
  • 40

3 Answers3

13

Not a Grunt expert either, but here's a solution based on git describe which I currently use for a large AngularJS app. We store the major version in the project's package.json. In addition to that I generate a version.json file containing the revision and date for every build. This information can later be accessed by the client to help testers and maintainers to see which version/revision of the app they're using.

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

  'git-describe': {
    options: {
      prop: 'meta.revision'
    },
    me: {}
  },

  ...
});

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

  grunt.file.write('public/version.json', JSON.stringify({
    version: grunt.config('pkg.version'),
    revision: grunt.config('meta.revision'),
    date: grunt.template.today()
  }));
});

grunt.registerTask('version', ['git-describe', 'tag-revision']);

So by including the version task in our build tasks we can tag each build with a version.json file.

inukshuk
  • 1,419
  • 13
  • 9
6

Not a gruntjs specialist, but maybe you can include in your build step a call to the gruntjs-git-describe module, which will call that task:

module.exports = function( grunt ) {
  grunt.registerTask("describe", "Describes current git commit", function (prop) {
    var done = this.async();

    grunt.log.write("Describe current commit: ");

    grunt.util.spawn({
      cmd : "git",
      args : [ "describe", "--tags", "--always", "--long", "--dirty" ]
    }, function (err, result) {
      if (err) {
        grunt.log.error(err);
        return done(false);
      }

      grunt.config(prop || "meta.version", result);

      grunt.log.writeln(result.green);

      done(result);
    });
  });
};

Using git-describe is a good way to record a "version number" with Git, as it is SHA1-based (unambiguous id).
See more on that topic:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

To elaborate on Lucas Pottersky's comment to inukshuk's answer, the correct way to do the same thing with grunt-git-describe >= 2.2.0 is the following:

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

  'git-describe': {
    options: {
    },
    me: {}
  },

  ...
});

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

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

  grunt.file.write('public/version.json', JSON.stringify({
    version: grunt.config('pkg.version'),
    revision: grunt.option('gitRevision'),
    date: grunt.template.today()
  }));
});

grunt.registerTask('version', ['saveRevision', 'tag-revision']);
guidoman
  • 1,071
  • 2
  • 11
  • 20