chovy
chovy

Reputation: 75686

Do grunt tasks wait for previous tasks to finish?

When I call shell:clean at the end, it appears to delete the ~/tmp/<domain> directory before the shell:sync task finishes. I'm missing files in the destination ~/www/versions/<domain>.

The shell:sync tasks does an rsync command to copy files from ~/tmp to ~/www.

grunt.registerTask('deploy', 'Deploy web app', function(){
    grunt.task.run('shell:create_temp');

    //create version directories
    grunt.task.run('shell:create_version');
    grunt.task.run('shell:sync');

    //create symlink to live site
    grunt.task.run('shell:symlink');
    grunt.task.run('shell:clean');
});

Here is the config:

grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        cfg: _.extend(grunt.file.readJSON('config.json'), grunt.file.readJSON('config.'+env+'.json'), { env: env }),
        version_path: '<%= cfg.www_dir %>/versions/<%= cfg.domain %>@<%= pkg.version %>',
        tmp_site_path: '<%= cfg.tmp_dir %>/<%= cfg.domain %>',
        live_path: '<%= cfg.www_dir %>/<%= cfg.domain %>',
        shell: {
            create_temp: {
                command: 'mkdir -p <%= tmp_site_path %>',
                options: {
                    stdout: true,
                    stderr: true
                }
            },
            create_version: {
                command: 'mkdir -p <%= version_path %>',
                options: {
                    stdout: true,
                    stderr: true
                }
            },
            sync: {
                command: 'rsync -avz --delete --progress <%= tmp_site_path %>/ <%= version_path %>/',
                options: {
                    stdout: true,
                    stderr: true
                }
            },
            symlink: {
                command: 'rm <%= live_path %>; ln -sv <%= version_path %> <%= live_path %>',
                options: {
                    stdout: true,
                    stderr: true
                }
            },
            clean: {
                command: 'rm -rf <%= tmp_site_path %>',
                options: {
                    stdout: true,
                    stderr: true
                }
            }
        }
    });

Upvotes: 1

Views: 2856

Answers (2)

Ben
Ben

Reputation: 10146

You might want to check out grunt-bg-shell. It allows you to specify a callback for when tasks are finished running.

bgShell: {
  sync: {
    cmd: 'rsync -avz --delete --progress <%= tmp_site_path %>/ <%= version_path %>/',
    done: function () {
      grunt.task.run('shell:clean');
    }
  }
}

Upvotes: 0

Simon Boudrias
Simon Boudrias

Reputation: 44609

Usually tasks are run synchronously. But a plugin could bypass this - and this could be a bug in the plugin too.

Note though, that you'll usually combine taks this way:

grunt.registerTask("deploy", [
    'shell:create_temp',
    'shell:create_version',
    'shell:sync',
    'shell:symlink',
    'shell:clean'
]);

Upvotes: 2

Related Questions