My First CI/CD Pipeline with JENKINS!
Tags: devops, jenkins, docker, kubernetes
Published: 2023-08-18 09:44:11

## 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

```
[](https://github.com/taqiyeddinedj/ci-cd_project)

  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"]
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdgre7455wu87byeuuvg.png)

**Check my docker hub repo**
[](https://hub.docker.com/repository/docker/taqiyeddinedj/my-repo)

 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zss2lzhwylqyu60wegzu.png)


  Inside the Jenkins container, I've included the Docker runtime, enabling it to build Docker images directly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ci64hyg2q4j8ykknskeo.png)



  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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e381lochqsyjhjzrdaa2.png)


  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.

[](https://stackoverflow.com/questions/71084850/jenkins-pipeline-to-deploy-on-kubernetes#:~:text=Download%20Kubernetes%20Continuous%20Plugin%201.0.0%20version%20from%20https%3A%2F%2Fupdates.jenkins.io%2Fdownload%2Fplugins%2Fkubernetes-cd%2F1.0.0%2Fkubernetes-cd.hpi,%22Deploy%22%20button%20as%20shown%20below%3A%20Then%20run%20manually%3A)

  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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/reobwtr84hrd6f0i9omy.png)


  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.