diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..b4b16f646 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM openjdk:21-jdk + +LABEL project="petclinic" + +LABEL author="bhanu" + +EXPOSE 8080 +ARG artifact=target/*.ja + +COPY ${artifact} /spring-petclinic.jar + +ENTRYPOINT ["java", "-jar", "/spring-petclinic.jar"] \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..0c9ca575a --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,63 @@ +pipeline { + agent { label 'hello' } + stages { + stage('Checkout') { + steps { + sh 'echo passed' + //git branch: 'main', url: 'https://github.com/iam-veeramalla/Jenkins-Zero-To-Hero.git' + } + } + stage('Build and Test') { + steps { + sh 'ls -ltr' + // build the project and create a JAR file + sh 'mvn clean package' + } + } + stage('Static Code Analysis') { + environment { + SONAR_URL = "http://20.197.43.119:32768" + } + steps { + withCredentials([string(credentialsId: 'sonarqube', variable: 'SONAR_AUTH_TOKEN')]) { + sh 'mvn sonar:sonar -Dsonar.login=$SONAR_AUTH_TOKEN -Dsonar.host.url=${SONAR_URL}' + } + } + } + stage('Build and Push Docker Image') { + environment { + DOCKER_IMAGE = "bhanu3333/kube:${BUILD_NUMBER}" + // DOCKERFILE_LOCATION = "java-maven-sonar-argocd-helm-k8s/spring-boot-app/Dockerfile" + REGISTRY_CREDENTIALS = credentials('docker-cred') + } + steps { + script { + sh 'docker build -t ${DOCKER_IMAGE} .' + def dockerImage = docker.image("${DOCKER_IMAGE}") + docker.withRegistry('https://index.docker.io/v1/', "docker-cred") { + dockerImage.push() + } + } + } + } + stage('Update Deployment File') { + environment { + GIT_REPO_NAME = "Jenkins-Zero-To-Hero" + GIT_USER_NAME = "vuyyuru-bhanu" + } + steps { + withCredentials([string(credentialsId: 'github', variable: 'GITHUB_TOKEN')]) { + sh ''' + git config user.email "prasad.bhanu59@gmial.com" + git config user.name "bhanu" + BUILD_NUMBER=${BUILD_NUMBER} + sed -i "s/replaceImageTag/${BUILD_NUMBER}/g" deployment.yml + git add deployment.yml + git commit -m "Update deployment image to version ${BUILD_NUMBER}" + git push https://${GITHUB_TOKEN}@github.com/${GIT_USER_NAME}/${GIT_REPO_NAME} HEAD:test + ''' + } + } + } + } +} \ No newline at end of file diff --git a/deployment.yml b/deployment.yml new file mode 100644 index 000000000..937f0f0a0 --- /dev/null +++ b/deployment.yml @@ -0,0 +1,21 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: spring-boot-app + labels: + app: spring-boot-app +spec: + replicas: 2 + selector: + matchLabels: + app: spring-boot-app + template: + metadata: + labels: + app: spring-boot-app + spec: + containers: + - name: spring-boot-app + image: bhanu3333/kube:replaceImageTag + ports: + - containerPort: 8080 \ No newline at end of file diff --git a/service.yml b/service.yml new file mode 100644 index 000000000..bde948724 --- /dev/null +++ b/service.yml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: spring-boot-app-service +spec: + type: NodePort + ports: + - name: http + port: 80 + targetPort: 8080 + protocol: TCP + selector: + app: spring-boot-app \ No newline at end of file