Grunt watch: only upload files that have changed

grunt-contrib-watch, gruntjs, ssh

Solution

You can achieve that with Grunt:

grunt-contrib-watch

grunt-rsync

First things first: I am using a Docker Container. I also added a public SSH key into my Docker Container. So I am uploading into my "remote" container only the files that have changed in my local environment with this Grunt Task:

'use strict';

module.exports = function(grunt) {

    grunt.initConfig({

        rsync: {
            options: {
                args: ['-avz', '--verbose', '--delete'],
                exclude: ['.git*', 'cache', 'log'],
                recursive: true
            },
            development: {
                options: {
                    src: './',
                    dest: '/var/www/development',
                    host: 'root@www.localhost.com',
                    port: 2222
                }
            }
        },

        sshexec: {
            development: {
                command: 'chown -R www-data:www-data /var/www/development',
                options: {
                    host: 'www.localhost.com',
                    username: 'root',
                    port: 2222,
                    privateKey: grunt.file.read("/Users/YOUR_USER/.ssh/id_containers_rsa")
                }
            }
        },

        watch: {
            development: {
                files: [
                'node_modules',
                'package.json',
                'Gruntfile.js',
                '.gitignore',
                '.htaccess',
                'README.md',
                'config/*',
                'modules/*',
                'themes/*',
                '!cache/*',
                '!log/*'
                ],
                tasks: ['rsync:development', 'sshexec:development']
            }
        },

    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-rsync');
    grunt.loadNpmTasks('grunt-ssh');

    grunt.registerTask('default', ['watch:development']);
};

Good Luck and Happy Hacking!

Problem

related I was able to set up a Grunt task to SFTP files up to my dev server using grunt-ssh: ``` sftp: { dev: { files: { './': ['**','!{node_modules,artifacts,sql,logs}/**'], }, options: { path: '/path/to/project', privateKey: grunt.file.read(process.env.HOME+'/.ssh/id_rsa'), host: '111.111.111.111', port: 22, username: 'marksthebest', } } }, ``` But this uploads everything when I run it. There are thousands of files. I don't have time to wait for them to upload one-by-one every time I modify a file. How can I set up a watch to upload only the files I've changed, as soon as I've changed them? (For the curious, the server is a VM on the local network. It runs on a different OS and the setup is more similar to production than my local machine. Uploads should be lightning quick if I can get this working correctly)

Original source