diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b02de25e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +k8smanifest/* +.gitignore +Jenkinsfile +README.md \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..4d79d421 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +#build war file +FROM maven:3.8-openjdk-8-slim as buildstage +RUN mkdir /app +WORKDIR /app +COPY src /app/src +COPY pom.xml /app +RUN mvn -f /app/pom.xml clean package + + + +#Deploying into tomcat +FROM tomcat:9.0 +RUN mv webapps webapps2 +RUN cp -r webapps.dist/ webapps + +COPY --from=buildstage /app/target/WebAppCal-1.3.2.war /usr/local/tomcat/webapps/webappcal.war \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index f49a41bf..226eb64c 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,23 +1,87 @@ pipeline { agent any + environment{ + VERSION = "${env.BUILD_ID}" + } stages { - stage('CODE-VALIDATION') { + stage('sonarQualityCheck') { + steps { + script { + withSonarQubeEnv('sonar-server') { + echo 'validatining code..' + sh 'mvn clean sonar:sonar' + + } + + timeout(time: 1, unit: 'HOURS'){ + def qg = waitForQualityGate() + if (qg.status != 'OK'){ + error "Pipeline aborted due to quality gate failure: ${qg.status}" + } + } + } + } + } + + stage('Build war file') { + steps { + sh 'mvn clean package' + } + } + + stage('Nexus Release') { + steps { + nexusArtifactUploader artifacts: [[artifactId: 'WebAppCal', classifier: '', file: 'target/WebAppCal-1.3.2.war', type: 'war']], credentialsId: 'nexus', groupId: 'com.web.cal', nexusUrl: '172.31.44.35:8081/', nexusVersion: 'nexus3', protocol: 'http', repository: 'maven-releases', version: '1.3.2' + + } + } + + stage('docker build and docker push') { steps { - echo 'validate code..' - sh 'mvn validate compile' + script{ + withCredentials([string(credentialsId: 'registry_pw', variable: 'registry_password')]) { + sh ''' + docker build -t 172.31.44.35:8082/webappcal:${VERSION} . + docker tag 172.31.44.35:8082/webappcal:${VERSION} 172.31.44.35:8082/webappcal:latest + docker login -u admin -p $registry_password 172.31.44.35:8082 + docker push 172.31.44.35:8082/webappcal:${VERSION} + docker rmi 172.31.44.35:8082/webappcal:${VERSION} + ''' + } + + } } } - stage('Unit Test') { + stage('Manual approval') { steps { - echo 'Testing..' - sh 'mvn test' + timeout(10) { + mail bcc: '', body: "Please click on the below link to review and approve deployment http://13.233.214.250:8080/job/pipeline_test/${env.BUILD_NUMBER}/console ", cc: '', from: '', replyTo: '', subject: 'Need approval to deploy', to: 'muqeeth.23@gmail.com' + input id: 'Deploygate', message: "Deploy ${env.JOB_NAME}?", ok: 'deploy' + } } } - stage('Deploy') { + + stage('Deploy with approval') { steps { - echo 'Deploying....' + script{ + withCredentials([kubeconfigFile(credentialsId: '7f2f53d9-632b-46e9-a7fc-ab7349cdf007', variable: 'KUBECONFIG')]) { + dir('k8smanifest') { + kubectl apply -f registrysecret.yml + kubectl apply -f deployment.yml + kubectl apply -f service.yml + } + + } + } + } } + } + post { + always { + mail bcc: '', body: "
Project: ${env.JOB_NAME}
Build Number: ${env.BUILD_NUMBER}
URL de build: ${env.BUILD_URL}", cc: '', charset: 'UTF-8', from: '', mimeType: 'text/html', replyTo: '', subject: "${currentBuild.result} CI: Project name -> ${env.JOB_NAME}", to: "muqeeth.23@gmail.com"; + } + } } diff --git a/README.md b/README.md new file mode 100644 index 00000000..e4b0a943 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Java Project +Build tool is Maven + +when we build the code using command $mvn package it will generate war file. that war can be placed in tomcat server to see application web page + +code is integrated with sonarqube plugin which help us in static code analysis + diff --git a/k8smanifest/deployment.yml b/k8smanifest/deployment.yml new file mode 100644 index 00000000..f0ed0522 --- /dev/null +++ b/k8smanifest/deployment.yml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: webappcal + name: webappcal +spec: + replicas: 2 + selector: + matchLabels: + app: webappcal + template: + metadata: + labels: + app: webappcal + spec: + imagePullSecrets: + - name: registry-secret + containers: + - image: 172.31.44.35:8082/webappcal + name: webappcal diff --git a/k8smanifest/registrysecret.yml b/k8smanifest/registrysecret.yml new file mode 100644 index 00000000..e955f0f0 --- /dev/null +++ b/k8smanifest/registrysecret.yml @@ -0,0 +1,7 @@ +apiVersion: v1 +data: + .dockerconfigjson: eyJhdXRocyI6eyJodHRwOi8vMTcyLjMxLjQ0LjM1OjgwODIiOnsidXNlcm5hbWUiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWRtaW4xMjMiLCJlbWFpbCI6ImFkbWluQHRlc3QuY29tIiwiYXV0aCI6IllXUnRhVzQ2WVdSdGFXNHhNak09In19fQ== +kind: Secret +metadata: + name: registry-secret +type: kubernetes.io/dockerconfigjson \ No newline at end of file diff --git a/k8smanifest/service.yml b/k8smanifest/service.yml new file mode 100644 index 00000000..462d12ec --- /dev/null +++ b/k8smanifest/service.yml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: webappcal + name: webappcal +spec: + ports: + - port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: webappcal + type: LoadBalancer \ No newline at end of file diff --git a/pom.xml b/pom.xml index c7c44528..a1927373 100644 --- a/pom.xml +++ b/pom.xml @@ -24,11 +24,13 @@ servlet-api 2.5 + + releases - http://52.53.153.183:8081/nexus/content/repositories/releases + diff --git a/src/main/java/mypackage/Calculator.java b/src/main/java/mypackage/Calculator.java index d49b7aca..0f10fad6 100644 --- a/src/main/java/mypackage/Calculator.java +++ b/src/main/java/mypackage/Calculator.java @@ -1,11 +1,13 @@ package mypackage; +import static java.lang.System.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Calculator extends HttpServlet { + public long addFucn(long first, long second){ return first+second; @@ -13,7 +15,7 @@ public long addFucn(long first, long second){ public long subFucn(long first, long second){ - return second-first; + return second/first; } public long mulFucn(long first, long second){ @@ -21,9 +23,12 @@ public long mulFucn(long first, long second){ return first*second; } - + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + try { response.setContentType("text/html"); @@ -50,7 +55,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro } catch(Exception e) { - - } + out.println("something went wrong"); + } } }