Grunt-shell 将命令输出保存为变量

Grunt-shell save command output as variable

我正在使用 Grunt 和 Grunt-shell 来 build/deploy 我的 Javascript 项目。

我想获取最新的 git-提交编号并将其存储为变量,但不知道如何操作。 我试过回调并设置全局变量。这个变量在一个函数中可用,但在另一个块中似乎不可用

grunt.initConfig({
...
shell: {
      getGitCommitNo: {
        command: 'git rev-parse --short HEAD',
        options: {
          callback: function (err, stdout, stderr, cb) {
              global['gitCommitNo'] = stdout;
              grunt.log.ok(global.gitCommitNo);
              cb();
            }
        }
      },
      philTest: {
         command: 'echo Git Commit No: ' +  global.gitCommitNo
      },
...
}

输出:

>> Starting deployment process for version 1.1 in dev environment

Running "shell:getGitCommitNo" (shell) task
bfc82a9
>> bfc82a9

Running "shell:printTest" (shell) task
Git Commit No: undefined

Done, without errors.

谁能建议我如何将命令行的输出保存到可用的变量中?

发现我实际上可以在回调中使用配置变量(而不是全局变量)来做到这一点。 (注意下面的行也删除了换行符)。

grunt.config.set('gitCommitNo', stdout.replace('\n', '')); 

然后可以使用以下方式访问:

<%=gitCommitNo%>