From 0638fea013b44df84ba6c294e0b5ea9a08dd7bea Mon Sep 17 00:00:00 2001 From: "vinod.bangera" <vinod.bangera@niveussolutions.com> Date: Mon, 6 May 2024 14:11:07 +0530 Subject: [PATCH] Initial commit by containerization --- Dockerfile | 8 +++++ Jenkinsfile | 89 ++++++++++++++++++++++++++++++++++++++++++++++ Procfile | 1 + k8/deployment.yaml | 19 ++++++++++ k8/service.yaml | 12 +++++++ package.json | 2 ++ 6 files changed, 131 insertions(+) create mode 100644 Dockerfile 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 new file mode 100644 index 0000000..6b5092e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,8 @@ +FROM node:14-alpine +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +RUN npm run build +EXPOSE 3000 +CMD npm start diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..88674cc --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,89 @@ +pipeline { + agent any + + tools { + nodejs 'NodeJS' + } + + environment { + PROJECT_ID = "tech-rnd-project" + CLUSTER_NAME = "react-cluster" + LOCATION = "us-central1-b" + 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 react-app ." + } + } + } + + // stage('Image Scan') { + // steps { + // //add steps here + // echo 'image scanning' + // sh 'trivy image --no-progress --exit-code 1 --severity CRITICAL react-app' + // } + // } + + stage('Push Docker Image') { + steps { + script { + echo 'Push Docker Image' + sh "docker tag react-app asia-south1-docker.pkg.dev/tech-rnd-project/container-react/reactjs" + sh 'gcloud auth configure-docker asia-south1-docker.pkg.dev' + sh 'docker push asia-south1-docker.pkg.dev/tech-rnd-project/container-react/reactjs' + // 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..063b78f --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: npm start diff --git a/k8/deployment.yaml b/k8/deployment.yaml new file mode 100644 index 0000000..0b4cf30 --- /dev/null +++ b/k8/deployment.yaml @@ -0,0 +1,19 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: react-app +spec: + replicas: 1 + selector: + matchLabels: + app: react-app + template: + metadata: + labels: + app: react-app + spec: + containers: + - name: react-app + image: asia-south1-docker.pkg.dev/tech-rnd-project/container-react/reactjs + ports: + - containerPort: 3000 diff --git a/k8/service.yaml b/k8/service.yaml new file mode 100644 index 0000000..89c0726 --- /dev/null +++ b/k8/service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: node-app + name: react-app +spec: + ports: + - port: 3000 + targetPort: 3000 + type: LoadBalancer + selector: + app: react-app diff --git a/package.json b/package.json index 9a06582..d511a31 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { + "sonarqube-scanner": "^3.0.1", "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", @@ -13,6 +14,7 @@ "web-vitals": "^2.1.4" }, "scripts": { + "sonar": "node sonar-project.js", "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", -- GitLab