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 following script:
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:
3) Login to the Jenkins server by entering the 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:
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:
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 the 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 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:
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 the master branch of Git. In case of any queries please leave a comment.
Author | Blogger | Tech Lead | Elastic Stack | Innovator |View Profile
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 .
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.
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
Mar 9, 2019, 8:20:38 AM | Anurag Srivastava
Feb 9, 2019, 6:34:22 PM | Anurag Srivastava
Jan 31, 2018, 6:11:29 AM | Anurag Srivastava
Apr 14, 2018, 1:18:05 PM | Anurag Srivastava
Feb 9, 2019, 12:06:18 PM | Anurag Srivastava
Oct 13, 2018, 8:49:59 PM | Anurag Srivastava
Jun 21, 2018, 4:58:11 AM | Anurag Srivastava
Jun 26, 2018, 4:50:18 PM | Anurag Srivastava
Jun 13, 2018, 3:44:01 PM | Anurag Srivastava
Jul 16, 2018, 5:00:02 PM | Anurag Srivastava
Jul 12, 2018, 7:40:51 PM | Anurag Srivastava
Aug 14, 2018, 4:47:56 PM | Anurag Srivastava