Jenkins 02 | importance of Jenkins| job creation | integration with Github and webserver | automation from push to running a job

Jenkins 02 | importance of Jenkins| job creation | integration with Github and webserver | automation from push to running a job

ยท

9 min read

you have some idea and this idea you want to convert into solid form that is product for this we require some programming language let say java. Multiple guys work together to make the product for this they going to put their code in some centralized system which is called SCM ( source code management) one of the most SCM is github. our developers write the code in java and push in github

If you want to create some webapp for this you have to write the code so to run this webapp first you have to compile the code and also you have to create the package for which we use Build tools to compile and package and here we use famous build tool maven for java.

After we build the tools then we have to test the code we have different tools available maven can also do the testing like vlc player have pause, speed, sound, play and resume every button do different types of functionality to check whether every button work properly so we have to write different testcases for every functionality. This type of test is called unit Test.

after our code was uploaded in github then there is somebody who started building my code and after building we have to test.

The question is who will download this webapp for me and after download who will build the code and after building who will test the code.

but for installing testing tools like selenium or maven we required operating system for this who will install this os.

in testing phase let say some testcases fail like out of 10 two testcases fails. They will send back the feedback(report of testcases) surefire reports to developers and developer will fix the bugs they again upload the code in github. so if you already build setup for the webapp good practise is to do build the code again don't use the same build setup again. so we have to do again all thing like again we have to launch the os again install maven or selenium for building and testing. same process is going on hundred and hundred times because may be some bugs arises and we keep on fixing so if we use manual this requires lots of time and if we want to faster then we have to use some automation tools for this we use the famous tools jenkins. what jenkins will it keeps on monitoring when the codes change it will take care of build, launching os installing selenium it will take care of that. This is called as Continuous integration. Entire jenkins will take care of integration.

If you jenkins do you know how to connect github jenkins say no, how to connect maven jenkins say no for this we have to install plugins depending on whom you are integrating with.

after started create the user

then save and continue

now jenkins installation is completed and to connect with jenkins you have given a url

click on start using jenkins

jenkins have a capability to install the maven launching the os again and again. Since we have to do this faster and faster as main goal is to time to market the product so make it more faster we use containerization technology as it launch the os in less then 1 sec.

we have different steps from pulling to testing the code so we have to tell these steps to jenkins once then jenkins will run whenever required by putting these steps in the concept called jobs.

jenkins == project == item

so lets create a job in jenkins click on create a job

enter the name of job and since we want to make customisable job in which we can add steps those are known as freestyle type so click on it then click ok

In jenkins world running the job is called building.

so in the mayafirst job we are going to run the date command as it is in top of linux os so we can run all the command that linux can run.

since we are going to run a command in black screen that is shell

then write the command you want to run and then keep all thing save

to run the job click on build now and now we are able to see the history as it is single line command so it take less time to run

to see the output click on down arrow key behind #1 then click console output

if we want to change some code so click on configure

lets edit the job since we know there is no command like date123 in linux lets built what happens

then save it .

this time we get error when we click on build now

this cloud icon will show visually if the job is failing so much then it will change to thunderstorm, raining and so on.

after we fix the bugs

lets integrate jenkins with github

so for this you have to write email id and username of github

$ git config --global user.email "mayank07082001@gmail.com" $ git config --global user.name "mayaworld13"

then commit the webpage that you have made

git remote add origin https://github.com/mayaworld13/jenkinsmaya.git
git push -u origin main

if you have login already then there is no issue but if you now then provide username password to it after that you will see the web page that you have pushed in github after refreashing

jobs in jenkins have their own workspace in which they store artifacts

under source project management type the url of your repository

to avoid this error you have to install git on the instance where jenkins is running because jenkins know how to intigrate the github but git command to be know by the os in which the jenkins is running so thats why we install git command in instance

sudo yum install git

then click here there then type the url

and your entire data downloaded on workspace

now ...

let setup webserver

to setup the webserver in linux we have to configure it firstly

sudo yum install httpd

but i want to run this command by jenkins

then run this job and we will see that error arises due to user as a particular user have limited power and when you first time install jenkins in instance it automatically create the user of it own name and it run the job with this power by default.

like if we change the code run the job of whoami command using execute in console the click then we will see that command is run by jenkins and jenkins have no power

so for testing lets give jenkins the whole power (not good for practise only for testing)

after giving power u will see that the job is run by root user

now we get the root power so lets install the webserver

save it and build the job

and we see that service is started in instance

there are two jobs we have created

  1. setup web server

  2. deploy the webpages

but we only downloaded the webpages we have to add the steps to deploy the webpages so we have to write the code under execute in the console of gitproject1

save it and run the job

now lets do the changes in code and commit the changes

git commit radhey.html -m 'new changes'

and that all now when jenkins is keep on monitoring the changes in code in github but we have to manually click on build on jobs or run the jobs and when we click it

it will deploy the webpages let see

so that all guys thats how we integrate jenkin with the github and webserver

so here mulitple things are automated the one thing that is not automated here is that we have to run the job manually

But as a devops engineer we want to run the job automatically for this there is a concept in Jenkins have a concept called trigger.

Through the jenkins every time (time span we will given eitheri per min or per hour) monitor the scm and when some changes in code occur then download it and then deploy it and this trigger is known as Poll scm.

for doing backup type of jobs we use Build periodically

but we don't want to do this our requirement is that jenkins keep on monitoring github when some changes occur then jenkins auto download it.

this having the syntax of cronjob ( min hour date month day)

here we are going to run the job every minute

but here every time jenkins download the code even there is no changes in code which we don't want to do that's why we don't use this type of trigger instead we use poll scm trigger

so lets make the changes in configure of gitfirstjob and use poll scm trigger

In this jenkins will only download the code when some changes occur in the code lets do the changes and see whether it download automatically or not

see it started automatically when saw the changes in code

this time it says that it uses different trigger called scm change

so poll scm is good trigger but there are more powerful trigger are also available.

one thing also we want to automate that commit part that the developers do is manual in local system because its in hand of developer when they want to commit but we want to automate the git push part that is manual but how we automate the push part is by using git events

whenever you do anything in the git they are called events when you use git commit command then they know some event is happening. The point is that git know everything what is going on local system.

because git know what is happening so some part of automation we can do some trigger locally and what trigger will do is it will use git push behind the scene and this command we write in scripts and this scripts are known as Hooks.

so where your git project is running there is a hidden folder called .git inside git hook is available.

and since we want to push the code after commit we write the script inside hooks folder and file name for push is fixed where we will get the filename ( from documentation) and it is

vim post-commit

this echo command is only for my reference

make sure the file that you have made should be executable power

now we as a developer have to commit the code only else everything done automatically

so lets commit it and see what happens ( take some seconds)

I hope you guys enjoys the blog do share and like it

want to connect with me ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

My Contact Info:

๐Ÿ“ฉEmail:-

LinkedIn:- linkedin.com/in/mayank-sharma-devops

ย