Blog Detail

preview image DevOps
by Anurag Srivastava, Jun 13, 2018, 3:44:01 PM | 3 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 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 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 web server where you are going to deploy the code. Open the file and write 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 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) Login to the Jenkins server by entering username and password.

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

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

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

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

8) Under Source Code Management select the git option.



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 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 apply button and then save button.

Now whenever new code is merged with master branch Jenkins will initiate the build process automatically without any intervention. We can manually deploy the code by clicking on 'Build Now' link from left menu. If we want to see the deployment log then we can click on the build number or date under 'Build History' section of left menu. Anytime we can disable the project by clicking on the 'Disable Project' button of 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 installation process then refer to my previous blog:

Install Jenkins on Ubuntu

In that blog I have explained how we can install Jenkins on an Ubuntu 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 master branch of Git.

In case of any queries please leave a comment.

About Author

Anurag Srivastava

Author | Blogger | Tech Lead | Data Scientist | Innovator |

View Profile

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

Load csv Data into Elasticsearch

Oct 15, 2018, 6:25:43 PM | Anurag Srivastava

htop: An Interactive Process Viewer

Oct 13, 2018, 8:49:59 PM | Anurag Srivastava

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Execute Commands on Remote Machines using sshpass

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

Configure SonarQube Scanner with Jenkins

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

Build and deploy Angular code using Python

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

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

SonarQube installation on Ubuntu

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

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Top Blogs

Build and deploy Angular code using Python

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

Configure SonarQube Scanner with Jenkins

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

Configure Logstash to push MySQL data into Elasticsearch

Jul 7, 2018, 8:51:30 AM | Anurag Srivastava

Configure Jenkins for Automated Code Deployment

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

SonarQube installation on Ubuntu

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

Execute Commands on Remote Machines using sshpass

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

Why SonarQube is important for IT projects ?

Apr 24, 2018, 2:52:28 PM | Anurag Srivastava

Install Jenkins on Ubuntu

May 26, 2018, 6:42:02 PM | Anurag Srivastava

Analyze your project with SonarQube

Jun 2, 2018, 10:49:54 AM | Anurag Srivastava

Install Kafka on Ubuntu

Jul 12, 2018, 7:40:51 PM | Anurag Srivastava