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.

Can anyone recommend a good technique and plugins to achieve this?

  • How to use git for setting up a component based project
  • Deploy with git to dokku on digitalocean
  • Git debug custom merge driver
  • How do I include a .gitignore file as part of my npm module?
  • Should I stop node server before a git push? Can I update node on Openshift?
  • Using NodeJS, Bower and GulpJS in a project, what should I exclude from my Git repository?
  • Installing ungit on Windows XP error: C:/Documents: No such file or directory
  • npm and git command lines are very slow on Windows; how can I sort out why?
  • 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.