Table of contents
- Prerequisite
- Fork Project To Your GitHub Account
- Create EC2 Instance (Virtual Machine)
- Installation Off Jenkins
- Connection Between Jenkins Job & GitHub Repository via GitHub Integration.
- Adding SSH & GPG keys In GitHub And Jenkins Project
- Create Jenkins Project
- Automate Building And Running Containers
- Automate Deployment Using Webhook
Hey, I am going to Integrate and Deploy the Node JS To-Do app using EC2server, GitHub, Docker,
and most important SSH connection between Jenkins Job and GitHub
and Jenkins with a GitHub hook trigger for GITScm polling
feature of Jenkins.
Prerequisite
Git Understanding
GitHub Account
AWS Account for the Virtual Machine(EC2 Instance)
Docker Understanding
Docker Compose Understanding
Jenkins Understanding
Note: Here we will use AWS EC2 with Ubuntu Linux OS
Now Let's get started implementing the complete Jenkins CI/CD pipeline
Fork Project To Your GitHub Account
You can use this TO-DO app in case you do not have any project
Here is the link TO Do App Git Repo
Create EC2 Instance (Virtual Machine)
Here we will use Ubuntu as an AWS EC2 Instance(Virtual Machine)
Please check here-> How to Create EC2 Instance step by step
Now we will install Jenkins on this EC2 instance and below are the commands.
Installation Off Jenkins
Please Check Here -> Installation of Jenkins on Ubuntu
Connection Between Jenkins Job & GitHub Repository via GitHub Integration.
To Create an SSH connection between your Server(EC2 Instance)
and GitHub
we need SSH key
, Let's create it.
Connect your
EC2 instance
and go to theTerminal
and use the belowssh-keygen
command to generateSSH key
ssh-keygen
You can see the 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:"
Adding SSH & GPG keys In GitHub And Jenkins Project
Go to
GitHub
>>Setting
>>SSH and GPG keys.
Click on
New SSH key
Provide the
Title
and select theKey type
asAuthentication Key
and also addpublic key
in the key field that we created using ssh-keygenClick on
Add SSH Key
and provide your GitHub password to confirm and you will see the SSH key is added
Create Jenkins Project
As earlier we have installed Jenkins on our server and make sure port 8080
is open in the server security group to access the Jenkins dashboard
Here we will use Docker, Docker-compose
to deploy the application in the Docker container
Access the
Jenkins dashboard
usingserver ip
withport 8080
as shown below<server-ip>:8080
Now go to the
dashboard
and createNew Item/Create a job
Enter the
Item/project Name
and select theFreestyle project
and click onOk
Now once you created the job enters the
Description
and select theGitHub project
and copy and enter the GitHub repository project URL asProject url
Now in
Source Code Management
enter the same repository url we have copied asRepository URL
fieldIn the
Credentials
section click onAdd
Now in the pop-up window keep
Domain
as it isIn the
Kind
selectSSH Username with private key
In the
Scope
keep it as it isIn the
ID
provide as per yourIn the
Description
provide as perIn the
Username
provide your server username in my case it isubuntu
In the Private key add the
private key
which we created using thessh-keygen command
and click onAdd
Now in Credentials select which we have created now
In the
Brances to build
keep theBranch Specifier
as it is in my case it is*master
Automate Building And Running Containers
Note: make sure Docker and Docker-compose
should install on the server
Please check Here -> Install Docker on Ubuntu Server
To install Docker-compose use sudo apt install docker-compose -y
command
Now in the
Build Step
underExecute shell
run the application usingDocker compose
add the commandApply and Save
For this first, we have to create the
Docker compose file
to deploy theDocker container
It is best practice to keep theDocker compose file
in theRepository
Note: If you got a
Permission denaied
related error while buildingAdd the
Jenkins as user
in thedocker group
Use the below command and reboot the serversudo usermod -a -G docker jenkins sudo rebbot
After Click on
Build Now
and after successfully build go theConsole output
and check as asuccess
the message at the endNow you can see the
Docker container
is deployed and up and runningNow copy your Instance Public-IP address and add
:8000/todo
to access the application as below[Instance-public-ip]:8000/todo
BBBBoooooooooMMMMM Congratulation we have successfully deployed our application using docker πΊπΊππππ
Automate Deployment Using Webhook
Install Pugin:
Here we need the
GitHub Integration
pluginOpen your
Jenkins dashboard.
Click
on theManage Jenkins
button on your Jenkins dashboardClick
onManage Plugins
Go to the
Available plugin
Search
GitHub Integration
plugin in thesearch box
Click
onInstall without restart
Click on Restart Jenkins when installation is complete.
Relogin into Jenkin
GitHub-Webhook Configuration:
Go to your
GitHub repository
and click onSettings.
Click on
Webhooks
and then click onAdd webhook.
In the
Payload URL
field copy and paste yourJenkins environment URL
. and at the end of this URL add/github-webhook/
In the
Content type
select:application/json
and leave theSecret
fieldempty
.In the
Which events would you like to trigger this webhook?
section keepJust the push event.
and click onAdd webhook
Refresh
the page until you will get theRight sign
βGo to
Jenkins
>>Your Job
>>Configure
>>Build Triggers
and selectGitHub hook trigger for GITScm polling
and click onapply and save
Now we are at our final step make some changes in the code and watch the Jenkins dashboard
Here we have changed the heading
"Welcome To The Amazing & Awesome DevOps World"
to"Welcome To the DevOps Community"
and committed the changes at the repository itselfAfter we have changed the code in the repository and committed the changes the
magic happens
πΊπΊππππ... Our Job build execution starts without clinking onBuild Now.
WebHook
isadded to trigger
theexecution of Jenkins jobs
based onGitHub events
. If anyone in the developer team changes the code, it will automate the build now process like this.We can also see the
docker container
running in the command lineAnd Finally!!! the changes have been reflected as well
BBBBoooooooooMMMMM Congratulation we have successfully automated to trigger the job using Webhook πΊπΊππππ
Thank you for reading the article.
Thanks for your valuable time.
Happy Learning !... Keep Learning ! π