For my simple private web projects when there are no script building or bundling, I just don’t need the whole CI/CD pipeline to get stuff working. I only needed git and Github. So here’s how I automate part of my deployment.
Before writing this bash script, I would have to first ssh into the remote server, them cd to the right directory and then do git pull, together with entering my credentials for the https authentication. Well, this is just a few steps but when you repeat them over and over and over… it just gets annoying.
For more details in the steps below, please visit https://github.com/ccontavalli/ssh-ident.
The reason I use ssh-ident is that it
will create an ssh-agent and load the keys you need the first time you actually need them, once. No matter how many terminals, ssh or login sessions you have, no matter if your home is shared via NFS.
0. generate a pair of keys for your Github repo if you don’t have them. Upload the public key’s content to Github (Settings -> SSH and GPG Keys -> New SSH Key).
1. install ssh-ident according to the instructions on Github. Try to understand the commands so that you can customise it if you want.
2. create a ~/.ssh-ident file and put in the match conditions in order to instruct ssh-ident where to find the identities for different situation. But remember to create a corresponding directory (same name with the identity name you specify in the ~/.ssh-ident file) under ~/.ssh/identities for storing the keys.
3. logout, login and do a git pull once in order to let the ssh-ident save your key and your passphrase. Voila! From now on up til the lifetime of the added key(you can customise the lifetime by setting SSH_ADD_DEFAULT_OPTIONS in ~/.ssh-ident), whenever you perform git pull again in your remote server, ssh-ident comes to the rescue and save the hassle of inputting credentials again!
The Deployment Script
This script below is a simple bash script and it actually just does a few simple commands for you. The usage is like this:
And that’s it! You just have to specify whether you are deploying to your demo server or production server; the ssh details are entailed inside the script.
#!/bin/bash if [ "$1" != "" ]; then environment=$1 else echo "Please specify environment (demo/prod)." exit 1 fi if [ "$environment" == "demo" ]; then target="ssh -i 'path/to/demo/pem' [email protected]" elif [ "$environment" == "prod" ]; then target="ssh -i 'path/to/prod/pem' [email protected]" else echo "Environment argument should be either demo or prod." exit 1 fi # ssh and then perform git pull + restart server cmd="$target 'cd path/to/code && git pull && ./restart_server.sh'" eval $cmd
The Final Execution
After I have pushed the commits locally on my development machine, I just need to run this script and specify the targeted environment, then the deployment will be done in like within 10 seconds.
Hope someone will find this interesting or even useful! Please leave comments below if you have anything to ask!