Gulp git run the first commit task and then complete the task

I am working on a project and I want to commit and click on git using gulp, but I have to face some problem when I run the git task, so push then the task does not wait for a commit ....

Anyone can help me! I want the task to run as the first commit attempt, and then automatically push and not push the task until the commit task completes ....

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']) }); 
+6
source share
2 answers

The problem is that you are running the runSequence plugin to run tasks in parallel. The solution is very 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 will make sure that gulp: push will only be started after gulp: commit is completed and after gulp: done is completed .

In addition, I recommend that you return the stream to gulp: click and use the completed callback parameter in gulp: done , unless you do that gulp does not know when the following tasks are completed:

 // 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 should return the stream to the gulp: commit task, I changed my answer to show you how to do this.

+1
source

gulp - git commit does not actually return a stream (by design). See Commit does not start end # 49 .

To get around this, you can use a promise library like bluebird .

Here's what worked for me, as github bfricka user suggested:

 gulp.task('commit-changes', function (cb) { var q = require('bluebird'); //commit needs aa promise return new q(function (resolve, reject) { gulp.src('../') .pipe(git.add()) .pipe(stream = git.commit("my commit message")).on('error', function (e) { console.log('No changes to commit'); }) .on('error', resolve) //resolve to allow the commit to resume even when there are not changes. .on('end', resolve); stream.resume(); //needed since commmit doesnt return stream by design. ; }); }); 
0
source

All Articles