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 (4)
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 .
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.
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
shiva kumar
Sep 9, 2021, 1:49:55 PM
Hi, iam getting error at execute shell command. see below error. I've gone thru all sudo changes, but no luck. please help me out. + sshpass -p skptest ssh -T -o StrictHostKeyChecking=no skptest@192.168.56.106 sh pull.sh sudo: no tty present and no askpass program specified Build step 'Execute shell' marked build as failure Finished: FAILURE
Leave a comment
Related Blogs
Introduction to Logstash
Dec 20, 2019, 11:38:31 AM | Anurag Srivastava
Importing MongoDB data into Elasticsearch
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Importing MySQL data into Elasticsearch
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Snapshot and Restore Elasticsearch Indices
Sep 16, 2019, 5:55:06 AM | Anurag Srivastava
Log analysis with Elastic stack
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Creating Elasticsearch Cluster
Apr 6, 2019, 8:41:41 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
Importing MongoDB data into Elasticsearch
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Importing MySQL data into Elasticsearch
Feb 9, 2019, 12:06:18 PM | 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