-
Notifications
You must be signed in to change notification settings - Fork 367
Bank application-Springboot Jenkins #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: DevOps
Are you sure you want to change the base?
Changes from all commits
6bc1c56
10948d7
013a0ae
b3090df
0d52682
303bf76
2ea6df8
a1dffef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
||
| # Build application and skip test cases | ||
| RUN mvn clean install -DskipTests=true | ||
|
|
@@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.jarAlso, 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.jarAlso 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"] | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 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
Suggested change
|
||||||||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix errors in 'Code Build & Test' stage script Several issues are present in this script block:
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
Suggested change
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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:
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct the placement and syntax of the error handling block The 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 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix syntax errors in deployment health check shell script The shell script intended to check the application's readiness has multiple syntax issues:
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. |
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
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
.dockerignorefile with the following contents: