Blog Detail

Covid-19 Tracker Ask Question

preview image DevOps
by Anurag Srivastava, Jun 13, 2018, 3:44:01 PM | 4 minutes |

Configure Jenkins for Automated Code Deployment

In this blog, I am going to cover the process of configuring Jenkins after installing it to automate the process of code deployment on a web server. I will set the Jenkins scheduler for 1 minute which will keep on checking the Git code on a given branch and as soon as the code on that branch changes, Jenkins will start the build process to deploy the code. I am taking an example of a demo project code and going to explain the process in a step by step way.

Setup on Deployment Server (Server A)
1) Create a shell script file as pull.sh on the home directory of the web server where you are going to deploy the code. Open the file and write the following script:

cd /var/www/html/project_directory
sudo git pull origin master

Save the script file and close it.

Setup on Jenkins Server (Server B)

1) Before configuring the Jenkins we need to install sshpass on the server where Jenkins is installed and for that, we need to run the following command:

sudo apt-get install sshpass


2) Now open the Jenkins on a browser by typing the URL and port of Jenkins:

http://192.168.xx.xx:8080


3) Log in to the Jenkins server by entering the username and password.

4) Click on the 'Create new job' link for creating a new job.

5) Under the item, name write the name of the job.

6) Click on the 'Freestyle project' click on the 'OK' button on the page. This will create a new job with the given name.

7) From the project, listing click on the project drop-down and click on 'configure' to configure the project.

8) Under Source Code Management selects the git option, please refer to the below screenshot:



9) Provide the repository URL and add credentials.

10) Under 'Branch Specifier' provide the Git branch name like:

*/master


11) Under build triggers select the Poll SCM and under schedule add * * * * * to pull every minute for any changes to deploy.

12)Under build click on execute shell to open the text area for executing the command. Enter the following command:

pwd
HOST=192.168.xx.xx
sshpass -p "password" ssh -T '-o StrictHostKeyChecking=no' username@$HOST sh pull.sh

If you want to know more about sshpass then click here.

13) Click on the apply button and then the save button.

Now, whenever new code is merged with the master branch Jenkins will initiate the build process automatically without any intervention. We can manually deploy the code by clicking on the 'Build Now' link from the left menu. If we want to see the deployment log then we can click on the build number or date under the 'Build History' section of the left menu. Anytime we can disable the project by clicking on the 'Disable Project' button of the project page.

We can configure SonarQube testing before code deployment through Jenkins which adds a code quality check before deployment but this I will explain in my next blog of Jenkins. If you want to know the Jenkins and SonarQube installation process and configuration then refer to my previous blog:

------------------------------------------------------------------------

Why SonarQube is important for IT projects ?  
SonarQube installation on Ubuntu 
Install Jenkins on Ubuntu  
Configure SonarQube Scanner with Jenkins
Analyze your project with SonarQube

------------------------------------------------------------------------
In that blog I have explained how we can install Jenkins on an Ubuntu the server now I am going to explain the process through which we can automatically deploy code from Git to a web server whenever there is a code merge in the master branch of Git. In case of any queries please leave a comment.


Comments (3)

  • user image
    Hanumanth reddy
    Jun 20, 2018, 7:10:34 PM

    Hi Sir, thanks for sharing the info. Suppose i want to deploy the code in some linux server ,then how we can configure the linux server details in jenkins . please explain .

  • user image
    Anurag Srivastava
    Jun 20, 2018, 11:43:06 PM

    I have already explained the process Hanumanth. On Linux server you need to create a script where you can change the directory to your application deployment directory then pull the code from Git. From Jenkins apply the configuration to execute this script. Thats it. If you still have any query then please let me know the specific problem which you are facing.

  • user image
    Amit Sharma
    Jul 5, 2018, 1:46:57 PM

    I have 5 Jboss JVMs running on a remote linux box. How do I configure my jenkins to start/stop those JVMs. For example - Start/stop all JVMs in one go, or start/stop them by selection. Thanks

Leave a comment

Related Blogs

Introduction to Cloud Computing

Sep 23, 2020, 2:38:00 PM | Anurag Srivastava

Monitoring website uptime using Kibana

Sep 6, 2020, 5:20:33 PM | Anurag Srivastava

Monitoring Application using Elastic APM

Jan 16, 2020, 11:43:29 AM | Anurag Srivastava

Configuring Django application with Elastic APM

Jan 14, 2020, 10:22:34 AM | Anurag Srivastava

Introduction to Elastic APM

Jan 7, 2020, 7:15:34 PM | Anurag Srivastava

Why monitoring is important?

Jan 6, 2020, 7:30:13 PM | Anurag Srivastava

Top Blogs

Configure SonarQube Scanner with Jenkins

Jun 21, 2018, 4:58:11 AM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

Jul 16, 2018, 5:00:02 PM | Anurag Srivastava

Configuring Logstash to send MongoDB data into Elasticsearch

Mar 9, 2019, 8:20:38 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

Jun 13, 2018, 3:44:01 PM | Anurag Srivastava

Deploying Angular code using Python script

Jun 26, 2018, 4:50:18 PM | Anurag Srivastava

SonarQube installation on Ubuntu

May 12, 2018, 4:47:07 PM | Anurag Srivastava