gitgruntjsgrunt-shell

Grunt-shell save command output as variable


I'm using Grunt and Grunt-shell to build/deploy my Javascript project.

I want to get the latest git-commit number and store it as a variable but can't figoure out how. I have tried having a callback and setting a global variable. This variable is usable within a function but not from within another block it seems

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
      },
...
}

Output:

>> 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.

Can anyone suggest how I might save the output of a command line to a variable which is usable please?


Solution

  • Found that I can actually do this using a config variable (instead of global) inside the callback. (Note below line also removes the newline).

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

    Then this can be accessed using:

    <%=gitCommitNo%>