【问题标题】:Concatenating gulp-git tasks连接 gulp-git 任务
【发布时间】:2016-05-10 00:47:38
【问题描述】:

我有以下 gulp-git 任务,它们几乎是此处提供的任务示例的副本https://www.npmjs.com/package/gulp-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;
  });
});

逻辑很简单,签出一个分支,然后将其推送到远程源。

当我使用 gulp push-stage 或 gulp push-dist 分别运行任务时,它们运行良好。

但我想同时推送 stage 和 dist,所以我创建了一个名为 deploy-remote 的新任务。 该任务有效,它将提交推送到原始存储库,但最终崩溃:

[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)

这应该如何实现?

【问题讨论】:

    标签: git gulp gulp-git


    【解决方案1】:

    首先,这些 git 操作都是异步的。这意味着您必须通过调用回调函数cb 来告诉 gulp 何时完成。例如:

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

    其次,您不能同时在同一个 repo 上运行两个 git 进程,这就是您正在做的事情。每个 git 进程都会创建一个 .git/index.lock 文件,以防止其他 git 进程访问同一存储库。

    您必须使用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);
    });
    

    【讨论】:

    • 谢谢,不知道 run-secuence 的包,就像一个魅力
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2015-08-02
    • 1970-01-01
    相关资源
    最近更新 更多