Skip to content

Springboot-BankApp#10

Open
nkantamani2023 wants to merge 4 commits intoLondheShubham153:DevOpsfrom
nkantamani2023:DevOps
Open

Springboot-BankApp#10
nkantamani2023 wants to merge 4 commits intoLondheShubham153:DevOpsfrom
nkantamani2023:DevOps

Conversation

@nkantamani2023
Copy link

@nkantamani2023 nkantamani2023 commented Nov 24, 2024

Jrnkinsfile and README.md updated

Summary by CodeRabbit

  • New Features

    • Introduced a .dockerignore file to optimize Docker image creation by excluding unnecessary files.
    • Added a new 'Deploy' stage in the Jenkins pipeline for zero-downtime deployment with health checks and rollback mechanisms.
    • Enhanced the README.md with clearer deployment instructions and security practices for sensitive information.
  • Bug Fixes

    • Improved clarity in the Jenkins pipeline stage names for better understanding.
  • Documentation

    • Restructured README.md to provide clearer guidance on deployment options and prerequisites.

@coderabbitai
Copy link

coderabbitai bot commented Nov 24, 2024

Walkthrough

The changes in this pull request introduce a new .dockerignore file to exclude unnecessary files from the Docker build context, optimizing the image creation process. The Dockerfile has been modified to update the working directory from /src to /app, affecting the paths for source code and artifacts. The Jenkinsfile has undergone significant updates, including changes to the pipeline agent, stage names, and the introduction of a new deployment stage with health checks and rollback mechanisms. The README.md has been restructured for clarity, emphasizing deployment options and security practices.

Changes

File Change Summary
.dockerignore New file created with entries to exclude target/, .git/, .idea/, *.iml, *.log, .env, and .DS_Store.
Dockerfile Updated working directory from /src to /app; adjusted COPY and ENTRYPOINT paths accordingly; maintainer label removed.
Jenkinsfile Changed agent label, added VERSION variable, updated stage names, replaced checkout method, added new 'Deploy' stage with health checks and rollback.
README.md Restructured with new title and description; clarified pre-requisites and deployment options; added security practices for Docker commands.

Possibly related PRs

  • Created CICD pipeline. #2: The changes in the .env file related to environment variable declarations are relevant as they may interact with the new .dockerignore file by specifying which Docker images to use, thus impacting the Docker build context.

Suggested reviewers

  • LondheShubham153

Poem

🐰 In the garden where code does bloom,
A .dockerignore sweeps out the gloom.
With paths now clear, the builds take flight,
Jenkins and Docker, all set just right.
README shines bright, with guidance anew,
Hop along, dear coder, there's much more to do! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

🧹 Outside diff range and nitpick comments (5)
Dockerfile (2)

6-6: Consider using a more specific base image tag

Using maven:3.8.3-openjdk-17 without a specific digest or SHA can lead to inconsistent builds over time.

-FROM maven:3.8.3-openjdk-17 AS builder 
+FROM maven:3.8.3-openjdk-17@sha256:abc123... AS builder 

32-32: Consider adding JVM tuning parameters

The current ENTRYPOINT doesn't include any JVM tuning parameters, which might affect performance in production.

-ENTRYPOINT ["java", "-jar", "/app/target/bankapp.jar"]
+ENTRYPOINT ["java", \
+  "-XX:+UseContainerSupport", \
+  "-XX:MaxRAMPercentage=75.0", \
+  "-jar", "/app/target/bankapp.jar"]
README.md (2)

105-150: Enhance Jenkins pipeline with better error handling and artifacts

The pipeline configuration can be improved with better practices:

  1. Add timeout for build stage
  2. Archive build artifacts
  3. Add cleanup steps
  4. Improve error handling

