A simple script to git pull remotely

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:

./deploy.sh [demo/prod]

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.


if [ "$1" != "" ]; then
    echo "Please specify environment (demo/prod)."
    exit 1

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]"
    echo "Environment argument should be either demo or prod."
    exit 1

# 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!

Published by Mageek

Just another geek in the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: