Professional Documents
Culture Documents
How To Build and Deploy Application On Kubernetes With Ci/Cd Pipeline Using Jenkins, Docker, Harbor Private Repository, and Argocd
How To Build and Deploy Application On Kubernetes With Ci/Cd Pipeline Using Jenkins, Docker, Harbor Private Repository, and Argocd
Harbor Privat…
Getting Started
This is the general workflow
1. Docker File Creation: Intially, a Docker file needs to be created. In this file, a
DevOps engineer specifies the environment and dependencies needed for the
application.
2. Commit Code to the Repository: A repository, like Git, stores the source code and
the Docker file. I’m going to store the repository on GitHub.
3. Jenkins Pulls the Code: We can launch the Jenkins job to pull the most recent
code from the source code repository in order to begin the build process.
4. Building and Pushing the Docker Image: Jenkins uses the Docker file to build a
Docker image. Jenkins pushes the completed image to the Harbour private
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 1/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
repository. You can look at here to learn how to build the Harbour private
repository.
The Workflow
CI/CD Pipeline Using GitHub, Jenkins, Harbor, Argo CD, and Kubernetes
Implementation
Tools needed
Kubernetes Cluster
Docker
Jenkins
Open in app
Harbor
Search
ArgoCD
GitHub
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 2/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 3/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
You can find all the files in the GitHub repository below:
https://github.com/tanmaybhandge/Harbor_CICD_Pipeline.git
The first job (Upsteam job) involves cloning a GitHub repository to obtain the latest
source code. From this code, a Docker image will be created. Then it will build the
image, logs into a Harbor private registry, pushes the image, and tags it with the
build number of the upstream job.
After the image is successfully pushed, a downstream job will be start by the
upstream job. The downstream job will receive the build number as a parameter,
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 4/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
which will have the build number. Then downstream job will modify the image
version same as build number and will push the changes on the Kubernetes
deployment file.
When you have one Jenkins job that starts another, the job that initiates the process
is called the upstream job, while the job that gets started as a result is known as the
downstream job. In the below example Build_Docker_Image_Push_Harbor is the
upstream job and push_image_tag_git is the downstream job.
Job: Build_Docker_Image_Push_Harbor
This Jenkins pipeline script automates the process of building a Docker image from
a GitHub repository, pushing the image to a Harbor repository, and triggering
another Jenkins job push_image_tag_git to modify the deployment file on GitHub
and commit the changes.
pipeline {
agent any
stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git branch: 'main', url: 'https://github.com/tanmaybhandge/Harb
sh 'docker build -t library/harbor_cicd_v2 .'
}
stage('Push to Harbor') {
environment {
DOCKER_CREDENTIALS = credentials('Harbor')
}
steps {
script {
// Login to Harbor using credentials
sh "docker login -u ${DOCKER_CREDENTIALS_USR} -p ${DOCKER_C
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 5/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
}
}
Pipeline script
Stages
Here is the explanation of each stage
Build
Git Checkout: Clones the repository from the specified URL and checks out the
‘main’ branch.
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 6/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Docker Build: Builds a Docker image from the Dockerfile in the repository,
tagging it as library/harbor_cicd_v2 .
Push to Harbor
Docker Login: Logs into the Harbor registry using the retrieved credentials.
Docker Tag: Tags the built Docker image with a new tag that includes the Jenkins
build number. We will use the build number as the tag of the current pipeline.
Docker Push: Pushes the tagged Docker image to the specified Harbor
repository.
Pass Parameters: Sends the current build number to the triggered job.
Job: Push_Image_Tag_Git
This job will modify the Kubernetes deployment YAML file hosted on GitHub to
include the new image tag pushed by the upstream job. To achieve this, you’ll need
the following:
GitHub Plugin
A. Ensure the option This project is parameterised is selected, and then proceed to
configure the string parameter accordingly.
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 7/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
B. In the Source Code Managment section, select Git, paste the repository URL, and
choose the appropriate credentials which has the permission/access to your GitHub.
For the Branches to Build section, specify your branch. I’m using the default main
branch.
C. Add Execute Shell on the Build Steps and paste the following.
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 8/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Build Steps
This script locates the image tag in the Deployment.yaml file, replaces it with the
specified version from the Jenkins build (${Build_Number_Image}), configures the
email and name for Git commits, adds all modified files to the Git staging area, and
commits the changes with a descriptive message.
D. In the Post-Build Actions section, select ‘Push Only If Build Succeeds’ Then,
specify the branch name and the target remote name under the ‘Branches’ field. I
am specifying branch name as main and the target remote name as origin
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 9/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Build_Docker_Image_Push_Harbor
Push to Harbor Stage: Logs into a Harbor registry, tags the image with the build
number, and pushes it.
Push_image_Tag_Git
Execute Shell: It will locate the image tag in the Deployment.yaml file, replace it
with the specified version from the previous Jenkins build, configures the email
and name for Git commits, adds all modified files to the Git staging area, and
commits the changes with a descriptive message.
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 10/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
To ensure synchronization between the Docker image build and the Kubernetes
Deployment, the version specified in the Deployment.yaml file will be updated to
reflect the build number generated during the Build_Docker_Image_Push_Harbor
CLUSTER: https://kubernetes.default.svc
NAMESPACE: webapp
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 11/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
PATH: Deployment
You may need to create the webapp namespace in your Kubrnetes Cluster.
I have exposed the deployment via a NodePort service for testing purposes. To
access the application, use the service port along with the IP address of the worker
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 12/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
node. This setup allows easy access to the webpage for testing and validating the
application’s functionality.
apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
If you’ve made it this far, I hope you’ve found this enjoyable to read! If you have any
questions or just want to connect, feel free to reach out to me on LinkedIn. Cheers!
Follow
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 13/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Tanmay Bhandge
77
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 14/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Tanmay Bhandge
Tanmay Bhandge
Introduction
81 2
Tanmay Bhandge
18
Spacelift in Spacelift
68 2
16 1
Lists
Productivity
241 stories · 452 saves
Nidhi Ashtikar
19 2
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 18/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
61
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 19/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…
Faisal Kuzhan
108 5
Project 48: Implementing Azure IaC DevOps for Terraform Project: CI/CD
with Azure DevOps
Scenario:
63
https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 20/20