Enhance the pipeline with these improvements:

 pipeline{
     agent { label 'agent-slave' }
+    options {
+        timeout(time: 1, unit: 'HOURS')
+        buildDiscarder(logRotator(numToKeepStr: '5'))
+    }
     
     stages{
         stage("Code Clone"){
             steps{
                 echo "Code Clone Stage"
-                git url: "https://github.com/nkantamani2023/Springboot-BankApp.git", branch: "DevOps"
+                checkout scm
             }
         }
         stage("Code Build & Test"){
+            options {
+                timeout(time: 20, unit: 'MINUTES')
+            }
             steps{
-                echo "Code Build Stage"
-                sh "docker system prune -f"
-                sh "docker build -t bankapp ."
+                script {
+                    try {
+                        sh "docker system prune -f"
+                        sh "docker build -t bankapp ."
+                    } catch (Exception e) {
+                        currentBuild.result = 'FAILURE'
+                        error "Build failed: ${e.message}"
+                    }
+                }
             }
         }

153-157: Improve clarity of final steps

The final steps need better clarity and proper grammar:

  1. Missing "the" article in step 4
  2. Step 5 lacks details about expected outcome

Update the steps as follows:

-Step 4: Navigate to bank app dashboard and click on `Build Now` button.
+Step 4: Navigate to the Bank App dashboard and click on the `Build Now` button.

-Step 5: Your Pipeline has been created
+Step 5: Your pipeline will be created and executed. Monitor the pipeline stages in the Jenkins console for progress and any potential errors.
🧰 Tools
🪛 LanguageTool

[uncategorized] ~153-~153: You might be missing the article “the” here.
Context: ... } } } ``` Step 4: Navigate to bank app dashboard and click on Build Now ...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)


[uncategorized] ~153-~153: You might be missing the article “the” here.
Context: ...gate to bank app dashboard and click on Build Now button. Step 5: Your Pipelin...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)

Jenkinsfile (1)

73-76: Avoid redundant rollback in the 'post' failure block

The rollback logic in the post { failure { ... } } section may be redundant because the 'Deploy' stage already includes rollback steps if the service fails to become healthy. This duplication can lead to unnecessary commands being executed or potential conflicts.

Consider modifying or removing the post failure block:

  • If the rollback is fully handled within the 'Deploy' stage, you may remove the post { failure { ... } } section.
  • Alternatively, adjust the post block to handle only unexpected failures that are not caught in the 'Deploy' stage.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d51f97c and cb13732.

⛔ Files ignored due to path filters (2)
  • images/jenkins-pipeline-op.png is excluded by !**/*.png
  • images/jenkins-pipeline.png is excluded by !**/*.png
📒 Files selected for processing (4)
  • .dockerignore (1 hunks)
  • Dockerfile (2 hunks)
  • Jenkinsfile (1 hunks)
  • README.md (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .dockerignore
🧰 Additional context used
🪛 LanguageTool
README.md

[uncategorized] ~153-~153: You might be missing the article “the” here.
Context: ... } } } ``` Step 4: Navigate to bank app dashboard and click on Build Now ...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)


[uncategorized] ~153-~153: You might be missing the article “the” here.
Context: ...gate to bank app dashboard and click on Build Now button. Step 5: Your Pipelin...

(AI_EN_LECTOR_MISSING_DETERMINER_THE)

🔇 Additional comments (3)
Dockerfile (2)

16-16: Verify if skipping tests is intentional

Skipping tests during the build process (-DskipTests=true) could lead to quality issues. Consider running tests as part of the build unless there's a specific reason to skip them.

✅ Verification successful

Tests are already executed in the CI pipeline

The tests are being run in the Jenkinsfile with ./mvnw clean test, so skipping tests in the Dockerfile is acceptable as it's likely an optimization for container builds while maintaining test coverage in the CI pipeline.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if tests exist in the project
fd -e java -p "Test\.java$" || echo "No test files found"

# Check if tests are run elsewhere in the pipeline
rg "mvn.*test" -g "Jenkinsfile"

Length of output: 112


Line range hint 1-32: Verify alignment with Jenkins pipeline changes

The Dockerfile changes (especially the working directory change from /src to /app) need to be reflected in the Jenkins pipeline configuration.

✅ Verification successful

Let me gather more information about the Jenkins pipeline configuration to verify the working directory setup.


No alignment issues found between Dockerfile and Jenkins pipeline

The verification shows that:

  • The Jenkins pipeline doesn't reference any specific working directory paths
  • The pipeline uses ./mvnw for building, which is path-independent
  • Docker build and deployment stages are correctly configured
  • Health checks are properly set up using the correct port (8080) and actuator endpoint
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if Jenkins pipeline references the old /src path
rg "/src" -g "Jenkinsfile"

# Check if deployment stage in Jenkins uses the correct container health check
rg "health|readiness|liveness" -g "Jenkinsfile"

Length of output: 331


Script:

#!/bin/bash
# Check the complete Jenkinsfile content to understand the build and deployment stages
cat Jenkinsfile

# Also check if there are any other files that might reference the working directory
rg "WORKDIR|working.*directory" --type-add 'docker:*.{dockerfile,Dockerfile}' -t docker

Length of output: 3151

README.md (1)

14-15: Verify image paths and add alt text descriptions

The README references images that might not be accessible:

  • images/login.png
  • images/transactions.png

