diff --git a/Deployment.yaml b/Deployment.yaml new file mode 100644 index 0000000..8d8bf09 --- /dev/null +++ b/Deployment.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: my-app + name: nodejs-app +spec: + replicas: 4 + selector: + matchLabels: + app: my-app + template: + metadata: + labels: + app: my-app + spec: + containers: + - name: node-app + image: public.ecr.aws/y0j0v8z3/test-ecr:feature + ports: + - containerPort: 80 + +--- + +apiVersion: v1 +kind: Service +metadata: + name: my-service +spec: + selector: + app: my-app + type: NodePort + ports: + - port: 3000 + targetPort: 3000 + nodePort: 32600 + protocol: TCP diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..956a2d0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM node:16 +WORKDIR /app +COPY . . +RUN npm install +EXPOSE 8080 +CMD ["npm","start"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..83fb537 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,55 @@ +pipeline { + + agent any + + stages { + + stage('Clone repository') { + steps { + script{ + checkout scm + } + } + } + stage('Build') { + steps { + sh 'pwd' + sh 'ls -la' + sh 'npm install' + sh 'npm run' + sh 'ls -la' + } + + } + + stage('Docker Image Build') { + steps { + sh 'docker build -t test-ecr .' + } + } + + stage('Push Docker Image to ECR') { + steps { + withAWS(credentials: 'aws-credentials', region: 'ap-south-1') { + sh 'aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/y0j0v8z3' + sh 'docker tag test-ecr:latest public.ecr.aws/y0j0v8z3/test-ecr:feature' + sh 'docker push public.ecr.aws/y0j0v8z3/test-ecr:feature' + } + } + } + + stage('Integrate Jenkins with EKS Cluster and Deploy App') { + steps { + withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'K8S', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') { + script { + sh 'ls -la' + sh "kubectl apply -f Deployment.yaml" + + } + + } + } + } + + } +} diff --git a/README.md b/README.md index 22e0844..1c31826 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ # SampleExpressApp Used for Devops interview + +STEP BY STEP CICD + + +Launch a virtual machine of any linux flavour and install jenkins, docker, AWS CLI and nodejs + +Create a registry in AWS ECR and create IAM user with required permission to carry out ECR activities. + +Create EKS cluster using AWS CLI along with creation of IAM role with cloudformation, IAM & EC2 permissions. + +Create AWS access key and secret keys from IAM user + +Access jenkins through port 8080 on browser and download AWS:Pipeline, ECR, docker pipeline, EKS, cloudbees plugins + +Store AWS credentials in jenkins which are required to login to ECR + +Create Dockerfile (As shown in repo) and Jenkinsfile with necessary steps and stages + +Create K8S manifest file for Deployment and services and choose type as Nodeport (With appropriate port of an application) + +Now, access this application with Node IP:NodePort on browser.