If you are looking for a solution that your developer develop a web app and you would like to manage it using SCM(Git) and then share code on GitHub. After that move web app code in document root of web server. So in short we can say that how to deploy web application using Jenkins?
Step 1 :- Create Git local repository
Priyanshi@Priyanshi-PC MINGW64 /d/My/Msi Laptop/Training/DevOpsAL/Practice/Day4
$ pwd
/d/My/Msi Laptop/Training/DevOpsAL/Practice/Day4
$ ls
$ mkdir myws4
$ cd myws4/
$ ls -la
total 4
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:09 ./
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:09 ../
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:09 .git/
$ touch index.html web.html
$ cat >> index.html
This is my home page
$ cat >> web.html
This is my another web page
$ ls
index.html web.html
$ git init
Initialized empty Git repository in D:/My/MsiLaptop/Training/DevOpsAL/Practice/Day4/myws4/.git/
$ git add index.html web.html
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: index.html
new file: web.html
$ git status -s
A index.html
A web.html
$ git commit . -m "First commit of my web pages"
[master (root-commit) c4e11a7] First commit of my web pages
2 files changed, 2 insertions(+)
create mode 100644 index.html
create mode 100644 web.html
Step 2 :- Move code in GitHub
Create repository on GitHub - Home page > New > Reposigory name(project4) > Description > Do not click on "Initialize this repository with a README" > Create Repository
$ git remote add origin https://github.com/jay2tinku/project4.git
$ git push -u origin master
fatal: TaskCanceledException encountered.
A task was canceled.
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 352 bytes | 176.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/jay2tinku/project4.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
Step 3 :- Install Jenkins if yum/dnf installed, if not first configure yum/dnf
[root@devopsal_workstation ~]# yum install java-11-openjdk.x86_64 -y
[root@devopsal_workstation ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@devopsal_workstation ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
[root@devopsal_workstation ~]# yum install henkins -y
Step 4 :- Install apache web server
[root@devopsal_workstation ~]# yum install httpd -y
[root@devopsal_workstation ~]# systemctl start httpd
[root@devopsal_workstation ~]# systemctl enable httpd
[root@devopsal_workstation ~]# systemctl status httpd
[root@devopsal_workstation html]# setfacl -m u:jenkins:rwx /var/www/html/
[root@devopsal_workstation ~]# cd /var/www/html/
[root@devopsal_workstation html]# ls
Step 5 :- Jenkins configurations
Create project - Home page > New > Enter an Item name(project4) > Click on Freestyle project > OK
Configure project - Jenkins > project4 > Configure > Source Code Management > Select Git > Copy Clone URL from GitHub and paste in Repository URL > Build > Execute shell
After this Click on Build Now, if everything is fine than you will be able to access your web page. I am able to access.
But here we have to do lots of manual this like -
- Whenever developer change the code first of commit the code local Git
- Push the code on GitHub
- Inform someone to 'Build Now' project again.
Step 6 :- git push automatically.
We can do this using hooks. Hooks is a program which can run scripts available under hooks directory as per required. Also we can choose when do I want to run the script? pre-commit or post-commit.
$ ls -la
total 14
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:16 ./
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:35 ../
drwxr-xr-x 1 Priyanshi 197121 0 May 3 11:35 .git/
-rw-r--r-- 1 Priyanshi 197121 266 May 3 11:17 index.html
-rw-r--r-- 1 Priyanshi 197121 28 May 3 11:17 web.html
$ cd .git/hooks/
$ ls -ltrh
total 41K
-rwxr-xr-x 1 Priyanshi 197121 478 May 3 11:09 applypatch-msg.sample*
-rwxr-xr-x 1 Priyanshi 197121 896 May 3 11:09 commit-msg.sample*
-rwxr-xr-x 1 Priyanshi 197121 4.6K May 3 11:09 fsmonitor-watchman.sample*
-rwxr-xr-x 1 Priyanshi 197121 189 May 3 11:09 post-update.sample*
-rwxr-xr-x 1 Priyanshi 197121 424 May 3 11:09 pre-applypatch.sample*
-rwxr-xr-x 1 Priyanshi 197121 1.7K May 3 11:09 pre-commit.sample*
-rwxr-xr-x 1 Priyanshi 197121 416 May 3 11:09 pre-merge-commit.sample*
-rwxr-xr-x 1 Priyanshi 197121 1.4K May 3 11:09 pre-push.sample*
-rwxr-xr-x 1 Priyanshi 197121 544 May 3 11:09 pre-receive.sample*
-rwxr-xr-x 1 Priyanshi 197121 4.8K May 3 11:09 pre-rebase.sample*
-rwxr-xr-x 1 Priyanshi 197121 1.5K May 3 11:09 prepare-commit-msg.sample*
-rwxr-xr-x 1 Priyanshi 197121 3.6K May 3 11:09 update.sample*
$ cp post-update.sample post-commit
$ ls -ltrh post-commit
-rwxr-xr-x 1 Priyanshi 197121 189 May 3 12:42 post-commit*
$ > post-commit
$ cat >> post-commit-push
#!/bin/bash
echo "Hello user! welcome to my Git Push
git push
$ cd ../..
$ cat >> index.html
I am adding few more things
$ git commit index.html -m "first update in home page"
Hello user! welcome to my Git Push
fatal: TaskCanceledException encountered.
A task was canceled.
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 660 bytes | 165.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/jay2tinku/project4.git
c4e11a7..5e18002 master -> master
[master 5e18002] first update in home page
1 file changed, 1 insertion(+
Have a look on last command output, you see the message I printed in hooks file. That means hooks file executed and it has been pushed automatically on GitHub. So this we can have automated push operation.
Step 7 :- If code has been updated in GitHub then same has to be updated in web server directory by Jenkins as well. For this again navigate to Jenkins > project4 > Configure > Build Triggers > Click on Build Periodically and write five star, it is exactly same as you are scheduling a cronjob. By doing this you can achieve automation but it will run periodically only as you have scheduled. But in this approach it will keep on running as per schedule even if there is no commit. So again this is not a good option because even if no update in code but Jenkins will keep on downloading same code every time.
So to fix this issue we can use another option "Poll SCM" available just below to "Build Periodically". It is the same to configure like crontab but in background it will only download the code when there is any change in code.
So finally what is requirement? my job in jenkins should only run when there is any change in my code on Git. For this you can do below -
curl --user "admin:redhat" http://192.168.1.5:8080/job/project4/build?token=redhat
$ cat post-commit
#!/bin/bash
echo "Hello user! welcome to my Git Push
git push
curl --user "admin:redhat" http://192.168.1.5:8080/job/project4/build?token=redhat
That's all we have completed the task. You can test if you want.
cat >> index.html
For testing I am adding few more line
$ git commit index.html -m "Test the hook code"
Hello user! welcome to my Git Push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 358 bytes | 358.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/jay2tinku/project4.git
988b0cb..1ade3cb master -> master
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
[master 1ade3cb] Test the hook code
1 file changed, 10 insertions(+), 14 deletions(-)
rewrite index.html (75%)
$ git log
commit 1ade3cb10760fd0e96ba6d15bb3651a53ad6ebeb (HEAD -> master, origin/master)
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:18:54 2020 +0530
Test the hook code
commit 988b0cb105514e45a5c2aa755dce758e7042d8d4
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:03:25 2020 +0530
Last line
commit 2f49e921bddd5b78c098de639fca0c0993bf7b4f
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:00:46 2020 +0530
A new line
commit 8fcb83488e7c586c8a4b324ce26764d36e202d3a
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 14:49:51 2020 +0530
Commit for jenkins
commit e4a770d9efcb9f0e1294ffff75b36ae127c6afb6
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 13:33:21 2020 +0530
Few more changes
commit 5e180026d80f61d5e8dd85cb7ace3b927acab38f
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 12:55:51 2020 +0530
first update in home page
commit 26fd2aa6d1d66dcd0aa077a9599becb5952aaa73
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 12:52:10 2020 +0530
first update in home page
commit c4e11a7a8264d63497c8bf8b33af41a90830988d
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 11:20:05 2020 +0530
First commit of my web pages
So to fix this issue we can use another option "Poll SCM" available just below to "Build Periodically". It is the same to configure like crontab but in background it will only download the code when there is any change in code.
So finally what is requirement? my job in jenkins should only run when there is any change in my code on Git. For this you can do below -
- Navigate - Jenkins > project4 > Configure > Build Triggers > Click on "Trigger builds remotely (e.g., from scripts)"
- Set a password "Authentication Token"
- Click on Apply and Save.
- Write in post-commit file same as we written for push operation. In case of me my username is admin and password is redhat.
curl --user "admin:redhat" http://192.168.1.5:8080/job/project4/build?token=redhat
$ cat post-commit
#!/bin/bash
echo "Hello user! welcome to my Git Push
git push
curl --user "admin:redhat" http://192.168.1.5:8080/job/project4/build?token=redhat
That's all we have completed the task. You can test if you want.
cat >> index.html
For testing I am adding few more line
$ git commit index.html -m "Test the hook code"
Hello user! welcome to my Git Push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 358 bytes | 358.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/jay2tinku/project4.git
988b0cb..1ade3cb master -> master
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
[master 1ade3cb] Test the hook code
1 file changed, 10 insertions(+), 14 deletions(-)
rewrite index.html (75%)
$ git log
commit 1ade3cb10760fd0e96ba6d15bb3651a53ad6ebeb (HEAD -> master, origin/master)
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:18:54 2020 +0530
Test the hook code
commit 988b0cb105514e45a5c2aa755dce758e7042d8d4
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:03:25 2020 +0530
Last line
commit 2f49e921bddd5b78c098de639fca0c0993bf7b4f
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 15:00:46 2020 +0530
A new line
commit 8fcb83488e7c586c8a4b324ce26764d36e202d3a
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 14:49:51 2020 +0530
Commit for jenkins
commit e4a770d9efcb9f0e1294ffff75b36ae127c6afb6
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 13:33:21 2020 +0530
Few more changes
commit 5e180026d80f61d5e8dd85cb7ace3b927acab38f
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 12:55:51 2020 +0530
first update in home page
commit 26fd2aa6d1d66dcd0aa077a9599becb5952aaa73
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 12:52:10 2020 +0530
first update in home page
commit c4e11a7a8264d63497c8bf8b33af41a90830988d
Author: Rakesh Kumar <kumar.rakesh05@outlook.com>
Date: Sun May 3 11:20:05 2020 +0530
First commit of my web pages
Comments
Post a Comment
Please share your experience.....