Skip to content

Instantly share code, notes, and snippets.

@travist
Last active August 29, 2015 14:08
Show Gist options
  • Save travist/69d76d93224af7891751 to your computer and use it in GitHub Desktop.
Save travist/69d76d93224af7891751 to your computer and use it in GitHub Desktop.
Gruntfile to easily deploy code changes in a project to multiple servers as you change code.

Grunt Watch and Deploy to Multiple Servers

This is a Grunt configuration to watch a folder for code changes, and then deploy that code to multiple servers after a change has been made. Then, you can execute a command once those files have been uploaded to the server, such as clearing cache and such.

Configuration

There are some variables at the start of the Gruntfile.js that you will want to change to get this working for your use case. Hopefully it is self explanatory.

You may also need to add some server configurations to your ~/.ssh/config file, like the following.

~/.ssh/config

Host server1
  HostName server1.com
  User travist
  IdentityFile /Users/travist/.ssh/id_rsa

Host server2
  HostName server2.com
  User travist
  IdentityFile /Users/travist/.ssh/id_rsa

Installation

Copy all of the files in this Gist to a separate folder, and then type.

npm install

Running

To run this, simply type

grunt

This will then watch the folder that you specified and if any files change, it will execute the rsync to the servers that you provide.

module.exports = function(grunt) {
// The hosts we wish to deploy to.
var hosts = ['server1', 'server2'];
// The folders to watch.
var watchFolder = 'src/**/*';
// Rsync src folder.
var rsyncSRC = './src/';
// Rsync dest folder.
var rsyncDEST = '/webroot/src';
// Command to run after the rsync has been made.
var postCommand = 'cd /webroot/src; rake symfony:assets; rake symfony:cache; sudo service apache2 reload;';
var config = {
pkg: grunt.file.readJSON('package.json'),
watch: {
upload: {
files: [watchFolder],
tasks: ['rsync', 'shell']
}
},
rsync: {
options: {
args: ['--verbose'],
exclude: ['node_modules'],
recursive: true
}
},
shell: {
options: {}
}
};
// Add each server to the configuration.
hosts.forEach(function(host) {
config.rsync[host] = {
options: {
src: rsyncSRC,
dest: rsyncDEST,
host: host
}
};
config.shell[host] = {
command: 'ssh ' + host + ' "' + postCommand + '"'
};
});
grunt.initConfig(config);
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-rsync');
grunt.loadNpmTasks('grunt-shell');
grunt.registerTask('default', ['watch']);
};
{
"name": "grunt-multi-deploy",
"version": "1.0.0",
"description": "A multi deploy mechanism as you change files..",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://gist.github.com/travist/69d76d93224af7891751"
},
"author": "Travis Tidwell",
"license": "MIT",
"homepage": "https://gist.github.com/travist/69d76d93224af7891751",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-watch": "^0.6.1",
"grunt-rsync": "^0.6.2",
"grunt-shell": "^1.1.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment