【问题标题】:Can you record the git revision with gruntjs你能用 gruntjs 记录 git 的修订吗
【发布时间】:2012-10-29 22:40:56
【问题描述】:

我使用 gruntjs 和 yeoman.io 构建我的 webapp。

我希望能够记录构建来自的 git 修订/提交/sha,以便我可以查看已部署的版本并仔细检查它的来源以及新版本的变化。

【问题讨论】:

    标签: git gruntjs


    【解决方案1】:

    也不是 Grunt 专家,但这里有一个基于 git describe 的解决方案,我目前用于大型 AngularJS 应用程序。我们将主要版本存储在项目的 package.json 中。除此之外,我还生成了一个 version.json 文件,其中包含每个构建的修订版和日期。客户稍后可以访问此信息,以帮助测试人员和维护人员查看他们正在使用的应用程序的版本/修订版。

    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']);
    

    因此,通过在我们的构建任务中包含版本任务,我们可以使用 version.json 文件标记每个构建。

    【讨论】:

    • 有趣。比我自己的答案更详细。 +1
    • 这不适用于我或我的 npm 版本组合。这是未解决的问题:github.com/mikaelkaron/grunt-git-describe/issues/10
    • 只是旁注:git-describe v2.2.0“删除了对回调和道具的支持(改用 grunt.event.emit)”
    【解决方案2】:

    不是 gruntjs 专家,但也许您可以在构建步骤中包含对 gruntjs-git-describe 模块的调用,该模块将调用 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);
        });
      });
    };
    

    使用 git-describe 是用 Git 记录“版本号”的好方法,因为它是基于 SHA1(明确的 id)的。
    查看有关该主题的更多信息:

    【讨论】:

      【解决方案3】:

      要详细说明 Lucas Pottersky 对 inukshuk 的回答的评论,使用 grunt-git-describe >= 2.2.0 执行相同操作的正确方法如下:

      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']);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-05
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        相关资源
        最近更新 更多