Let's embark on a journey to automate our CI/CD pipeline using Jenkins and Docker Compose. The end to end process to make a beautiful CI/CD Pipeline for Node JS Application is here
Task-01
Fork this repository:
Let's start by forking the repository containing the project code. Forking creates a copy of the original repository under your GitHub account, allowing to make changes without affecting the original source.
To fork a repository, go to the repository on GitHub and click on the "Fork" button as shown below-
Create a connection to your Jenkins job and your GitHub Repository via GitHub Integration.
Let's create a Freestyle Jenkins Project via GitHub Repository.
- Click on New Item, enter a project name, select Freestyle Project and click on OK.
- Enter the description of the project, select GitHub Project and provide the github project url of Node JS app.
- Select Git in Source Code Management section and provide the Repository url. Since, the repository is Public, leave the Credentials selected as none.
- Click on "GitHub hook trigger for GITScm polling" under Build Triggers section.
- Save the above Configuration. A connection to the Jenkins Project and GitHub is made via GitHub integration.
Read About GitHub WebHooks and make sure you have CICD setup.
A connection to the Jenkins and GitHub is made in the above configuration in Jenkins but we also need to make connection from GitHub to the Jenkins so that any changes made in the code, triggers the build automatically in Jenkins. Webhooks allows the integration to take an action in response to the event that occur on GitHub.
A webhook is created by specifying a URL which subscribe to events that occur on GitHub. When an event occurs, GitHub will send an HTTP request with data about the event to the specified URL. If the server is set up to listen for webhook deliveries at that URL, it can take action when it receives one.
Let's setup a CI/CD using GitHub WebHooks.
- Login to GitHub and go the Node JS App repository. Click on settings.
2. On the settings page, there is a Webhook option present on left side bar. Click on Webhooks option to open the Webhook configuration page.
- Click on Add Webhook.
Under "Payload URL", type the URL where you'd like to receive payloads. The URL should be the IP Address of Jenkins server followed by "/github-webhook/". Ex- http://54.91.2.132:8080/github-webhook.
You can provide any secret key or leave it as blank.
Select "Send me everything" under "Which events would you like to trigger this webhook?"
To make the webhook active immediately after adding the configuration, select Active.
Click Add webhook
To validate whether the connection to github-webhook is established or not, update the code and commit the changes. As soon as the code commit is made, go to the Jenkins server and see the automatic build being triggered. Wait for some time for the build to complete. The automatic build has been triggered by GitHub push and the build is successful as shown below -
Task-02
In the Execute shell run the application using Docker compose.
Enter the command
docker-compose up -d
in Execute shell, save and build the jobCheck below the Success build started by Jenkins User.
Also, check the Console Output to validate the container has started or not.
To check whether the container has started or not run the command
docker ps
.Run the project.
The project is up and running. Launch the url of the application to validate the working of the application.
Hurray! An end to end Project using Jenkins, Docker and GitHub is created successfully.
Thanks for reading!
~Happy Learning!