Today I have created a Completed CI/CD Pipeline for a Website from Scratch using GitHub + Docker + Jenkins + Nodejs.
This Blog also has Documentation of it for exploration of how I created it.
Let's make a beautiful CI/CD Pipeline for your Node JS Application.
What is CI/CD Pipeline?
CI
orContinuous Integration
is the practice of automating the integration of code changes from multiple developers into a single codebase. It is a software development practice where the developers commit their work frequently to the central code repository (Github or Stash). Then there are automated tools that build the newly committed code and do a code review, etc as required upon integration.The
key goals of Continuous Integration
are to find andaddress bugs quicker
,make the process of integrating code across a team of developers easier
,improve software quality
andreduce the time
it takes to release new feature updates.CD
orContinuous Delivery
is carried out after Continuous Integration to make sure that we can release new changes to our customers quickly and in anerror-free way
. This includes running integration and regression tests in the staging area (similar to the production environment) so that the final release is not broken in production. It ensures to automate the release process so that we have a release-ready product at all times and we can deploy our application at any point in time.
Note: Before Heading to the Project first fork this repository from this link.
Fork from GitHub Repository
Task-01
Create a connection to your Jenkins job and your GitHub Repository via GitHub Integration.
- Let's create a
freestyle project
for forked repository in which we will connect Jenkins and GitHub repository via GitHub Integration.
Forked into my Personal Account
Creation of freestyle project on Jenkins
What is GitHub Webhooks?
- A GitHub webhook is a mechanism that allows GitHub to send a notification to an external service (such as Jenkins) when certain events occur in a GitHub repository, such as when a new commit is pushed, a pull request is opened or closed, or a new release is published.
Steps:
In your GitHub repository, go to "Settings" and then click on "Webhooks".
Click on "Add webhook" and specify the URL that GitHub should send the webhook to.
Select the events that should trigger the webhook. Optionally, you can add a secret key to the webhook for added security.
Click on "Add webhook" to save your webhook configuration.
Now Since this is our private repository we will create SSH Key Pair.
Public Key
-> GitHub Profile.
Private Key
-> Jenkins Profile.
So, Open Terminal:
ssh-keygen
- To see what is Public Key:-
cat ~/.ssh/id_rsa.pub
- Now paste your Public Key in
"Settings > SSH and GPG Keys > Create New Key > Give any name > Paste Public Key"
.
- To see what is Private Key:-
cat ~/.ssh/id_rsa
Now, we will paste the Private Key into Jenkins "Created_Project > Configuration > Source Code Management > Credentials > Add Jenkins > Choose SSH Private Key > Paste it"
.
Now we have connected everything between GitHub and Jenkins.
Let's start Project Testing On Jenkins.
Creation of freestyle project on Jenkins
Did the Basic Configurations in Jenkins
.
Final Output
Task-02
Let's first build Docker Image and run it on port 8000.
docker build . -t todo-node-app
docker run -d --name node-todo-app -p 8000:8000 todo-node-app
Final Output
On localhost 8000
In the Execute shell run the application using Docker Compose Down
docker-compose down
Final Output
Since docker downed the website so there is no connection
In the Execute shell run the application using Docker Compose Up.
docker-compose up -d
Final Output
Since docker is Up we have a website connected to CI/CD Pipeline.