Deploy a Website using Gulp and Rsync

There are many ways to deploy a website from a local development machine to a server. If you are already using Gulp as your build tool, here is yet another approach to do so. It uses the command line tool rsync to connect to the server via SSH and syncs changed or new files. A basic deploy task in Gulp with this approach could look as follows.

var gulp = require('gulp');
var spawn = require('child_process').spawn;

gulp.task('deploy', ['build'], function () {
    spawn('rsync', [
        '--dry-run', // makes rsync perform a trial run that doesn’t make any changes
        '-a', // archive mode; same as -rlptgoD (no -H)
        '-v', // increase verbosity
        '-z', // compress file data during the transfer
        '--delete', // delete extraneous files from dest dirs
        '-e', // remote shell
        'ssh', // use secure shell
        'build/', // local directory, trailing slash causes only content to be synced
        'username@example.org:/path/of/website/' // host and remote directory
    ], {stdio: 'inherit'});

Note that the --dry-run flag will simulate the deployment, but it will not actually perform it. Use it for testing, remove it to actually deploy the website. You can find more information about the specifics of all these flag on explainshell.

Combining the flags where possible and removing flags that are set by default leads to the following much shorter task.

gulp.task('deploy', ['build'], function () {
    spawn('rsync', [ '-avz', '--delete', 'build/', 'username@example.org:/path/of/website/' ], {stdio: 'inherit'});

Using this approach to deploy something is simple and suitable for small projects. For a larger code base, other approaches might be better and quicker, though.