Please ensure:

  1. These images are committed to the repository
  2. The paths are correct
  3. Add meaningful alt text descriptions for better accessibility
✅ Verification successful

Images are correctly referenced and exist in the repository

The referenced images login.png and transactions.png are present in the expected images directory, confirming that the paths in the README.md are correct and accessible.

However, adding descriptive alt text would still improve accessibility:

![Login and Authentication Flow Architecture](images/login.png)
![Banking Transaction Processing Flow](images/transactions.png)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify if the referenced images exist
fd -t f "login.png|transactions.png" -x echo "Found: {}"

Length of output: 117


# Copy source code from local to container
COPY . /src
COPY . /app
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Optimize Docker build performance with selective copying

The current COPY . /app copies all files, which can be inefficient. Consider copying only the necessary files first to leverage Docker's layer caching for dependencies.

-COPY . /app
+COPY pom.xml /app/
+RUN mvn dependency:go-offline
+
+COPY src /app/src/

This change will:

  1. Cache Maven dependencies in a separate layer
  2. Only rebuild when source code changes
  3. Improve build times significantly
📝 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
COPY . /app
COPY pom.xml /app/
RUN mvn dependency:go-offline
COPY src /app/src/

Comment on lines +23 to +26
FROM openjdk:17-alpine AS deployer

# 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
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance security and operational aspects

Consider the following security and operational improvements:

  1. Run the application as a non-root user
  2. Add a HEALTHCHECK instruction
  3. Configure JVM parameters appropriately
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

+HEALTHCHECK --interval=30s --timeout=3s \
+  CMD wget -q --spider http://localhost:8080/actuator/health || exit 1

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

Comment on lines +70 to +74
docker run -itd --name mysql \
-e MYSQL_ROOT_PASSWORD=${DB_PASSWORD} \
-e MYSQL_DATABASE=${DB_NAME} \
--network=bankapp mysql
```
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add security considerations and environment variable setup

The MySQL container configuration needs security improvements and proper documentation:

  1. Environment variables are used but not defined
  2. MySQL container security can be enhanced
  3. Missing instructions for setting up environment variables

Add these security enhancements:

+# First, create a .env file (add to .gitignore):
+DB_NAME=BankDB
+DB_USER=root
+DB_PASSWORD=<strong-password>
+
 docker run -itd --name mysql \  
 -e MYSQL_ROOT_PASSWORD=${DB_PASSWORD} \
 -e MYSQL_DATABASE=${DB_NAME} \  
-  --network=bankapp mysql  
+  --network=bankapp \
+  --health-cmd='mysqladmin ping -h localhost' \
+  --health-interval=10s \
+  --health-timeout=5s \
+  --health-retries=3 \
+  mysql:8.0 \
+  --default-authentication-plugin=mysql_native_password \
+  --character-set-server=utf8mb4 \
+  --collation-server=utf8mb4_unicode_ci
📝 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
docker run -itd --name mysql \
-e MYSQL_ROOT_PASSWORD=${DB_PASSWORD} \
-e MYSQL_DATABASE=${DB_NAME} \
--network=bankapp mysql
```
# First, create a .env file (add to .gitignore):
DB_NAME=BankDB
DB_USER=root
DB_PASSWORD=<strong-password>
docker run -itd --name mysql \
-e MYSQL_ROOT_PASSWORD=${DB_PASSWORD} \
-e MYSQL_DATABASE=${DB_NAME} \
--network=bankapp \
--health-cmd='mysqladmin ping -h localhost' \
--health-interval=10s \
--health-timeout=5s \
--health-retries=3 \
mysql:8.0 \
--default-authentication-plugin=mysql_native_password \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci

Comment on lines +21 to +25
Ensure you have the following:
- **AWS Account**
- **Ubuntu EC2 Instance** (Recommended: `t2.medium`)
- **Installed Tools**:
- Docker
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance prerequisites section with critical details

The prerequisites section should include:

  1. Minimum EC2 specifications (CPU, RAM, Storage)
  2. Required open ports (8080, 3306)
  3. Required AWS IAM permissions
  4. Docker version requirements
  5. Network requirements

Add these details under the "Pre-requisites" section:

 Ensure you have the following:  
 - **AWS Account**  
-  - **Ubuntu EC2 Instance** (Recommended: `t2.medium`)  
+  - **Ubuntu EC2 Instance**:
+    - Type: t2.medium (minimum)
+    - vCPUs: 2
+    - RAM: 4 GB
+    - Storage: 20 GB
 - **Installed Tools**:  
