Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp Git run first commit task then push task

I am working on project and I want to commit and push on git using by gulp but I am facing some issue when I am running git task so push then task is not waiting for commit....

Anyone can do my help! I want to make the task like first run commit and then push automatically and don't run push task until complete commit task....

Gulp Task for Gulp Git Commit and Push!

var gulp              = require('gulp'),
    runSequence       = require('run-sequence'),
    gutil             = require('gulp-util'),
    git               = require('gulp-git'),
    prompt            = require('gulp-prompt');


/* task to commit and push all file on git
******************************************************************/
// git commit task with gulp prompt
gulp.task('gulp:commit', function(){
  // just source anything here - we just wan't to call the prompt for now
  gulp.src('./*')
  .pipe(prompt.prompt({
    type: 'input',
    name: 'commit',
    message: 'Please enter commit message...'
  }, function(res){
    // now add all files that should be committed
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too
    return gulp.src([ '!node_modules/', './*' ], {buffer:false})
    .pipe(git.commit(res.commit));
   }));
});

// Run git push, remote is the remote repo, branch is the remote branch to push to
gulp.task('gulp:push', ['gulp:commit'], function(cb){
    git.push('origin', 'master', cb, function(err){
        if (err) throw err;
    });
});

// # task completed notification with green color!
gulp.task('gulp:done', ['gulp:push'], function(){
  console.log('');
  gutil.log(gutil.colors.green('************** Git push is done! **************'));
  console.log('');
});

// gulp task to commit and push data on git
gulp.task('git', function(){
  runSequence(['gulp:commit', 'gulp:push', 'gulp:done'])
});
like image 401
Faizy Avatar asked Nov 09 '22 15:11

Faizy


1 Answers

The problem is you're telling runSequence plugin to run the tasks in parallel. The solution is really simple:

// gulp task to commit and push data on git
gulp.task('git', function(){
  return runSequence('gulp:commit', 'gulp:push', 'gulp:done');
});

By doing this you ensure that gulp:push will run only after gulp:commit finished, and after the push is done gulp:done will run.

Also, I recommend you return the stream in gulp:push and use the done callback parameter in gulp:done, if you don't do that gulp doesn't know when this tasks ends:

// git commit task with gulp prompt
gulp.task('gulp:commit', function(){
  // just source anything here - we just wan't to call the prompt for now
  return gulp.src('./*')
  .pipe(prompt.prompt({
    type: 'input',
    name: 'commit',
    message: 'Please enter commit message...'
  }, function(res){
    // now add all files that should be committed
    // but make sure to exclude the .gitignored ones, since gulp-git tries to commit them, too
    return gulp.src([ '!node_modules/', './*' ], {buffer:false})
    .pipe(git.commit(res.commit));
   }));
});

// Run git push, remote is the remote repo, branch is the remote branch to push to
gulp.task('gulp:push', ['gulp:commit'], function(cb){
  return git.push('origin', 'master', cb, function(err){
    if (err) throw err;
  });
});

// # task completed notification with green color!
gulp.task('gulp:done', ['gulp:push'], function(done){
  console.log('');
  gutil.log(gutil.colors.green('************** Git push is done! **************'));
  console.log('');
  done();
});

EDIT: You have to return the stream in gulp:commit task too, I changed my answer to show you how.

like image 128
franmartosr Avatar answered Nov 14 '22 22:11

franmartosr