2016-05-09 35 views
0

我有一個幾乎是這裏https://www.npmjs.com/package/gulp-git串聯一飲而盡,git的任務

/** 
* Checkout production build branch. 
*/ 
gulp.task('checkout-dist', function(){ 
    git.checkout('dist', function (err) { 
    if (err) throw err; 
    }); 
}); 

/** 
* Checkout pre production build branch. 
*/ 
gulp.task('checkout-stage', function(){ 
    git.checkout('stage', function (err) { 
    if (err) throw err; 
    }); 
}); 

/** 
* Push production build branch. 
*/ 
gulp.task('push-dist', ['checkout-dist'], function(){ 
    git.push('origin', 'dist', {args: " -f"}, function (err) { 
    if (err) throw err; 
    }); 
}); 

/** 
* Push pre production build branch. 
*/ 
gulp.task('push-stage', ['checkout-stage'], function(){ 
    git.push('origin', 'stage', {args: " -f"}, function (err) { 
    if (err) throw err; 
    }); 
}); 

/** 
* Push production and pre production branches. 
*/ 
gulp.task('deploy-remote', ['push-stage', 'push-dist'], function(){ 
    git.checkout('master', function(err) { 
    if (err) throw err; 
    }); 
}); 

的邏輯很簡單提供的任務實例複製以下嚥,git的任務,檢出一個分支,並將它推到原始的遙控器。

當我一飲而盡推階段或大口推DIST單獨運行任務,他們正在工作的罰款。

但我想在同一時間推階段和DIST所以我創建了一個名爲部署的遠程新的任務。 這項任務的工作原理,它推提交到原點回購,但在最後與崩潰:

[17:18:56] Starting 'checkout-stage'... 
[17:18:56] Finished 'checkout-stage' after 12 ms 
[17:18:56] Starting 'push-stage'... 
[17:18:56] Finished 'push-stage' after 9.41 ms 
[17:18:56] Starting 'checkout-dist'... 
[17:18:56] Finished 'checkout-dist' after 14 ms 
[17:18:56] Starting 'push-dist'... 
[17:18:56] Finished 'push-dist' after 14 ms 
[17:18:56] Starting 'deploy-remote'... 
[17:18:56] Finished 'deploy-remote' after 12 ms 

gulpfile.js:483 
    if (err) throw err; 
       ^
Error: Command failed: fatal: Unable to create '.git/index.lock': The file already exists. 

If no other git process is currently running, this probably means a 
git process crashed in this repository earlier. Make sure no other git 
process is running and remove the file manually to continue. 

    at ChildProcess.exithandler (child_process.js:658:15) 
    at ChildProcess.emit (events.js:98:17) 
    at maybeClose (child_process.js:766:16) 
    at Socket.<anonymous> (child_process.js:979:11) 
    at Socket.emit (events.js:95:17) 
    at Pipe.close (net.js:466:12) 

應該如何實現這一目標?

回答

2

首先那些混帳操作都是異步的。這意味着你必須通過調用回調函數cb來告訴他們完成的時間。例如:

gulp.task('push-dist', ['checkout-dist'], function(cb) { 
    git.push('origin', 'dist', {args: " -f"}, function (err) { 
    cb(err); 
    }); 
}); 

其次,你不能同時在同一個倉庫上運行兩個git進程,這就是你正在做的。每個git進程都會創建一個.git/index.lock文件,以防止其他g​​it進程訪問同一個存儲庫。

你必須使用run-sequence運行一個接一個的操作:

var runSequence = require('run-sequence'); 

gulp.task('checkout-master', function(cb) { 
    git.checkout('master', function(err) { 
    cb(err); 
    }); 
}); 

gulp.task('deploy-remote',, function(cb) { 
    runSequence('push-stage', 'push-dist', 'checkout-master', cb); 
}); 
+0

謝謝,不知道運行secuence的包,就像一個魅力 – lapinkoira