-  - Docker 
+  - Docker (version 20.10 or higher)
+- **Network Requirements**:
+  - Inbound ports: 8080 (application), 3306 (MySQL)
+  - Outbound: All traffic
+- **AWS Permissions**:
+  - EC2 full access
+  - VPC access
📝 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
Ensure you have the following:
- **AWS Account**
- **Ubuntu EC2 Instance** (Recommended: `t2.medium`)
- **Installed Tools**:
- Docker
Ensure you have the following:
- **AWS Account**
- **Ubuntu EC2 Instance**:
- Type: t2.medium (minimum)
- vCPUs: 2
- RAM: 4 GB
- Storage: 20 GB
- **Installed Tools**:
- Docker (version 20.10 or higher)
- **Network Requirements**:
- Inbound ports: 8080 (application), 3306 (MySQL)
- Outbound: All traffic
- **AWS Permissions**:
- EC2 full access
- VPC access

pipeline {
agent {label 'runner_1'}

agent { label 'agent-slave' }
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Update agent label to avoid deprecated terminology

The label 'agent-slave' includes the term "slave", which is deprecated in Jenkins due to its negative connotations. It's recommended to use alternative labels like 'agent' or a more descriptive name that aligns with current best practices.

Comment on lines +11 to +19
git url: 'https://github.com/nkantamani2023/Springboot-BankApp.git', branch: 'DevOps'
timeout(time: 5, unit: 'MINUTES')
{
/* groovylint-disable-next-line DuplicateStringLiteral */
git url: params.REPO_URL ?: 'https://github.com/nkantamani2023/Springboot-BankApp.git',
/* groovylint-disable-next-line DuplicateStringLiteral */
branch: params.BRANCH ?: 'DevOps',
shallow: true
}
Copy link

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 and duplicate git commands in 'Code Clone' stage

There are syntax issues and duplicate git checkout steps in the 'Code Clone' stage:

  • The git step is called twice, which is unnecessary.
  • The timeout block is not correctly formatted with its curly braces.
  • The placement of braces {} is causing improper nesting, which may lead to pipeline execution errors.

Apply this diff to correct the syntax and remove duplication:

 stage('Code Clone') {
     steps {
         echo 'Code Clone Stage'
-        git url: 'https://github.com/nkantamani2023/Springboot-BankApp.git', branch: 'DevOps'
-        timeout(time: 5, unit: 'MINUTES')
-        {
-            /* groovylint-disable-next-line DuplicateStringLiteral */
-            git url: params.REPO_URL ?: 'https://github.com/nkantamani2023/Springboot-BankApp.git',
-            /* groovylint-disable-next-line DuplicateStringLiteral */
-            branch: params.BRANCH ?: 'DevOps',
-            shallow: true
-        }
+        timeout(time: 5, unit: 'MINUTES') {
+            /* groovylint-disable-next-line DuplicateStringLiteral */
+            git url: params.REPO_URL ?: 'https://github.com/nkantamani2023/Springboot-BankApp.git',
+            /* groovylint-disable-next-line DuplicateStringLiteral */
+            branch: params.BRANCH ?: 'DevOps',
+            shallow: true
+        }
     }
 }

This correction:

  • Removes the initial redundant git command.
  • Properly nests the git step within the timeout block.
  • Ensures that parameters are correctly utilized.
📝 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
git url: 'https://github.com/nkantamani2023/Springboot-BankApp.git', branch: 'DevOps'
timeout(time: 5, unit: 'MINUTES')
{
/* groovylint-disable-next-line DuplicateStringLiteral */
git url: params.REPO_URL ?: 'https://github.com/nkantamani2023/Springboot-BankApp.git',
/* groovylint-disable-next-line DuplicateStringLiteral */
branch: params.BRANCH ?: 'DevOps',
shallow: true
}
timeout(time: 5, unit: 'MINUTES') {
/* groovylint-disable-next-line DuplicateStringLiteral */
git url: params.REPO_URL ?: 'https://github.com/nkantamani2023/Springboot-BankApp.git',
/* groovylint-disable-next-line DuplicateStringLiteral */
branch: params.BRANCH ?: 'DevOps',
shallow: true
}

Comment on lines +43 to +66
COMPOSE_PROJECT_NAME = "${JOB_NAME}-${VERSION}"
MAX_RETRIES = 30
RETRY_INTERVAL = 10
}
steps {
script {
// Graceful deployment with zero downtime
sh """
docker compose -p ${COMPOSE_PROJECT_NAME} up -d --build
# Wait for service to be ready
for i in \$(seq 1 \$MAX_RETRIES); do
if curl -sf http://localhost:8080/actuator/health | grep -q '"status":"UP"'; then
echo "Service is healthy"
docker compose -p ${JOB_NAME}-previous down || true
exit 0
fi
echo "Attempt \$i: Service not ready yet..."
sleep \$RETRY_INTERVAL
done
echo "Service failed to become healthy - rolling back"
docker compose -p ${COMPOSE_PROJECT_NAME} down
# Redeploy the previous version
docker compose -p ${JOB_NAME}-previous up -d
exit 1
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Ensure consistent usage of environment variables for deployment and rollback

There are inconsistencies in how environment variables are used during deployment and rollback:

  • COMPOSE_PROJECT_NAME is defined as ${JOB_NAME}-${VERSION}, but during rollback, the script references ${JOB_NAME}-previous.
  • This mismatch may lead to the wrong Docker Compose projects being brought up or down, potentially causing deployment issues.

Consider updating the script to maintain consistency:

  1. Define a PREVIOUS_COMPOSE_PROJECT_NAME for clarity.

  2. Update the deployment and rollback commands to use the correct project names.

Apply this diff to align the variable usage:

 environment {
     COMPOSE_PROJECT_NAME = "${JOB_NAME}-${VERSION}"
+    PREVIOUS_COMPOSE_PROJECT_NAME = "${JOB_NAME}-previous"
     MAX_RETRIES = 30 
     RETRY_INTERVAL = 10
 }
 
 steps {
     script {
         // Graceful deployment with zero downtime
         sh """
     docker compose -p ${COMPOSE_PROJECT_NAME} up -d --build
     # Wait for service to be ready
     for i in \$(seq 1 \$MAX_RETRIES); do
         if curl -sf http://localhost:8080/actuator/health | grep -q '"status":"UP"'; then
             echo "Service is healthy"
-            docker compose -p ${JOB_NAME}-previous down || true
+            docker compose -p ${PREVIOUS_COMPOSE_PROJECT_NAME} down || true
             exit 0
         fi
         echo "Attempt \$i: Service not ready yet..."
         sleep \$RETRY_INTERVAL
     done
     echo "Service failed to become healthy - rolling back"
     docker compose -p ${COMPOSE_PROJECT_NAME} down
     # Redeploy the previous version
-    docker compose -p ${JOB_NAME}-previous up -d
+    docker compose -p ${PREVIOUS_COMPOSE_PROJECT_NAME} up -d
     exit 1
     """
     }
 }

This ensures that the correct Docker Compose projects are managed during deployment and rollback.

📝 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
COMPOSE_PROJECT_NAME = "${JOB_NAME}-${VERSION}"
MAX_RETRIES = 30
RETRY_INTERVAL = 10
}
steps {
script {
// Graceful deployment with zero downtime
sh """
docker compose -p ${COMPOSE_PROJECT_NAME} up -d --build
# Wait for service to be ready
for i in \$(seq 1 \$MAX_RETRIES); do
if curl -sf http://localhost:8080/actuator/health | grep -q '"status":"UP"'; then
echo "Service is healthy"
docker compose -p ${JOB_NAME}-previous down || true
exit 0
fi
echo "Attempt \$i: Service not ready yet..."
sleep \$RETRY_INTERVAL
done
echo "Service failed to become healthy - rolling back"
docker compose -p ${COMPOSE_PROJECT_NAME} down
# Redeploy the previous version
docker compose -p ${JOB_NAME}-previous up -d
exit 1
COMPOSE_PROJECT_NAME = "${JOB_NAME}-${VERSION}"
PREVIOUS_COMPOSE_PROJECT_NAME = "${JOB_NAME}-previous"
MAX_RETRIES = 30
RETRY_INTERVAL = 10
}
steps {
script {
// Graceful deployment with zero downtime
sh """
docker compose -p ${COMPOSE_PROJECT_NAME} up -d --build
# Wait for service to be ready
for i in \$(seq 1 \$MAX_RETRIES); do
if curl -sf http://localhost:8080/actuator/health | grep -q '"status":"UP"'; then
echo "Service is healthy"
docker compose -p ${PREVIOUS_COMPOSE_PROJECT_NAME} down || true
exit 0
fi
echo "Attempt \$i: Service not ready yet..."
sleep \$RETRY_INTERVAL
done
echo "Service failed to become healthy - rolling back"
docker compose -p ${COMPOSE_PROJECT_NAME} down
# Redeploy the previous version
docker compose -p ${PREVIOUS_COMPOSE_PROJECT_NAME} up -d
exit 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments