Introduction
Jenkins, an open-source automation server, plays a pivotal role in achieving continuous integration and continuous deployment (CI/CD). In this guide, we'll walk through the process of setting up a basic Jenkins pipeline to streamline your software delivery.
Note: This blog depicts the demonstration of setting up a basic pipeline step by step after setting up Jenkins. So after setting up Jenkins you may head to following steps to create a pipeline for continuous integration and deployment of your software.
Create a Jenkins Pipeline:
First step will be to give your project name with selection of pipeline functionality offered by jenkins.
This would led to another interface which will have so many options, but for now we will only look for github project. This is important to make your software present on github to ensure continuous integration and deployment. You can grab your github project url and paste it here as it is displayed below.
after creating kindly save this project for a moment. Head on to Dashboad in order to create a credentials. Now,
Why to have credential? / Why it is necessary?
This is because when we want to push the code image there would be a need of your username and password. Suppose you are working in a team , where your jenkins dashboard would be having access to many people. If you would be writing your docker hub username and password in your groovy syntax pipeline creation code, than it would be risky to openly write your password in public. Therefore, we have another option of providing credentials by which we can access dockerhub alot easier way.
steps to create credentials:
Go to dashboard
manage jenkins
Under security, click on credentials
- head to global word and click on it as displayed below.
- Click on (+ Add Credentials)
Provide the required information asked for creating credentials. Such as your dockerHub username with password. Note kindly keep the kind option as "Username and Password". Make your id unique and rememberable as I have made such as "dockerHub". You can provide common description for it.
Now you have to write the code to create pipeline:
pipeline
{
agent any
stages
{
stage("Code")
{
steps
{
git url: "https://github.com/v-aryan/node-todo-cicd.git", branch: "master"
echo "code is clone"
}
}
stage("Build & Test")
{
steps
{
sh "docker build -t node-app ."
echo "code building is also done"
}
}
stage("Scan Image")
{
steps
{
echo "Image scanning is done"
}
}
stage("Push")
{
steps
{
withCredentials([usernamePassword(credentialsId: "dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")])
{
sh "echo ${env.dockerHubPass} | docker login -u ${env.dockerHubUser} --password-stdin"
}
}
}
stage("Deploy")
{
steps
{
sh "docker-compose down && docker-compose up -d"
echo "code is deployed"
}
}
}
}
The Stage "push" has the code which is also available on documentation if you have any doubt in case. Here excluding the echo command everything would be written into sh "... " as sh stands for shell commands.
Kindly save the work you have untill now.
Head on to terminal, to install docker.io and docker-compose
sudo apt intall docker.io
sudo apt install docker-compose
We also have to chang the user permission for different users, so to do that we will input the commands listed below on terminal
sudo usermod -aG docker jenkins
sudo usermod -aG docker ubuntu
sudo usermod -aG docker $USER
Kindly write "sudo reboot"
in order to update the change of installation and permission to get updates and reflect on jenkins software.
reconnect and relogin the jenkins software.
Edit inbound rules in aws security option(OPEN port 8000 Ipv4) and save the changes.
Kindly click on "build now" in jenkins.
If every step of yours is correct in journey of making the pipeline than there will be green boxes displayed under your project process as displayed below.
The port which earlier you had opened, kindly access the "Todo application" through instance public ip in format eg: 3.45.343.67:8000.
That's the final output , that you have run this application on your localhost ip which will ensure smoother continuous integration as well as deployment.
Conclusion:
we embarked on a journey to set up a basic Jenkins pipeline, a fundamental component in modern software development practices. By following the steps outlined, you've laid the groundwork for a robust continuous integration and continuous deployment (CI/CD) system.
quote for the day:" Fall seven times, stand up eight."