Hello Everyone ! I have learnt basics of jenkins and tried to deploy node js and python application thru it. I have used freestyle and pipeline projects. Hope it will be useful.
What is Jenkins
Jenkins is a CICD tool used for automating jobs and helps developers to build, test and deploy software. . A job is nothing but an automated process of building our source code. It performs CI continuous integration and CD continuous delivery.
Continuous Integration - It is a process of ensuring whenever a developer pushes his code into GitHub it should be build using build tools like docker and test the code. If it passes then it will move forward otherwise it will send to the developer again to correct it.
Continuous Delivery (CD) is the practice of continuously building, testing and preparing software for release, but not necessarily releasing it to production automatically.
Continuous Deployment (CD) takes CD a step further and automatically releases code changes to production as soon as they pass all tests.
CICD - Continuous Delivery
Lets implement continuous Delivery.
We are using docker-compose.
Steps
Code--Build--Deliver
Code
Let's link my GitHub repo URL of the project. Here we are not adding any credential because this repo is public.
Project repo branch is main. so give main as branch name.
Build
Give commands to execute.
docker-compose up
: start and run the defined services in the docker-compose.yml
file.
-d
: run the services in the background.
--no-deps
: don't start linked services.
--build web
: build the 'web' service only, ignoring the other services defined in the docker-compose.yml
file.
and build the application.
Deliver
Here we clicked on build to build the project. It means this project is ready for release(deployment)
Now lets perform continuos Deployment which mean automatically releases code changes to production as soon as they pass all tests.
Continuos Deployment
Adding a webhook will automatically deploy the app whenever there is a change.
Goto github project repo --setting--webhook and fill the below details. Add IP address of the instance with jenkins port and webhook as below.
It should display as a tick.
Follow same steps as above and additionally add webhook trigger as below.
Now I will make some changes in my readme file. Here I committed changes in a new branch and merged with main branch. As soon as some changes occurred in main branch, github webhook will trigger the IP address (link) and automatically deploy(code,build,test,deploy) the application as below.
CICD pipeline
Add project repo URL from github
Add groovy script which performs cloning the code, building, testing and deploying
Groov
Groovy is commonly used for writing Jenkins pipeline scripts due to its concise syntax and versatility.
pipeline {
agent any
stages{
stage('Code'){
steps{
git url: 'https://github.com/AasifaShaik029/node-todo-cicd.git', branch: 'master'
}
}
stage('Build'){
steps{
sh 'sudo docker build . -t aasifa/node-todo-test:latest'
}
}
stage('Test'){
steps{
echo "Testing completed successfully"
}
}
stage('Push'){
steps{
withCredentials([usernamePassword(credentialsId: 'DockerHub', passwordVariable: 'dockerHubPassword', usernameVariable: 'dockerHubUser')]) {
sh "sudo docker login -u ${env.dockerHubUser} -p ${env.dockerHubPassword}"
sh 'sudo docker push aasifa/node-todo-test:latest'
}
}
}
stage('Deploy'){
steps{
sh "sudo docker-compose down && sudo docker-compose up -d"
}
}
}
}
This is a Jenkins pipeline script written in Groovy that automates a Continuous Integration and Continuous Deployment (CI/CD) pipeline for a Node.js application.
The pipeline consists of five stages:
Code: Clones the code from the Github repository 'github.com/AasifaShaik029/node-todo-cicd.git' using the Git command.
Build: Builds a Docker image using the 'sudo docker build' command and tags it with 'aasifa/node-todo-test:latest'.
Test: Echoes a message "Testing completed successfully".
Push: Pushes the built Docker image to DockerHub using the 'sudo docker push' command. The DockerHub credentials are stored as a Jenkins credential and retrieved at runtime.
(note: DockerHub id an ID which should match with the Id in the credentials page)
make sure you have enabled env plugin.
Deploy: Brings down any existing containers and starts new containers using the 'sudo docker-compose up -d' command.
Add credentials of the dockerhub (manage jenkins - credentials - add docker hub credentials)
Build the project
Our pipeline ran syccesfully.
So far we have performed continuous delivery, deployment and pipeline deployment.
--Thank you for your time. Meet you in my next blog :)--