loading...

How to Automate Manual Steps after SSH

amitness profile image Amit Chaudhary Originally published at amitness.com Updated on ・2 min read

I recently worked on a Django web application which was deployed on a Linux Server with SSH access. Deployments
were done manually and the CI/CD pipeline was still in planning.

We had to perform these tasks every time to deploy the latest changes.

  • SSH into the server
  • Goto directory where the code is present
  • Activate Virtual Environment
  • Pull the latest changes on the current branch using Git
  • Install any newly added libraries from requirements.txt
  • Run migrations for the database
  • Run command to generate static files
  • Restart Nginx and Supervisor

I found the process repetitive and researched on whether it was possible to automate the commands I need to run
after SSHing into the server.

ssh -i "webapp.pem" ubuntu@example.com
Enter fullscreen mode Exit fullscreen mode

Turns out, we can write a shell script to automate the task.

Step 1:

Create a new shell script file deploy.sh with the following content. Modify it to point to your PEM file, username, and IP address.

#!/bin/bash
ssh -i "webapp.pem" ubuntu@example.com << EOF
    echo "Hello World"
EOF
Enter fullscreen mode Exit fullscreen mode

The above code prints 'Hello World' on the remote server.

Step 2:

You can write any shell commands between the two EOF and it will be run on the remote server.
Add the sequence of commands you currently run manually on the server to this script.

For the Django project, I wrote the following commands that pulls the latest code and restarts the services.

#!/bin/bash
ssh -i "webapp.pem" ubuntu@example.com << EOF
cd /var/www/webapp/

echo "Switching to www-data user"
sudo -Hu www-data bash

echo "Pulling Latest Changes"
git pull

echo "Activating Virtual Environment"
source venv/bin/activate

echo "Installing any new libraries"
pip install -r requirements.txt

echo "Migrating Database"
python manage.py migrate

echo "Returning back to Ubuntu user"
exit

echo "Restarting Supervisor and Nginx"
sudo service supervisor restart
sudo service nginx restart

echo "Deployment Finished"
EOF
Enter fullscreen mode Exit fullscreen mode

Step 3:

Run the below command to change permissions of the script and make it executable.

chmod a+x deploy.sh
Enter fullscreen mode Exit fullscreen mode

Step 4:

Run the script and it will perform all the deployment.

./deploy.sh
Enter fullscreen mode Exit fullscreen mode

This simple script has saved me a lot of time until the CI/CD process is in place.

Connect

If you enjoyed this blog post, feel free to connect with me on Twitter where I share new blog posts every week.

Discussion

pic
Editor guide
Collapse
zahiruddinnorzain profile image
zahiruddin

Thanks, good info. I use jenkins and run the deploy.sh file