My First CI/CD Pipeline with JENKINS!
taqiyeddinedj
Posted on August 18, 2023
Project Scope and Tools Used:
This project centers around a comprehensive CI/CD pipeline, showcasing the integration of popular tools that are widely utilized in the field.
GitHub Repository Overview:
When you take a look at my GitHub repository, you'll notice several key files that play a crucial role in this pipeline's functionality.
project-root/
├── templates/
│ └── index.html
├── Dockerfile
├── app.py
├── Jenkinsfile
├── deployment-service.yml
├── script.groovy
├── .gitignore
└── requirements.txt
The repository contains files such as 'app.py,' which is a Flask web application."
Docker Image Creation:
To build the application image, I leverage a Dockerfile.
FROM python:3.7-alpine
COPY . /app
WORKDIR /app
RUN pip install flask
CMD ["python", "app.py"]
In the deployment stage of our CI/CD pipeline, I've crafted a Kubernetes Deployment configuration file, deployment.yml
which defines the specifications for our application's deployment
The accompanying Service configuration file, service.yml
, defines how our application can be accessed
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
namespace: default
spec:
selector:
matchLabels:
app: myapp
replicas: 2
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: taqiyeddinedj/my-repo:webapp-2.0
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: myapp-service
namespace: default
spec:
type: NodePort
selector:
app: myapp
ports:
- port: 8080
targetPort: 8080
nodePort: 30000
Jenkins Setup and Building Triggers:
For continuous integration, I've set up Jenkins on a separate local machine using a Docker container.
Inside the Jenkins container, I've included the Docker runtime, enabling it to build Docker images directly.
Using a webhook, any push to the repository automatically triggers the build process."
Pipeline Stages**:
The pipeline consists of several distinct stages, each serving a specific purpose.
These stages include initialization, testing (which identifies the active branch), building and pushing to Docker Hub, and the final deployment to a Kubernetes cluster.
Jenkins File and Groovy Syntax:
The pipeline is orchestrated using a Jenkinsfile, written in Groovy syntax."
def buildDockerImage() {
echo "Building the docker image...."
withCredentials([usernamePassword(credentialsId:'dockr-hub-repo', passwordVariable: 'PASS', usernameVariable: 'USER')]){
sh "docker build -t taqiyeddinedj/my-repo:webapp-2.0 ."
sh " echo $PASS | docker login -u $USER --password-stdin"
sh "docker push taqiyeddinedj/my-repo:webapp-2.0"
}
}
def deploytok8s() {
echo "Deploying now the apllication on the kubernetes cluster"
kubernetesDeploy (configs: 'deployment-service.yml', kubeconfigId: 'kubernetes')
}
return this
Setting up Your Own Kubernetes Cluster:
Establishing my personal Kubernetes cluster quite challenging, but i made it work."
Troubleshooting was a significant aspect of getting the cluster operational.
Integration of Kubernetes with Jenkins:
Connecting Kubernetes with Jenkins was a critical step. I discovered a helpful plugin on Stack Overflow, conveniently provided by the Jenkins community.
An obstacle I encountered was a certificate signing issue, likely related to port forwarding. This led me to seek a cluster accessible from the public network.
Transition to Azure and Deploying a Cluster**:
To address these challenges, I migrated to Microsoft Azure and successfully deployed a Kubernetes cluster.
There's a specific command that needs to be included in the Jenkinsfile for this Azure-based cluster setup.
#!/usr/bin/env groovy
def gv
pipeline {
agent any
stages {
stage('Init') {
steps {
script {
gv = load "script.groovy"
}
}
}
stage('test') {
steps {
script {
echo "Testing the application"
echo "Executing pipeline for branch $BRANCH_NAME"
}
}
}
stage ('Build & pushing'){
steps {
script {
gv.buildDockerImage()
}
}
}
stage ('Deploy to K8S'){
steps {
script {
gv.deploytok8s()
}
}
}
}
}
To ensure seamless connectivity, the kubeconfig file is stored in a hidden directory (.kube) within your home directory, and its contents are uploaded to Jenkins as special credentials."
Azure Cluster Status:
Currently, the Azure-based Kubernetes cluster is up and running, serving as the backbone of our robust CI/CD pipeline.
Conclusion and Summary:
In summary, i have taken you through the intricate process of establishing a comprehensive CI/CD pipeline.
From the initial setup of Jenkins and Docker, to overcoming Kubernetes integration challenges, and finally transitioning to a reliable Azure-based cluster, we've covered a wealth of insights and practical steps.
Posted on August 18, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.