This repository demonstrates a Jenkins pipeline that automates:
- Checkout source code from GitHub
- Build & Push a Docker image to a container registry
- Deploy the application to a Kubernetes cluster
The pipeline uses the following key variables:
def dockerfile = 'dockerfile'
def registry = 'localhost:5000'
def imageName = 'myimage'
def imageTag = 'latest'
def manifestFile = 'kubernetes-manifest.yaml'
def namespace = 'default'
def IMAGE_BRANCH_TAG = "${registry}/${imageName}:${imageTag}"git branch: 'master', url: 'https://github.com/amitopenwriteup/cicd.git'- Clones the source code from the GitHub repository.
def dockerImage = docker.build("${registry}/${imageName}:${imageTag}", "-f ${dockerfile} .")
dockerImage.push()- Builds a Docker image from the
dockerfile. - Tags it as
localhost:5000/myimage:latest. - Pushes the image to the registry.
withCredentials([file(credentialsId: '9445dd9b-e604-40cd-a802-79f747011ecb', variable: 'KUBECONFIG')]) {
sh """
sed \
-e "s|{{NAMESPACE}}|${namespace}|g" \
-e "s|{{PULL_IMAGE}}|${IMAGE_BRANCH_TAG}|g" \
${manifestFile} \
| kubectl apply -f -
"""
}-
Uses a stored Kubeconfig credential in Jenkins to connect to the cluster.
-
Replaces placeholders in the Kubernetes manifest:
{{NAMESPACE}}→default{{PULL_IMAGE}}→ built Docker image
-
Deploys the updated manifest with
kubectl apply.
├── Dockerfile # Docker build instructions
├── kubernetes-manifest.yaml # K8s deployment template with placeholders
├── Jenkinsfile # Jenkins pipeline
└── requirements.txt # Python dependencies (if applicable)
-
Run Jenkins job
- The pipeline will automatically build, push, and deploy.
-
Verify deployment
kubectl get pods -n default kubectl get svc -n default
-
Access the application using the exposed service (LoadBalancer/NodePort/Ingress depending on manifest).
- Jenkins with Docker & Kubernetes plugins installed
- Docker registry (local or remote)
- Kubernetes cluster accessible from Jenkins
- Properly configured KUBECONFIG in Jenkins credentials