Wikunia Wikunia - 2 months ago 7
Git Question

Babel not working in git hook

I want to babel some project files after pushing on the server.
Therefore I use a

post-receive
hook which calls the following line

/usr/bin/gulp babel --branch $branch --path $i


where $branch is
devel
and $i is
server/server.js


This is my gulp file:

var gulp = require('gulp'),
gprint = require('gulp-print'),
notify = require('gulp-notify'),
babel = require('gulp-babel'),
gWatch = require('gulp-watch');
var argv = require('yargs').argv;


gulp.task('default', ['watch-js', 'watch-all']);

// run with gulp babel --branch devel --path server/server.js
gulp.task('babel', function() {
console.log('argv path: ',argv.path);
console.log('argv branch: ',argv.branch);
if (argv.path.indexOf('server/') == 0) {
return gulp.src(argv.path, { base: './' })
.pipe(babel())
.pipe(gulp.dest('../wikaike-'+argv.branch+'-deployed'))
.pipe(gprint(function(filePath){ return "File processed: " + filePath; }));
} else {
return;
}
});


If I run the command this way I get this output:

[18:35:11] Using gulpfile /var/www/wikaike-devel/gulpfile.js
[18:35:11] Starting 'babel'...
argv path: server/server.js
argv branch: devel
[18:35:15] File processed: ../wikaike-devel-deployed/server/server.js
[18:35:15] Finished 'babel' after 4.36 s


but if I push I get this one:

remote: [18:47:20] Using gulpfile /var/www/wikaike-devel/gulpfile.js
remote: [18:47:20] Starting 'babel'...
remote: argv path: server/server.js
remote: argv branch: devel
remote: abort: 0��� ������$0������
remote:
remote: ==== Stack trace ============================================
remote:
remote: Security context: 0x3733462d <JS Object>#0#
remote: 1: /* anonymous */ [/var/www/wikaike-devel/node_modules/is-utf8/is-utf8.js:~2] (this=0x373346d5 <JS Global Object>#1#,bytes=0x57528e71 <a Buffer>#2#)
remote: 2: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/strip-bom/index.js:9] (this=0x37308091 <undefined>,arg=0x57528e71 <a Buffer>#2#)
remote: 3: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/lib/src/getContents/bufferFile.js:11] (this=0x37308091 <undefined>,err=0x37308081 <null>,data=0x57528e71 <a Buffer>#2#)
remote: 4: /* anonymous */ [fs.js:266] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>)
remote: 5: /* anonymous */ [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/graceful-fs/graceful-fs.js:102] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>)
remote: 7: oncomplete [fs.js:107] (this=0x575292e9 <an Object>#3#)
remote: 8: arguments adaptor frame: 1->0
remote:
remote: ==== Details ================================================
remote:
remote: [1]: /* anonymous */ [/var/www/wikaike-devel/node_modules/is-utf8/is-utf8.js:~2] (this=0x373346d5 <JS Global Object>#1#,bytes=0x57528e71 <a Buffer>#2#) {
remote: // stack-allocated locals
remote: var i = 11629
remote: // expression stack (top to bottom)
remote: [02] : 0
remote: [01] : 2019638
remote: --------- s o u r c e c o d e ---------
remote: function (bytes)?{? var i = 0;? while(i < bytes.length)? {? if( (// ASCII? bytes[i] == 0x09 ||? bytes[i] == 0x0A ||? bytes[i] == 0x0D ||? (0x20 <= bytes[i] && bytes[i] <= 0x7E)? )? ) {? ...
remote:
remote: -----------------------------------------
remote: }
remote:
remote: [2]: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/strip-bom/index.js:9] (this=0x37308091 <undefined>,arg=0x57528e71 <a Buffer>#2#) {
remote: // expression stack (top to bottom)
remote: [02] : 0x57528e71 <a Buffer>#2#
remote: [01] : 0x373346d5 <JS Global Object>#1#
remote: [00] : 0x44fecad1 <JS Function>#4#
remote: --------- s o u r c e c o d e ---------
remote: function (arg) {??if (typeof arg === 'string') {???return arg.replace(/^\ufeff/g, '');??}???if (Buffer.isBuffer(arg) && isUtf8(arg) &&???arg[0] === 0xef && arg[1] === 0xbb && arg[2] === 0xbf) {???return arg.slice(3);??}???return arg;?}
remote: -----------------------------------------
remote: }
remote:
remote: [3]: /* anonymous */(aka /* anonymous */) [/var/www/wikaike-devel/node_modules/vinyl-fs/lib/src/getContents/bufferFile.js:11] (this=0x37308091 <undefined>,err=0x37308081 <null>,data=0x57528e71 <a Buffer>#2#) {
remote: // expression stack (top to bottom)
remote: [03] : 0x57528e71 <a Buffer>#2#
remote: [02] : 0x37308091 <undefined>
remote: [01] : 0x44fecaf5 <JS Function>#5#
remote: [00] : 0x57524f79 <a File>#6#
remote: --------- s o u r c e c o d e ---------
remote: function (err, data) {? if (err) {? return cb(err);? }? file.contents = stripBom(data);? cb(null, file);? }
remote: -----------------------------------------
remote: }
remote:
remote: [4]: /* anonymous */ [fs.js:266] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>) {
remote: // expression stack (top to bottom)
remote: [03] : 0x57528e71 <a Buffer>#2#
remote: [02] : 0x37308081 <null>
remote: [01] : 0x37308091 <undefined>
remote: [00] : 0x57527ca9 <JS Function>#7#
remote: --------- s o u r c e c o d e ---------
remote: function (er) {? if (size === 0) {? // collected the data into the buffers list.? buffer = Buffer.concat(buffers, pos);? } else if (pos < size) {? buffer = buffer.slice(0, pos);? }?? if (encoding) buffer = buffer.toString(encoding);? return callback(er, buffer);?...
remote:
remote: -----------------------------------------
remote: }
remote:
remote: [5]: /* anonymous */ [/var/www/wikaike-devel/node_modules/vinyl-fs/node_modules/graceful-fs/graceful-fs.js:102] (this=0x373346d5 <JS Global Object>#1#,er=0x37308081 <null>) {
remote: // expression stack (top to bottom)
remote: [02] : 0x37308081 <null>
remote: [01] : 0x373346d5 <JS Global Object>#1#
remote: [00] : 0x57529081 <JS Function>#8#
remote: --------- s o u r c e c o d e ---------
remote: function (er) {? onclose()? cb(er)? }
remote: ---------------/var/opt/gitlab/git-data/repositories/root/wikaike.git/hooks/deploy-hook: line 42: 26701 Bus error /usr/bin/gulp babel --branch $branch --path $i


To make it short: Babel is not working if starting using my post-receive hook. But the script works on the server (started with the git user)

Any idea what's happening?

Answer

The two elements to check (when a command works in command line but not as a Git hook) are:

  • the user executing the script
  • the environment variables (most notably the $PATH)

If the $PATH differs, some tools can be missing, or have a different version as the one expected.