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
18 changes: 6 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
#----------------------------------

# Import docker image with maven installed
FROM maven:3.8.3-openjdk-17 as builder

# Add maintainer, so that new user will understand who had written this Dockerfile
MAINTAINER Madhup Pandey<madhuppandey2908@gmail.com>

# Add labels to the image to filter out if we have multiple application running
LABEL app=bankapp
FROM maven:3.8.3-openjdk-17 AS builder

# Set working directory
WORKDIR /src
WORKDIR /app

# Copy source code from local to container
COPY . /src
COPY . /app
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

Implement .dockerignore for better build context

The current COPY command includes all files in the build context. This could include unnecessary files like logs, IDE configurations, or sensitive information.

Create a .dockerignore file with the following contents:

.git
.gitignore
target/
.settings/
.project
.classpath
.idea/
*.iml
*.log
README.md


# Build application and skip test cases
RUN mvn clean install -DskipTests=true
Expand All @@ -25,13 +19,13 @@ RUN mvn clean install -DskipTests=true
#--------------------------------------

# Import small size java image
FROM openjdk:17-alpine as deployer
FROM openjdk:17-alpine AS deployer
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

Enhance security with non-root user

The application currently runs as root, which is a security risk. Consider creating and using a non-root user.

FROM openjdk:17-alpine AS deployer

+RUN addgroup -S spring && adduser -S spring -G spring
+USER spring:spring

