This tutorial shows how to create a simple CI domain, which can be used to communicate with virtual instances as well as to test and automate the deployment of your software in IaaS based cloud services.
Continuous integration and continuous development or CI/CD is one of the best modern software practices for DevOps teams deploying multiple applications in different types of servers. In IaaS based cloud software services, developers are required to configure their own servers and everything else, except the hardware components. Imagine having to regularly check for securities, operations, and development updates. This tutorial will show you how to write your own simple CI/CD bash script to automate all these tasks.
Infrastructure as a Service (IaaS): IaaS is a cloud service model offering access to computing resources like servers, storage, and networks. The IaaS provider hosts and manages this infrastructure. Unlike PaaS or SaaS, customers use the internet to access the hardware and resources.
Continuous integration (CI): This is the continuous automation of integration of code changes or frequent updates at mentioned time intervals. This facilitates effective processes for getting products to market faster than ever before by ensuring an ongoing flow and bug fixes without the constant need for manual help.
Continuous development (CD): Continuous development comprises the automated tests, builds, and packages updated in software to ensure it is kept up-to-date and evolves from time to time. Every change that passes the automated tests is automatically placed in production so that the developers are not constrained by regulatory or other requirements.
Shell script: Shell script is the interface used by developers to communicate directly with the operating system. In local Linux based systems, one uses the .sh type file to create shell scripts to communicate with local machines. In platforms like Azure Cloud, Google Cloud, and AWS cloud services, this can be implemented to communicate with their virtual instances.
Creating a simple CI/CD bash script
Let us take a simple HTML file deployed on Apache server for demonstration, where we create a folder named CICD which has been cloned from GitHub, and add two files in it, one of which is a JavaScript file called sample.js. This file contains a timeout for 3000 seconds, and reloads the web page using the reload () function so that the user is not required to refresh the web page to notice any new changes. Next, we create the index.html file, which contains the information required to be displayed on the web page.
anisha@DESKTOP-HUVAMN5:~/CICD$ cat sample.js setTimeout(function(){ window.location.reload(); },3000); anisha@DESKTOP-HUVAMN5:~/CICD$ cat index.html <!DOCTYPE html> <html> <script src=”sample.js”></script> <body> <h1> This is a sample html page</h1> <p>This is deployed on Apache server</p> <marquee> Test </marquee> </body> </html anisha@DESKTOP-HUVAMN5:~/CICD$ ls index.html sample.js anisha@DESKTOP-HUVAMN5:~/CICD$
Let us now create a shell script containing the CI/CD bash script you want to be executed for your deployment. The code given below shows that I am running an infinite loop, with a sleep interval of 3 seconds. In case there are multiple developers working on the same project, this file will be constantly running every 3 seconds, making sure that the code from GitHub is uploaded using the command git pull.
The next line copies this bash file to the /var/www/html folder, where all the Apache HTML files are to be stored. Then the line removes the error logs for the CICD repository and restarts the Apache server. This goes on till the developer manually stops the infinite loop. After this, we run the shell script, which shows the changes made, and performs all the instructions specified in the script. We can see the server restarting after every 3 seconds and automatically updating the new changes without the need for human intervention.
anisha@DESKTOP-HUVAMN5:~$ cat script.sh cd CICD while true do git pull sudo cp /var/www/html sudo rm -f /var/logs/apache2/error.log sudo service apache2 restart sleep 3 done
Figure 1 shows the output when the above script is executed.
You can clone your repository or work on your local system, download a server like Apache or Nginx and start it to see the HTML page running on the server when you type http://localhost.
This runs on any VM using a Linux based distro, such as using cloud shells for instances and VMs. Figure 2 shows the web page running.
I hope this short tutorial will make things easier for developers by automating the mundane and tedious tasks of infrastructure management.