This post outlines a simple and secure method for deploying a Jekyll site to a server/VPS by means of SSH using Grunt and Bash.
The objective is to deploy a website by means of a Grunt task without exposing senstive information (SSH port, server IP address, username etc) in the Gruntfile. This allows the Gruntfile to be added to your VCS.
This is achieved by running a custom Bash script, which is executed from within Grunt.
Setting up Grunt Exec
The grunt-exec plugin allows shell commands to be executed from within Grunt.
Install grunt-exec with Yarn:
…or install grunt-exec with NPM:
The configuration for grunt-exec might look like this:
Your deploy tasks can now be defined in the project Gruntfile.js:
Now from within your project directory running grunt deploy will run the exec:build task (bundle exec jekyll build), followed by the exec:deploy task.
The exec:deploy task runs a BASH script called deploy-my-website - which should be held locally, outside your version control system.
BASH Deploy Script
I like to organise deploy scripts into a dedicated directory, creating symlinks to the files from with my /usr/local/bin directory (or somewhere within your $PATH).
The deploy script should be executable:
The deploy script utilises Rsync to copy files across to the server:
In our case, SSH is configured to only allow connection by means of SSH Keys - Public Key Cryptography/Challenge Response Authemtication. This makes the deployment process very secure - the local private key (passphrase protected for extra security) is necessary to establish the connection to the server.