Automate your Deployments using Bash Scripts

thisurathenuka

Thisura Thenuka

Posted on May 9, 2021

Automate your Deployments using Bash Scripts

If you are a developer who is working on a project that doesn't have a code pipeline, this article would help you save a lot of time. Well, I understand that could be a very small group of people, but I am happy even if only one person learns something from this article.

In this article, I am going to cover the reason why you should automate your tasks as a developer and how you can use bash scripts to help you with that. Besides, I am going to explain some basic bash concepts to help you understand my bash script which I'll be sharing later in the article.

Without further ado, let's hop into the article.

Why should we focus on Automation as Developers?

Getting rid of Repetitive Tasks

As developers, we usually have to go through a set of repetitive tasks every single day. You'll be able to identify those tasks if you observe your daily routine. I believe that if I have to do the same thing twice, I need to automate it (I haven't automated much yet, but I always try to live by this). If you think about it, this actually is a core coding principle. This is just like writing a function to stop writing the same code over and over again.

Self-Satisfaction

I don't know if you ever have tried to automate something. It feels so satisfying when you finish automating something. The process is fun and interesting. You will learn so many things along the way. I didn't know much about Bash until I wanted to automatically deploy my services without doing individually which was really time-consuming. But now, I do have sound knowledge about Bash.

What are Bash Scripts?

Bash is a command language and also is a UNIX shell. It is the default command interpreter among Linux/GNU systems around the world. BASH (Bourne-Again SHell) was released in 1989 as a free software replacement for the Bourne Shell.

Bash Scripts are just plain text files with a set of commands. Basically, these are commands what we would usually type in on the command line. We just have created a file to put all the commands in it so that when we run that file, all the commands will be executed.

How can we use Bash Scripts for Automation?

Here what we are just going to create a single bash script file by putting in all the commands that we would have to type in when we log into the server. Instead of typing that set of commands individually which takes a lot of time, we are going to run this bash script file and let the magic happen.

Before I go into revealing and explaining my code, I think it would be much better if we learn the basics of Bash scripts. If you have a decent knowledge about BASH commands, you may directly skip over to the bash script.

Bash Basics

Creating a file

Bash Scripts have an extension of .sh

You can create a bash script with nano testing.sh or touch testing.sh

Parts of a Bash Script



#!/bin/bash

# Created by Thisura
echo "Hello World"


Enter fullscreen mode Exit fullscreen mode

The first line contains two parts. #! is called the Shebang. Next to the Shebang, we have to specify the path to the interpreter (You can find this by typing which bash). If you exactly don't know the path of the interpreter, you can just specify the first line like this.

#!/usr/bin/env bash

Important: There can't be any spaces between the characters in the first line. This line always has to be the first line of the bash script.

The 3rd line is a comment. Comments start with # in Bash.

The 4th line is printing Hello World in the terminal.

Making it executable

To make a bash script executable, we have to type chmod +x testing.sh.

Running a Bash Script

We can run the bash script file by typing ./testing.sh (Here the dot indicates the current directory) or bash testing.sh

We can also run the bash script without specifying the directory. For that, we need to add the script's directory to the $PATH variable by typing PATH=$PATH:/path/to/script.

The motivation behind creating the script

I have been working with Spring Boot and React environments. So, the services that I have to deploy usually are jar files and React builds. After uploading the releases to the server, I usually have to go to each folder and kill the existing service, run the new service and check if the service is running properly. In some projects, there are about 8 services and it takes forever to do the deployment.

So, in my free time, I began searching for a way to speed up the process. After some research, I realized creating a bash script would be the easiest and quickest way forward. So, I started learning Bash and after a lot of trial and error, I managed to create a bash script file that literally saved me literally hours. I'm still learning and trying to optimize what I have.

The Bash Script for Automating Deployments



#!/bin/bash

frontend_path='/root/Deployments/Frontend/bp-f-v0.4.0-zsic-iat'
backend_path='/root/Deployments/Backend'
auth_path='/root/Deployments/Auth'
reporting_path='/root/Deployments/Reporting'
claims_path='/root/Deployments/Claims'
email_path='/root/Deployments/Email'

reporting_release_name='rp-s-v0.3.0-zsic-iat.jar'
reporting_sleep=30

backend_release_name='bp-s-v0.4.7-zsic-iat.jar'
backend_sleep=30

auth_release_name='ua-s-v1.2.2-zsic-iat.jar'
auth_sleep=30

claims_release_name='cl-s-v5.0.2-zsic-iat.jar'
claims_sleep=30

email_release_name='em-s-v0.0.1.-zsic-iat.jar'
email_sleep=30

function kill_existing_services {
    echo "Killing Exisiting Services : "
    fuser -k 8087/tcp && sleep 2
    fuser -k 8091/tcp && sleep 2
    fuser -k 8206/tcp && sleep 2
    fuser -k 8001/tcp && sleep 2
    fuser -k 9988/tcp && sleep 2
    fuser -k 6999/tcp && sleep 2
    echo " "
}

function deploy_releases {
    cd $1
    java -jar $2 &
    sleep $3
    PID=$!
    kill -INT $PID
}

function checking_service_status {
    echo "Checking if services were deployed successfully : "
    echo "Reporting  Service : "
    fuser 8087/tcp && sleep 2
    echo "Backend  Service : "
    fuser 8091/tcp && sleep 2
    echo "Auth  Service : "
    fuser 8206/tcp && sleep 2
    echo "Email  Service : "
    fuser 9988/tcp && sleep 2
    echo "Claim  Service : "
    fuser 8001/tcp && sleep 2
    echo " "
}

function deploying_frontend {
    echo "Deploying Frontend Service (Please press Ctrl + C once the running stops)"
    cd $frontend_path
    nohup node server.js &
}

kill_existing_services
echo "Deploying Services : "
deploy_releases $reporting_path $reporting_release_name $reporting_sleep
deploy_releases $backend_path $backend_release_name $backend_sleep
deploy_releases $auth_path $auth_release_name $auth_sleep
deploy_releases $claims_path $claims_release_name $claims_sleep
deploy_releases $email_path $email_release_name $email_sleep
echo " "
checking_service_status
deploying_frontend


Enter fullscreen mode Exit fullscreen mode

Firstly, I have created variables for all the paths, release names and I also have specified an amount of time until which the thread will be sleeping (Until that relevant service is deployed).

I've created this bash script file using four functions.

  • kill_existing_services - This function kills the current running services in the specified ports where our services are running

Alt Text

  • deploy_releases - This function will deploying our backend releases in the server
  • checking_service_status - This function will check if the services were deployed in the specified ports

Alt Text

  • deploying_frontend - This function will deploy the frontend service

Alt Text

When calling the deploy_releases function, I am passing the release path, release name and sleep time as parameters. $1 is the first parameter and so on. I have made the thread sleep for 30 seconds to give time for each service to be deployed. This does differ on how good your internet connection is. You may adjust the time accordingly.

I believe other functions are pretty much self-explanatory.

I know this is not the perfect script for this. I am sharing this script for you to get an idea about how you should go on automating your tasks.

Conclusion

In this article, you learned the importance and benefits of automating repetitive tasks as a developer. In addition, you learned the basic of Bash scripts and I shared my deployment script for you to get an idea about how you would create your own.

If you feel like you learned something valuable today, please drop a like and share the article with your friends. If you are interested in studying Bash further, I highly recommend you guys to start by reading the following article.

https://www.taniarascia.com/how-to-create-and-use-bash-scripts/

💖 💪 🙅 🚩
thisurathenuka
Thisura Thenuka

Posted on May 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related