From 4fc29ff449f36da86e18a5ab42b44e034201393e Mon Sep 17 00:00:00 2001
From: "vinod.bangera" <vinod.bangera@niveussolutions.com>
Date: Tue, 14 May 2024 15:58:27 +0530
Subject: [PATCH] Initial commit by containerization

---
 Dockerfile         | 15 +-------
 Jenkinsfile        | 89 ++++++++++++++++++++++++++++++++++++++++++++++
 Procfile           |  1 +
 k8/deployment.yaml | 19 ++++++++++
 k8/service.yaml    | 12 +++++++
 5 files changed, 122 insertions(+), 14 deletions(-)
 create mode 100644 Jenkinsfile
 create mode 100644 Procfile
 create mode 100644 k8/deployment.yaml
 create mode 100644 k8/service.yaml

diff --git a/Dockerfile b/Dockerfile
index 7e5e42d..99923a5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,20 +1,7 @@
-# Use the official Python base image
 FROM python:3.9-slim
-
-# Set the working directory in the container
 WORKDIR /app
-
-# Copy the requirements file into the container at /app
 COPY requirements.txt /app/
-
-# Install dependencies
 RUN pip install --no-cache-dir -r requirements.txt
-
-# Copy the current directory contents into the container at /app
 COPY . /app/
-
-# Expose port 8000 to the outside world
 EXPOSE 8000
-
-# Command to run the FastAPI application
-CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
+CMD uvicorn main:app --host 0.0.0.0 --port 8000
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..70ff65f
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,89 @@
+pipeline {
+    agent any
+
+    // tools {
+    //     nodejs 'NodeJS'
+    // }
+
+    environment {
+                PROJECT_ID = "tech-rnd-project"
+                CLUSTER_NAME = "python-cluster"
+                LOCATION = "us-central1-a"
+                CREDENTIALS_ID = "335fefa0-0089-4fe2-994c-fd3bd9e2ab2b"
+    }
+
+    stages {
+        stage('Scm Checkout') {
+            steps {
+                    checkout scm
+            }
+        }
+        stage('Build') {
+            steps {
+                echo 'building the software'
+                // sh 'npm install --force'
+            }
+        }
+        stage('Test') {
+            steps {
+                 //add steps here
+                echo 'testing'
+            }
+        }
+
+
+       
+        stage('Build Docker Image') {
+            steps {
+                script {
+                    sh 'whoami'
+                    sh 'sudo chmod 777 /var/run/docker.sock'
+                    sh "docker build -t python-postgress ."
+                }
+            }
+        }
+
+        // stage('Image Scan') {
+        //     steps {
+        //         //add steps here
+        //         echo 'image scanning'
+        //         sh 'trivy image --no-progress --exit-code 1 --severity CRITICAL python-postgress'
+        //     }
+        // }
+
+        stage('Push Docker Image') {
+            steps {
+                script {
+                    echo 'Push Docker Image'
+                    sh "docker tag python-postgress asia-south1-docker.pkg.dev/tech-rnd-project/container-python/python"
+                    sh 'gcloud auth configure-docker asia-south1-docker.pkg.dev'
+                    sh 'docker push asia-south1-docker.pkg.dev/tech-rnd-project/container-python/python'
+                    // sh 'curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl'
+                    // sh 'chmod +x kubectl'
+                    // sh "sudo mv kubectl \$(which kubectl)"
+                }
+            }
+        }
+
+        stage('Deploy to K8s') {
+            steps {
+                echo 'Deployment started ...'
+                sh 'ls -ltr'
+                sh 'pwd'
+                echo 'Start deployment of deployment.yaml'
+                step([$class: 'KubernetesEngineBuilder', projectId: env.PROJECT_ID, clusterName: env.CLUSTER_NAME, location: env.LOCATION, manifestPattern: 'k8', credentialsId: env.CREDENTIALS_ID, verifyDeployments: true])
+                    echo 'Deployment Finished ...'
+                sh '''
+                '''
+            }
+        }
+    }
+    post {
+        always {
+            emailext to: 'vinod.bangera@niveussolutions.com',
+            subject: "jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}",
+            body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore Info can be found in the attached log",
+            attachLog: true
+        }
+    }
+}
diff --git a/Procfile b/Procfile
new file mode 100644
index 0000000..c2a9874
--- /dev/null
+++ b/Procfile
@@ -0,0 +1 @@
+web: uvicorn main:app --reload
diff --git a/k8/deployment.yaml b/k8/deployment.yaml
new file mode 100644
index 0000000..6b52a5f
--- /dev/null
+++ b/k8/deployment.yaml
@@ -0,0 +1,19 @@
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+  name: python-app
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: python-app
+  template:
+    metadata:
+      labels:
+        app: python-app
+    spec:
+      containers:
+        - name: python-app
+          image: asia-south1-docker.pkg.dev/tech-rnd-project/container-python/python
+          ports:
+            - containerPort: 8000
diff --git a/k8/service.yaml b/k8/service.yaml
new file mode 100644
index 0000000..cf50f9a
--- /dev/null
+++ b/k8/service.yaml
@@ -0,0 +1,12 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: node-app
+  name: python-app
+spec:
+  ports:
+    - port: 8000
+      targetPort: 8000
+  type: LoadBalancer
+  selector:
+    app: python-app
-- 
GitLab