Creating Freestyle Jenkins Pipeline.

Hey, I am going to Integrate and Deploy the To-Do app using EC2server,GitHub, Docker,and most important Jenkins.
Below are the prerequisite.
Prerequisite.
- Git Understanding
- GitHub Account
- EC2 instance on AWS
- GIT Understanding
- Docker understanding
- Jenkins understanding
Fork Project to your GitHub account if you do not have your any application.
You can use this TO-DO app in case you do not have any project.
Here is the link.

Your repository will look like the above screenshot.
Now, Let's create a token to access this repository.
You can create a token just to follow the below steps;
Click on your GitHub Profile then click on Settings
Go to Developer Settings
Click on Personal Access Token
Click on Token(Classic)
Click on Generate New Token

6. Click on Token(Classic) again
- It will ask Password , Put your password
Git Note description
Provide Expiration time as per your convenience
Check the Select scopes as per the requirement.
I checked scope : repo,workflow
Now click on Generate Token
Copy that token and save it at your safest place because you cannot find it again.
Let get EC2 Ready and Install Jenkins.
I hope you know how to create EC2 instance so I am not explaining that steps here.
Still need help then you can refer this link.
How to Create EC2 instance step by step
After creating Instance here we are connected to it.

Now we will install Jenkins on this EC2 instance and below are the commands.
Sudo apt update // Update the server
Jenkins needs the Java runtime as it is based on Java. Install open JDK 11 package using the following command.
sudo apt install openjdk-11-jre
Now let's check the Java version if it is installed or not.
$ java --version
Add the Jenkins Debian repo.
curl -fsSL | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
Then add a Jenkins apt repository entry:
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
Note: Maybe when you come to my this blog above details may change so if you are facing any issues please refer to the official doc for the same.
https://pkg.jenkins.io/debian-stable/
Now again update the system:
sudo apt-get update
sudo apt-get install fontconfig openjdk-11-jre
Now Install Jenkins.
sudo apt-get install jenkins
Start the Jenkins service & enable it for start during bootup.
sudo systemctl start jenkins
sudo systemctl enable jenkins
Now Let's check Jenkin's status.
sudo systemctl status jenkins

Now to run Jenkins we need to expose port 8080 as Jenkins by default run on port 8080
Below are the steps to expose port 8080
Steps 1 : Go to your server's security and click on the group link

Step 2 : Click on Edit inbound rules
Step 3 : Click on Add Rule and expose port 8080 then Save

Note: I have used Source as MY IP for the security reason but you can use as per your requirement.
Step 4 : Go to the browser and hit your public:8080
for example 123.23.2.3:8080
You will get below home page

ou can Password location already indicated on the Home page as
/var/lib/jenkins/secrets/initialAdminPassword
Step 5 : Go to your terminal and type
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
It will show your password like below.

Step 6 : Copy this password and put it into Jenkins Home pass which is asking for a password.

Step 7 : Click on Continue, You will see the below screen.

Step 8 : Click on Install Suggested Plugins
You will get below screen and Jenkins will install required plugins.

Step 9 : After installing all required plugins you will below screen.
Putt all the required information and same as below.

Step 10 : Click on Save and Continue then Save and Finish.
Your Jenkins is READY!!!!!

Create CICD Pipeline and make the application running.
Now we are ready to create our CICD pipeline let's start.....
Step1 : Click on New item.

Step 2 : Enter Item name and select freestyle project then click OK

Step 3 : Give a Description then select GitHub Project same as below

Step 4 : Now provide Project's SSH address in Project URL.


Now select Git under Source Code Management and paste GitHub URL


Now we have to create credentials for the Credentials Drop-down, currently, we have none.

To Create SSH connection between your server and GitHub we need SSH key, Let's create it.
- Go to your terminal and type "ssh-keygen"
You will get below inputs.

ou can see message as "Your identification has been saved in /home/ubuntu/.ssh/id_rsa Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub The key fingerprint is:"
Step 2 : Navigate to .ssh directory
"cd .ssh"
Step 3 : You will see below files here
authorized_keys id_rsa id_rsa.pub

Here id_rsa is private key and id_rsa .pub is public key
Step 4 : Cat id_rsa.pu and copy the content.

Step 5 : Go to GitHub>> Setting>> SSH and GPG keys.

