Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 655
the-a-train Avatar asked Jan 07 '16 10:01

the-a-train


2 Answers

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'));
like image 87
the-a-train Avatar answered Sep 20 '22 20:09

the-a-train


inject the latest git commit hash

Here is how you can do it:

git store the latest commit hash inside the .git/refs/heads/<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/heads/<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));
});
like image 45
CodeWizard Avatar answered Sep 20 '22 20:09

CodeWizard