COPY --from=builder /app/target/*.jar /app/target/bankapp.jar

Also, consider using a more deterministic JAR file name in the builder stage:

-RUN mvn clean install -DskipTests=true
+RUN mvn clean install -DskipTests=true && \
+    mv target/*.jar target/bankapp.jar

Also applies to: 25-25


# Copy build from stage 1 (builder)
COPY --from=builder /src/target/*.jar /src/target/bankapp.jar
COPY --from=builder /app/target/*.jar /app/target/bankapp.jar

# Expose application port
EXPOSE 8080

# Start the application
ENTRYPOINT ["java", "-jar", "/src/target/bankapp.jar"]
ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]
80 changes: 65 additions & 15 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,30 +1,80 @@
@Library("shared-library@DevOps") _

pipeline {
agent {label 'runner_1'}

agent {
label 'agent-slave'
}
stages {
stage('Checkout code') {
stage("Code Clone") {
steps {
codeCheckout('DevOps', 'https://github.com/joakim077/Springboot-BankApp.git')
echo "Code Clone Stage"
git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
}
}
stage('build') {
stage("Code Build & Test") {
steps {
buildImage("springboot-application")
script {
try {
echo "Starting Code Clone Stage"
git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
Comment on lines +16 to +17
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

Remove redundant code cloning in 'Code Build & Test' stage

The repository is already cloned during the 'Code Clone' stage. Cloning it again in this stage is redundant and may cause confusion. Consider removing the git command from this stage.

Apply this change:

- echo "Starting Code Clone Stage"
- git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
+ echo "Starting Build and Test Stage"
📝 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
echo "Starting Code Clone Stage"
git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
echo "Starting Build and Test Stage"

echo "Starting Build and Test"
sh 'mvn clean package'
sh 'mvn test'
sh 'docker build -t bankapp:latest
} catch (Exception e) {
error "Failed to clone repository: ${e.message}"
}
Comment on lines +16 to +24
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

Fix errors in 'Code Build & Test' stage script

Several issues are present in this script block:

  • The echo message incorrectly states "Starting Code Clone Stage" within the 'Code Build & Test' stage.
  • The sh command for docker build is incomplete, missing closing quote and context.
  • The catch block error message only references cloning failure but should cover build and test failures.

Apply these changes:

             script {
                 try {
-                    echo "Starting Code Clone Stage"
+                    echo "Starting Build and Test Stage"
-                    git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
                     echo "Starting Build and Test"
                     sh 'mvn clean package'
                     sh 'mvn test'
-                    sh 'docker build -t bankapp:latest 
+                    sh 'docker build -t bankapp:latest .'
                 } catch (Exception e) {
-                    error "Failed to clone repository: ${e.message}"
+                    error "Build and Test failed: ${e.message}"
                 }
             }
📝 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
echo "Starting Code Clone Stage"
git url: "https://github.com/sushmithavs/Springboot-BankApp.git", branch: "DevOps"
echo "Starting Build and Test"
sh 'mvn clean package'
sh 'mvn test'
sh 'docker build -t bankapp:latest
} catch (Exception e) {
error "Failed to clone repository: ${e.message}"
}
echo "Starting Build and Test Stage"
echo "Starting Build and Test"
sh 'mvn clean package'
sh 'mvn test'
sh 'docker build -t bankapp:latest .'
} catch (Exception e) {
error "Build and Test failed: ${e.message}"
}

}
}
}
stage('Push Image') {
stage("Push To DockerHub") {
steps {
pushImage("springboot-application")
withCredentials([usernamePassword(
credentialsId: "dockerhub-creds",
usernameVariable: "dockerHubUser",
passwordVariable: "dockerHubPass"
)]) {
sh '''
echo $dockerHubPass | docker login - u $dockerHubUser --password
- stdin
VERSION = $(git rev - parse -- short HEAD) docker image tag bankapp: latest $ {
dockerHubUser
}/bankapp:${VERSION}
docker image tag bankapp:latest ${dockerHubUser}/bankapp: latest
docker push $ {
dockerHubUser
}/bankapp:${VERSION}
docker push ${dockerHubUser}/bankapp: latest
docker rmi $ {
dockerHubUser
}/bankapp:${VERSION}
docker rmi ${dockerHubUser}/bankapp: latest
'''
Comment on lines +35 to +50
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

Fix syntax errors in shell script for pushing Docker image

The shell script in the 'Push To DockerHub' stage contains syntax errors and incorrect variable usage:

  • Incorrect spacing in variable references, e.g., $ { dockerHubUser } should be ${dockerHubUser}.
  • Misplaced line breaks and indentation make the script invalid.
  • The docker login command is improperly formatted.
  • The VERSION variable assignment is incorrect.

Apply the following corrected shell script:

sh '''
    echo $dockerHubPass | docker login -u $dockerHubUser --password-stdin
    VERSION=$(git rev-parse --short HEAD)
    docker image tag bankapp:latest ${dockerHubUser}/bankapp:${VERSION}
    docker image tag bankapp:latest ${dockerHubUser}/bankapp:latest
    docker push ${dockerHubUser}/bankapp:${VERSION}
    docker push ${dockerHubUser}/bankapp:latest
    docker rmi ${dockerHubUser}/bankapp:${VERSION}
    docker rmi ${dockerHubUser}/bankapp:latest
'''

}
} catch (Exception e) {
error "Failed to push Docker image: ${e.message}"
}
Comment on lines +52 to +54
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

Correct the placement and syntax of the error handling block

The catch block is misaligned and outside the appropriate scope. Exception handling should be enclosed within the script block, matching the corresponding try block.

Apply these changes:

- } catch (Exception e) {
-     error "Failed to push Docker image: ${e.message}"
- }
+                } catch (Exception e) {
+                    error "Failed to push Docker image: ${e.message}"
+                }

Ensure that the catch block is properly indented and placed within the script block.

📝 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
} catch (Exception e) {
error "Failed to push Docker image: ${e.message}"
}
} catch (Exception e) {
error "Failed to push Docker image: ${e.message}"
}

}
}
stage('Deploy'){
steps{
deploy()
stage("Deploy") {
steps {
script {
sh "docker compose down && docker compose up -d --build"
// Wait for application to be ready
sh '''
max_attempts = 30
attempt = 1
echo "Waiting for application to be ready..."
while [$attempt - le $max_attempts]; do if curl - s http: //localhost:8080/health; then
echo "Application is ready!"
exit 0
fi
attempt = $((attempt+1))
sleep 10
done
echo "Application failed to start within timeout"
exit 1
'''
Comment on lines +62 to +75
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

Fix syntax errors in deployment health check shell script

The shell script intended to check the application's readiness has multiple syntax issues:

  • Variable assignments should not have spaces around =.
  • Loop and conditional syntax are incorrect.
  • The curl command is improperly formatted.
  • Comparison operators and syntax need correction.

Apply the corrected shell script:

sh '''
    max_attempts=30
    attempt=1
    echo "Waiting for application to be ready..."
    while [ $attempt -le $max_attempts ]; do
        if curl -s http://localhost:8080/health; then
            echo "Application is ready!"
            exit 0
        fi
        attempt=$((attempt+1))
        sleep 10
    done
    echo "Application failed to start within timeout"
    exit 1
'''

This corrects variable assignments, loop constructs, and command syntax.

}
}
}

}
}

Loading