Table of contents
- 🎆Introduction
- 🧛♂️🐳Create Jenkins Declarative Pipeline with - Docker and GitHub, Webhook🔗🔀
- 🔑Prerequisite
- ⏭️Create The Pipeline
- 🔧Configure The Pipeline
- 🧬Clone Stage
- 🛠️Build Stage
- 🚀Push Stage &Configuring The DockerHub Credentials
- 🏭Deploy Stage
- ⚙️Execute The Pipeline
- ⚠️Error - 1
- ⚠️Error - 2
- ⚠️Error - 3
- 👏🎉🏆Successful Execution 🌟💯
- 🔝CI/CD Automation
- 📦Executing the Jenkins File from GitHub
- 🏗️Automate Deployment Using GitHub-Webhook
- 🔗GitHub-Webhook Configuration
- ✔🔗Checking The WebHook Integration
- ✨Conclusion
Day 27 Task Of 90DaysOfDevOps Challenge
🎆Introduction
As of now, we are familiar with the Jenkins Declarative Pipeline
Also, we are pretty familiar with Docker
and Git
, GitHub
.
In this blog, we are deep dive into the Integration of Docker
, GitHub
, Webhook
with Jenkins and we will take our skills to the next level.
🧛♂️🐳Create Jenkins Declarative Pipeline with - Docker and GitHub, Webhook🔗🔀
In this section, we are going to create a Jenkins Declarative Pipeline with Docker to host our application in a Docker container and GitHub to get the application code from the GitHub repository.
🔑Prerequisite
Make sure the following tools are installed and running in your system. Here we require the following tools...
🧛♂️Jenkins:
To create the Jenkins Declarative Pipeline. Check out the link to install Jenkins. https://deepakcloud22.hashnode.dev/day-22-getting-started-with-jenkins
🐳Docker:
Run the docker container to host our application. Check out the link to install docker https://deepakcloud22.hashnode.dev/day-16-task-docker-for-devops-engineers-part-1
🏢Docker Hub
: To push the Docker image into the Docker Hub repository. Check out to create the Docker-Hub account https://docs.docker.com/docker-id/
🏢Git/GitHub Repo:
To get the application code from the GitHub https://www.linkedin.com/pulse/day-8-task-basic-git-github-devops-engineers-deepak-patil/
📝Application Code:
We will use the below application code you can clone the code or fork this repository https://github.com/Dips-1991/django-notes-app.git
⏭️Create The Pipeline
First, we will create the Pipeline. As we have installed Jenkins in our system.
Navigate to the Jenkins home page and
click
onNew Item
.Enter
name
of the job.Select the
Pipeline
as a project and click onOk
.Enter the
Description.
Select the
GitHub project
As we know we are integrating GitHub with Jenkins and adding the GitHub repo URL to theProject url
As we know our application code is present on GitHub.
🔧Configure The Pipeline
Now, come to our main task here we will write the Ddeclerative pipeline Groovy syntax code.
🧬Clone Stage
Pipeline section:
In theDefinition
select thePipeline script
from the drop-down.
🛠️Build Stage
Script section:
Understages
in the firstclone
stage
we are cloning the code. To clone the code we have to use theGitHub URL
and theBranch Name
where our code is present.git url:"<github-repo-url>,<branch-name>"
In the second
Build
stage
we are building the docker image using the Dockerfile. Here we have to useGroovy syntax
to run the docker command with the help ofsh.
sh "docker build -t Django-app-image ."
🚀Push Stage &Configuring The DockerHub Credentials
In the this
stage
, we are pushing the image to DockerHub. For that First, we will save our credentials in Jenkins and we are associate them with theID
.As we are aware that exposing the credentials is how dangerous so, we are using here the Jenkins environment variable.
Navigate to the
Manage jenkins --> Security --> Credentials
Click on the
System
under theStores scoped to Jenkins
Once, you click on the System you will navigate to the next page where you have to click on
Global credentials (unrestricted)
.Once you click you will navigate to the next page click on
Add credentials.
Once, you click on
Add credentials
.You will navigate to the next page where we will store ourDockerHub credentials.
Here in
Kind
selectUsername with password
from the dropdown. As we are using DockerHub credentials.Keep
Scope
as it is.In the
Username
add theDockerHub Username
.In the Password add the
DockerHub Password
.Keep
Treat username as secret
uncheck.Enter the
ID (credentials are associate with ID)
name at your convenience. ThenID
We will use in our code to fetch the username and password.Click on Create
.Now you will see the credentials are added.
In the
Push Image
stage
We will push the image to DockerHub with the help ofwithCredentials
function and will pass thecredentiasId
,usernameVariable
,passwordVariable
to the"usernamePassword"
function.withCredentials([usernamePassword(credentiasId:"<your-credentials ID>",usernameVariable:"<any-name>",passwordVariable:"<any-name>")])
In the next command, we have to tag our created image with the DockerHub username.
sh "docker tag <image-name> $env.<usernameVariable>/<new-image-name:<tag>>"
In the next command, we will
login
to the DockerHubsh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}".
In the next command, we will push the image into the DockerHub.
sh "docker push $env.<usernameVariable>/<new-image-name:<tag>>"
🏭Deploy Stage
In this
stage
, we are deploying our application into the Docker container usingdocker-compose
the file.sh "docker-cmpose down && docker-compose up -d"
Make sure
docker-compose
is installed in your system.Here we have to replace your
image name
inimage
statement inDocker-Compose File
, Image that you have pushed into theDockerHub
. OurCompose-File
will fetch that image fromDockerHub.
Also, we are exposing the
ports
here(host-port:container-port)
in theports
statement.Here is the
Docker-compose
file.version : "3.3" services : Web : #Image which we have pushed into DockerHub image : deepak1603/django-app-image:v1 ports : - "8000:8000"
Now, In the final step click
Apply and Save
.
⚙️Execute The Pipeline
Now, Go to the Dashboard/Create project pipeline and click on Build Now.
The
pipeline execution
will start ...Code will beColning
fromGitHub
,Docker image
will be created,Image tagging
will be happening,DockerHub login
will be happening, andPushing image into DockerHub
will be happening and finally, theDocker Container
will be created with a single click .......Let's see the magic.
⚠️Error - 1
Once built here we are not able to build successfully because we have given the image name in capital letters see the below error.
So we are changing the
Image Name
here withdjango-app-image
Wherever applicable click onApply and Save.
sh "docker build -t django-app-image ."
Now after saving click on
Build Now.
again.
⚠️Error - 2
Now, here we got another error which is regarding the
Permission denied
becauseJenkins User
is not part of theDocker Group.
We have to add
Jenkins User
into theDocker Group
and Reboot the systemRun the below command in the terminal.
sudo usermod -a -G docker jenkins sudo systemctl reboot
Now after adding
Jenkins User
to theDocker Group
.Now after saving click on
Build Now.
again.
⚠️Error - 3
Now, here we are getting one more error
withCredentials step must be called with a body
.So make sure
withCredentials
is a function as we know, so we have to write the code inside the{ }
brackets.Here is the updated code of the
withCredentials()
function.withCredentials([usernamePassword(credentialsId:"DockerHubCredentials", passwordVariable:"dockerPassword", usernameVariable:"dockerUsername")]) { sh "docker tag django-app-image ${env.dockerUsername}/django-app-image:v1" sh "docker login -u ${env.dockerUsername} -p ${env.dockerPassword}" sh "docker push ${env.dockerUsername}/django-app-image:v1" }
After updating the changes again click on
Build Now
. And let's see this time.
👏🎉🏆Successful Execution 🌟💯
Now, Finally, we are running our pipeline successfully you can see the Console output
also the Pipeline Stage View
.
We are able to push the image
into DockerHub.
Now finally we can check whether the Docker container is created or not.
🕺🕺👏👏🎉🎉 BoooooooooooooMMMM we are able to deploy the application successfully.
Now it's time to access our application from the browser.
To access the application copy the EC2 instance IP Address and paste it into the browser along with :8000
, Because our application is running on Port 8000
.
🕺🕺👏👏🎉🎉 BBBBBBBoooooooooooooMMMMMMMMMMMMMM we have successfully deployed our application using the Jenkins pipeline.🕺🕺👏👏🎉🎉
🔝CI/CD Automation
Now, it's time to make our Jenkins pipeline fully automated.
As of now, we are writing our pipeline code into the Jenkins script section
.
Now, we will save our script code
into GitHub
also in the form of Jenkinsfile.
and we are fetching the script code from GitHub instead of writing it into Jenkins.
Here is the Jenkins file(same above script code)
. We will store it at GitHub
in the application code itself.
Here is the Jenkins file we will store on GitHub in the application code itself.
📦Executing the Jenkins File from GitHub
To Call Jenkins file
From Jenkins, we have to make some modifications in the configure section shown below.
Goto
Dashboard
and click onApplication Pipeline.
Goto
Pipeline
inDefinition
selectPipeline script from SCM
.Under
SCM
selectGit
.Under
Repositories
enter theGitHub URL.
In
Brach Specifier
underBranches to build
Enter your branch name in my case it ismain.
In
Script Path
enter theJenkinsfile
as path then click onApply and
Save
.
🏗️Automate Deployment Using GitHub-Webhook
Now, comes to the CI/CD Automation Using GitHub Webhook
.
We will configure our Jenkins file
to be executed after any changes are updated in the GitHub Repository.
To make it possible we will use here the Github Webhook.
Go to the Pipeline configure page in
Build Triggers.
Select the checkbox
GitHub hook trigger for GITScm polling
Click on Apply and Save.Also, we required
GitHub Integration
plugin got to plugin and install the required plugin.
🔗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 select as per your requirement and click onAdd webhook.
Refresh
the page until you will get theRight sign
✔
✔🔗Checking The WebHook Integration
We will Change the title from TVS Jupiter Is Amazing
to DevOps is Amazing
.
After we had changed the code in the repository and committed the changes the magic happens
🕺🕺👏👏🎉🎉... Our Job build execution starts without clinking on Build Now.
WebHook
is added to trigger
execution of Jenkins jobs
based on GitHub events
. If anyone in the developer team changes the code, it will automate the Build Now process
.
✨Conclusion
✔In this post, we combined GitHub Jenkins Declarative Pipelines and Docker for enhanced CI/CD.
✔We used the sh
command to run Docker commands within pipeline stages.
✔Then, we explored Docker Groovy syntax in the script
block for smoother image creation and container handling.
✔We have done troubleshooting.
✔We have learned about Jenkinsfile
✔We have implemented the Wbhook call successfully.
Check Out the Video for Complete CI/CD Using Jenkins, Docker, GitHub, and Github-Webhook.
Thank you🙏🙏... for taking the time to read this blog. I hope you found the information helpful and insightful. So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on
So, Stay in the loop and stay ahead in the world of DevOps!
Happy Learning !... Keep Learning ! 😊