add git commit hash to javascript variables during gulp build

I am using gulp to build my angularjs application and want to somehow inject the latest git commit hash into my angular app so I can use it when debugging production issues.

Even if it is just a case of injecting it into my index.html template as a global variable that would be great.

  • How can I get a bleeding edge version of angular.js from Github?
  • Guidelines to deploy a production MEAN Stack app
  • how to resolve this ERROR running one or more of the platform when i am runnning “ionic run android --device”
  • How to install angular-mocks via bower.json?
  • How to run “grunt build” during Heroku deployment (angular and ruby-on-rails app)
  • Nice Git architecture for server / client?
  • Can anyone recommend a good technique and plugins to achieve this?

  • Why can't a branch name contain the 'space' char?
  • git bash shell on Windows “Your edited hunk does not apply” (continued)
  • Gitlab installation based on gitolite fails
  • Detecting deletion of “files” in git
  • Alternatives to GitHub Pages?
  • How to set up git server WITHOUT ssh auth
  • 2 Solutions collect form web for “add git commit hash to javascript variables during gulp build”

    I ended up using git-rev-sync and gulp-replace and put {{git}} in my index.html source file.

    var git = require('git-rev-sync');
    
    return gulp.src(pathConfig.src + 'index.html')
        .pipe($.replace('{{git}}', git.long()))
        .pipe(gulp.dest(pathConfig.dest + 'index.html'));
    

    inject the latest git commit hash

    Here is how you can do it:

    git store the latest commit di inside the .git/refs/head<branch name>.
    Read this content of the file with the below code and then put it wherever you need

    # load fs for reading /writing files
    fs = require("fs"),
    
    // define your task 
    gulp.task('doSometing', function() {
    
      return gulp.src(dirs.src + '/templates/*.html')
    
        // read the  SHA-1 of the latest commit
        .pipe(fs.readFile("path/to/.git/refs/head<branch name>", 
    
              // Default encoding for fs is binary so we have to set it to UTF-8      
              "utf-8", 
    
              // Process the data you have read (SHA-1)
              function(err, _data) {
              //do something with your data
        })
       .pipe(gulp.dest('destination/path));
    });
    
    Git Baby is a git and github fan, let's start git clone.