diff --git a/Dockerfile b/Dockerfile index 21d689c8..fa8a0e6e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,10 +11,17 @@ VOLUME /tmp EXPOSE 8080 # The application's jar file -ARG JAR_FILE +#ARG JAR_FILE # Add the application's jar to the container -ADD ${JAR_FILE} app.jar +#ADD ${JAR_FILE} app.jar +COPY target/*.jar app.jar + +#Adding HealthCheckup +HEALTHCHECK CMD curl --fail http://localhost:8080/ || exit 1 # Run the jar file -ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] \ No newline at end of file +ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] + + + diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 00000000..bc1bd3b6 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,45 @@ +node{ + def Namespace = "default" + def ImageName = "sudiptod/cicd" + def Creds = "DockerHub" + def GITHUB_URL = "https://github.com/sudipto92/cicd-applied-to-spring-boot-java-app.git" + def GITHUB_Creds = "GITHUB_CREDENTIALS" + try{ + + stage('Checkout'){ + git credentialsId: "${GITHUB_Creds}", url: "${GITHUB_URL}" + sh "git rev-parse --short HEAD > .git/commit-id" + imageTag= readFile('.git/commit-id').trim() + } + stage('Compile, Test and Coverage'){ + sh "/usr/bin/mvn -B clean deploy" + publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: "/var/lib/jenkins/workspace/${JOB_NAME}/target/site/jacoco/", reportFiles: 'index.html', reportName: 'Jacoco HTML Report', reportTitles: 'Jacoco']) + } + + stage('SonarQube Analysis'){ + withSonarQubeEnv('sonar') { + sh "/opt/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=${JOB_NAME} -Dsonar.projectName=${JOB_NAME} -Dsonar.projectVersion=${BUILD_NUMBER} -Dsonar.sources=. -Dsonar.java.binaries=target/ -Dsonar.issuesReport.html.enable=true -Dsonar.issuesReport.html.location=. -Dsonar.issuesReport.html.name=sample -Dsonar.login=admin -Dsonar.password=admin123" + } + } + + + stage('Docker Build, Push'){ + withDockerRegistry([credentialsId: "${Creds}", url:'https://index.docker.io/v1/']) { + sh "docker build -t ${ImageName}:${imageTag} ." + sh "docker tag ${ImageName}:${imageTag} ${ImageName}:latest" + sh "docker push ${ImageName}:latest" + sh "docker push ${ImageName}:${imageTag}" + } + } + + stage('Deploy'){ + sh " curl -X POST https://admin:admin@172.31.75.85/api/v2/job_templates/23/launch/ --insecure" + } + } + + catch (err) { + currentBuild.result = 'FAILURE' + } +} + + diff --git a/ansible/host b/ansible/host new file mode 100644 index 00000000..7c64e642 --- /dev/null +++ b/ansible/host @@ -0,0 +1,4 @@ +[cloud] +#localhost ansible_connection=local +172.31.41.185 ansible_connection=ssh ansible_user=ubuntu ansible_ssh_private_key_file=/var/lib/jenkins/key.pem +#other2.example.com ansible_connection=ssh ansible_user=myotheruser diff --git a/ansible/roles/deploy/tasks/main.yml b/ansible/roles/deploy/tasks/main.yml new file mode 100644 index 00000000..bbd3f3b8 --- /dev/null +++ b/ansible/roles/deploy/tasks/main.yml @@ -0,0 +1,10 @@ +- name: Update apt-get repo and cache + apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 + +- name: This command will change the working directory + shell: + cmd: docker-compose up -d + chdir: /var/lib/jenkins/workspace/pipeline-maven-ansible-1 + + + diff --git a/ansible/site.yml b/ansible/site.yml new file mode 100644 index 00000000..6038376a --- /dev/null +++ b/ansible/site.yml @@ -0,0 +1,6 @@ +--- +- hosts: cloud + become: yes + roles: + - deploy + diff --git a/ansiblek8/host b/ansiblek8/host new file mode 100644 index 00000000..6e9be8c9 --- /dev/null +++ b/ansiblek8/host @@ -0,0 +1,4 @@ +[cloud] +#localhost ansible_connection=local +172.31.44.14 ansible_connection=ssh ansible_user=k8s-admin ansible_password=qwerty +#other2.example.com ansible_connection=ssh ansible_user=myotheruser diff --git a/ansiblek8/roles/deploy/files/deploy.sh b/ansiblek8/roles/deploy/files/deploy.sh new file mode 100644 index 00000000..61c82cfc --- /dev/null +++ b/ansiblek8/roles/deploy/files/deploy.sh @@ -0,0 +1,22 @@ +#!/bin/bash +#sudo scp /etc/kubernetes/admin.conf $HOME/ +#sudo chown $(id -u):$(id -g) $HOME/admin.conf +#export KUBECONFIG=$HOME/admin.conf + +whoami >/tmp/a.txt + + +kubectl get services | grep knote +RESULT=$? + +if [ $RESULT -eq 0 ]; then + kubectl delete -f /tmp/deployment/deploy.yml + kubectl apply -f /tmp/deployment/deploy.yml + else + kubectl delete -f /tmp/deployment + kubectl apply -f /tmp/deployment/ +fi + + + + diff --git a/ansiblek8/roles/deploy/files/deployment/deploy.yml b/ansiblek8/roles/deploy/files/deployment/deploy.yml new file mode 100644 index 00000000..e6924620 --- /dev/null +++ b/ansiblek8/roles/deploy/files/deployment/deploy.yml @@ -0,0 +1,20 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: knote +spec: + replicas: 1 + selector: + matchLabels: + app: knote + template: + metadata: + labels: + app: knote + spec: + containers: + - name: app + image: sudiptod/cicd + ports: + - containerPort: 8080 + imagePullPolicy: Always diff --git a/ansiblek8/roles/deploy/files/deployment/service.yml b/ansiblek8/roles/deploy/files/deployment/service.yml new file mode 100644 index 00000000..9b9aa96c --- /dev/null +++ b/ansiblek8/roles/deploy/files/deployment/service.yml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: knote +spec: + selector: + app: knote + ports: + - port: 80 + targetPort: 8080 + nodePort: 30000 + type: NodePort diff --git a/ansiblek8/roles/deploy/files/run_deploy.sh b/ansiblek8/roles/deploy/files/run_deploy.sh new file mode 100644 index 00000000..6bdccc3c --- /dev/null +++ b/ansiblek8/roles/deploy/files/run_deploy.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +su admin "/tmp/deploy.sh" diff --git a/ansiblek8/roles/deploy/tasks/main.yml b/ansiblek8/roles/deploy/tasks/main.yml new file mode 100644 index 00000000..ecc5650c --- /dev/null +++ b/ansiblek8/roles/deploy/tasks/main.yml @@ -0,0 +1,11 @@ +- name: Transfer executable script script + copy: src=deploy.sh dest=/tmp mode=0777 + +- name: Transfer executable script script + copy: src=run_deploy.sh dest=/tmp mode=0777 + +- name: Transfer deployment script + copy: src=deployment dest=/tmp + +- name: Execute the script + command: bash /tmp/run_deploy.sh diff --git a/ansiblek8/site.yml b/ansiblek8/site.yml new file mode 100644 index 00000000..ad9a111a --- /dev/null +++ b/ansiblek8/site.yml @@ -0,0 +1,5 @@ +--- +- hosts: all + become: yes + roles: + - deploy diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..6ac18000 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3" +services: + spring-boot-app: + image: sudiptod/cicd:latest + ports: + - 80:8080 diff --git a/pom.xml b/pom.xml index 21bb6ba9..2a2fecc0 100644 --- a/pom.xml +++ b/pom.xml @@ -64,7 +64,7 @@ - + + maven-dependency-plugin useful for creating docker image --> diff --git a/src/main/java/com/cicd/cicdappliedtospringbootjavaapp/controller/HelloController.java b/src/main/java/com/cicd/cicdappliedtospringbootjavaapp/controller/HelloController.java index 5a67838f..afd3e317 100644 --- a/src/main/java/com/cicd/cicdappliedtospringbootjavaapp/controller/HelloController.java +++ b/src/main/java/com/cicd/cicdappliedtospringbootjavaapp/controller/HelloController.java @@ -5,10 +5,13 @@ @RestController public class HelloController { - - @GetMapping("/") - public String home() { - return "Hello World from DZONE"; - } + @GetMapping("/") + public String home() { + return "Welcome To Nucleus"; + } + @GetMapping("/sudo") + public String home1() { + return "Spring Boot Application Testing "; + } } diff --git a/src/test/java/com/cicd/cicdappliedtospringbootjavaapp/CicdAppliedToSpringBootJavaAppApplicationTests.java b/src/test/java/com/cicd/cicdappliedtospringbootjavaapp/CicdAppliedToSpringBootJavaAppApplicationTests.java index 31205b9f..505c9599 100644 --- a/src/test/java/com/cicd/cicdappliedtospringbootjavaapp/CicdAppliedToSpringBootJavaAppApplicationTests.java +++ b/src/test/java/com/cicd/cicdappliedtospringbootjavaapp/CicdAppliedToSpringBootJavaAppApplicationTests.java @@ -21,7 +21,7 @@ public class CicdAppliedToSpringBootJavaAppApplicationTests { @Test public void contextLoads() { - Assert.assertEquals("Hello World from DZONE",helloController.home() ); + Assert.assertEquals("Welcome To Nucleus",helloController.home() ); }