Step 6 : Click on Generate SSH Key.
Step 7 : Add Tittle then put that copied public key in Key box.

Click on Add key>> Provide Password
You will be able to see your key in Authentication refer below.

Step 4 : Now come to your Jenkins and Click on Add+ button in your credentials and select Jenkins icon.

Step 5 : New window will open >> Select Kind drop down value as SSH Username with private key. and add ID whatever you want.

Step 7 : Provide Description and user name of your instance and select Private key as Enter Directly and paste your.


Step 8 : Click on Add and come back to Credentials on the same page , you will see your Description here which you mention.

Step 9 : Now Save and you will see below screen

.
Step 10 : Click on Build Now and my code built successfully.

Click on Green Right sign icon, It will show what action has been performed by Jenkins.


Step 11 : Now, Let's run the code.
Go to the path and find and open Readme file.

Step 12 : Follow the commands mentioned in README file.

sudo apt install nodejs sudo apt install npm
npm install
node app.js
After running all above command you will see below result.

It is saying application is running on port 8000
Step 13 : Expose port 8000 in your security group same as we have done for Jenkin's port 8080.
After Allowing port hit the IP:8000
You will get the result like below.

But this application will be stop if I close my Terminal so to deal with problem we will install this application on Docker.
Run the Application on Docker.
Step 1 : Install Docker on your server
sudo apt install docker.io
Step 2 : I will create a Dockerfile
sudo vi Dockerfile
Step 3 : I want a container with inbuilt Node JS
I will write in Dockerfile
FROM node:12.2.0-alpine //lite image
WORKDIR app // I have created a directory in which my app will run
COPY . . //copy code from system to image
RUN npm install // install npm
EXPOSE 8000 //expose port 8000
CMD ["node","app.js"] //run the command to run app.js
Step 4 : Now build the container from Dockerfile
sudo docker build . -t nodetodoapp
Here docker build is command to biuld container
-t nodetodoapp //here -t means tag and nodetodoapp is the name of tag.
You will get similar kind of results to referring below.

Step 5 : Run the docker in demon mode.
sudo docker run -d --name node-todo-app -p port 8000:8000 nodetodoapp
Here sudo docker run -d is refer to run docker in demon mode
--name node-todo-app refer container name
-p port 8000:8000 exposing port 8000 of container and mapping with server's port 8000
nodetodoapp:latest // build container name
Now your application is running again.
Automate building and running Containers.
- Go to Configure then Build Step>>Select Execute Shell and paste below commands.
docker build . -t nodetodoapp docker run -d --name node-todo-app1 -p 8000:8000 nodetodoapp

- Save
- Click on Build Now

Note : if you got a Permission related error while building.
Do : sudo usermod -a -G docker jenkins
sudo systemctl restart jenkins
- 3.1 : Login into Jenkins and rebuild the project
Now your application is up and running

Automating Deployment by Webhook.
Step 1 : Go to Manage Jenkins
Step 2 : Manage Plugins>>Available Plugins
Step 3 : GitHub Integration
Step 4 : Select Install without restart
Step 5 : Select Restart Jenkins when installation complete.
Step 6 : Relogin into Jenkins
Step 7 : Go back to GitHub go to Repository's setting

Step 8 : Go to Webhooks>>Add Webhook>> Ask for Password
Step 9 : Add Payload URL of Jenkins
like below http://3.109.60.82:8080/github-webhook/
Note: You have add after oprt/github-webhook/Step 8 : Go to Webhooks>>Add Webhook>> Ask for Password
Step 9 : Add Payload URL of Jenkins
like below http://3.109.60.82:8080/github-webhook/
Note: You have add after oprt/github-webhook/

Step 10 : Select Content Type as Application/jason then Add webhook.

Step 11: Refresh the page untill you will get the Right sign as below.

Step 12 : Go to Jenkins>>Your Job>> Configure>>Build Triggers.

Step 13 : Select GitHub hook trigger for GITScm polling >> Save
Now, Let's change code in GitHub and as expected Application Deploy automatically.
Step 14 : Go to GitHub Project and make the in todo.js file >>Save

Its Deployed!!!!!!!!!!!!!!!!!
Woohooooooooooo


I hope you enjoyed the Journey to creating freestyle Jenkins Pipeline.
Happy Learning Keep shining.
If you like the article, or have any doubt ...
Follow me on Linkedln
Saif Ali



