Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
pipeline {
agent { label 'spring-server' }

Comment on lines +1 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add timeout and resource constraints to the pipeline.

Consider adding pipeline-level timeout and resource constraints to prevent hung builds and resource exhaustion:

 pipeline {
     agent { label 'spring-server' }
+    options {
+        timeout(time: 1, unit: 'HOURS')
+    }
+    resources {
+        memory '2048Mi'
+        cpu '500m'
+    }

Committable suggestion skipped: line range outside the PR's diff.

stages {
stage("Code Clone") {
steps {
echo "Code Clone Stage"
git url: "https://github.com/shailesh271997/Springboot-BankApp.git", branch: "nginx-setup"
}
}
Comment on lines +5 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve Git checkout configuration.

Several improvements are recommended for the Code Clone stage:

  1. Use checkout scm instead of explicit git step for Pipeline projects
  2. Clean workspace before checkout
  3. Use parameters for branch selection instead of hard-coding
-        stage("Code Clone") {
+        stage("Checkout") {
             steps {
-                echo "Code Clone Stage"
-                git url: "https://github.com/shailesh271997/Springboot-BankApp.git", branch: "nginx-setup"
+                cleanWs()
+                checkout scm
             }
         }

If you need to keep the explicit git step, consider using parameters:

parameters {
    string(name: 'BRANCH_NAME', defaultValue: 'nginx-setup', description: 'Git branch to build')
}

stage("Code Build & Test") {
steps {
echo "Code Build Stage"
sh "docker build -t springboot-bankapp ."
}
}
Comment on lines +11 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Enhance build stage with tests and proper versioning.

The current build stage has several missing components:

  1. No actual tests are being run despite the stage name
  2. No version tagging strategy
  3. No build caching optimization
         stage("Code Build & Test") {
             steps {
-                echo "Code Build Stage"
-                sh "docker build -t springboot-bankapp ."
+                script {
+                    def version = env.BUILD_NUMBER
+                    // Run tests
+                    sh "./mvnw clean test"
+                    // Build with proper tags and cache
+                    sh """
+                        docker build \
+                            --cache-from springboot-bankapp:latest \
+                            -t springboot-bankapp:${version} \
+                            -t springboot-bankapp:latest \
+                            --build-arg VERSION=${version} \
+                            .
+                    """
+                }
             }
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
stage("Code Build & Test") {
steps {
echo "Code Build Stage"
sh "docker build -t springboot-bankapp ."
}
}
stage("Code Build & Test") {
steps {
script {
def version = env.BUILD_NUMBER
// Run tests
sh "./mvnw clean test"
// Build with proper tags and cache
sh """
docker build \
--cache-from springboot-bankapp:latest \
-t springboot-bankapp:${version} \
-t springboot-bankapp:latest \
--build-arg VERSION=${version} \
.
"""
}
}
}

stage("Push To DockerHub") {
steps {
withCredentials([usernamePassword(
credentialsId: "dockerHubCreds",
usernameVariable: "dockerHubUser",
passwordVariable: "dockerHubPass")]) {
sh 'echo $dockerHubPass | docker login -u $dockerHubUser --password-stdin'
sh "docker image tag springboot-bankapp ${env.dockerHubUser}/springboot-bankapp:latest"
sh "docker push ${env.dockerHubUser}/springboot-bankapp:latest"
}
}
}
Comment on lines +17 to +28
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add security scanning and error handling to Docker push stage.

The current stage needs security and reliability improvements:

  1. Add vulnerability scanning before pushing
  2. Add error handling
  3. Clean up local images after push
  4. Use specific registry URL instead of default Docker Hub
         stage("Push To DockerHub") {
             steps {
-                withCredentials([usernamePassword(
-                    credentialsId: "dockerHubCreds", 
-                    usernameVariable: "dockerHubUser", 
-                    passwordVariable: "dockerHubPass")]) {
-                    sh 'echo $dockerHubPass | docker login -u $dockerHubUser --password-stdin'
-                    sh "docker image tag springboot-bankapp ${env.dockerHubUser}/springboot-bankapp:latest"
-                    sh "docker push ${env.dockerHubUser}/springboot-bankapp:latest"
+                script {
+                    def version = env.BUILD_NUMBER
+                    try {
+                        // Scan for vulnerabilities
+                        sh "trivy image springboot-bankapp:${version}"
+                        
+                        withCredentials([usernamePassword(
+                            credentialsId: "dockerHubCreds", 
+                            usernameVariable: "dockerHubUser", 
+                            passwordVariable: "dockerHubPass")]) {
+                            sh '''
+                                echo $dockerHubPass | docker login -u $dockerHubUser --password-stdin docker.io
+                                docker image tag springboot-bankapp:${version} docker.io/${dockerHubUser}/springboot-bankapp:${version}
+                                docker image tag springboot-bankapp:${version} docker.io/${dockerHubUser}/springboot-bankapp:latest
+                                docker push docker.io/${dockerHubUser}/springboot-bankapp:${version}
+                                docker push docker.io/${dockerHubUser}/springboot-bankapp:latest
+                            '''
+                        }
+                    } catch (Exception e) {
+                        error "Failed to push image: ${e.message}"
+                    } finally {
+                        sh 'docker logout'
+                        // Clean up local images
+                        sh "docker rmi springboot-bankapp:${version} || true"
+                    }
                 }
             }
         }

Committable suggestion skipped: line range outside the PR's diff.

stage("Deploy") {
steps {
sh "docker compose down && docker compose up -d --build"
}
}
Comment on lines +29 to +33
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Improve deployment reliability and add health checks.

The current deployment strategy has several concerns:

  1. No health checks after deployment
  2. No rollback strategy
  3. Potential downtime during deployment
  4. No verification of docker-compose.yml
         stage("Deploy") {
             steps {
-                sh "docker compose down && docker compose up -d --build"
+                script {
+                    try {
+                        // Verify docker-compose.yml exists
+                        sh '[ -f docker-compose.yml ] || (echo "docker-compose.yml not found" && exit 1)'
+                        
+                        // Rolling update to avoid downtime
+                        sh '''
+                            docker compose pull
+                            docker compose up -d --no-deps --build app
+                            
+                            # Wait for health check
+                            timeout 60s bash -c 'until curl -s http://localhost:8080/actuator/health | grep -q "UP"; do sleep 5; done'
+                        '''
+                    } catch (Exception e) {
+                        echo "Deployment failed, rolling back..."
+                        sh 'docker compose rollback app'
+                        error "Failed to deploy: ${e.message}"
+                    }
+                }
             }
         }

Committable suggestion skipped: line range outside the PR's diff.

}
}

Binary file added jenkins-steps-images/step-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jenkins-steps-images/step-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jenkins-steps-images/step-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jenkins-steps-images/step-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jenkins-steps-images/step-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added jenkins-steps-images/step-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.