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?
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 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));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With