Skip to content
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

adding feature -2 files #1

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
86 changes: 86 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: Build, Deploy, and Upload Artifacts

on:
push:
branches:
- '**'
pull_request:
branches:
- master
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
# Checkout the repository
- name: Checkout Code
uses: actions/checkout@v3

# Set up Java
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update GitHub Actions to latest versions

The workflow uses outdated versions of GitHub Actions. Update them to the latest stable versions for improved security and features.

-        uses: actions/checkout@v3
+        uses: actions/checkout@v4

-        uses: actions/setup-java@v3
+        uses: actions/setup-java@v4

Also, consider upgrading to Java 17 (LTS) for better performance and longer support lifecycle.

📝 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
- name: Checkout Code
uses: actions/checkout@v3
# Set up Java
- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- name: Checkout Code
uses: actions/checkout@v4
# Set up Java
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
🧰 Tools
🪛 actionlint (1.7.4)

19-19: the runner of "actions/checkout@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)


23-23: the runner of "actions/setup-java@v3" action is too old to run on GitHub Actions. update the action's version to fix this issue

(action)

# Cache Maven dependencies
- name: Cache Maven dependencies
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-

# Build the project with Maven
- name: Build with Maven
run: mvn clean install

# Upload the JAR as an artifact using v4
- name: Upload JAR as artifact
uses: actions/upload-artifact@v4
with:
name: bus-booking-app
path: target/simple-parcel-service-app-1.0-SNAPSHOT.jar

# Run the Spring Boot application
- name: Run Spring Boot App
run: mvn spring-boot:run &
env:
SPRING_PROFILES_ACTIVE: "test"

# Display the IP address and port number for accessing the app
- name: Display IP Address and Port
run: |
echo "Fetching the runner's IP address..."
IP_ADDRESS=$(curl -s ifconfig.me) # This fetches the public IP of the runner
PORT=8080
echo "The app is accessible at: http://$IP_ADDRESS:$PORT"

# Validate that the application is running by sending a request to the app
- name: Validate App is Running
run: |
echo "Waiting for the app to start..."
sleep 15 # Allow some time for the Spring Boot app to fully start
echo "Checking if the app is running..."
RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080)
if [ "$RESPONSE" -eq 200 ]; then
echo "The app is running successfully!"
else
echo "The app failed to start. HTTP response code: $RESPONSE"
exit 1
fi
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve application startup validation

The current validation approach has several potential issues:

  1. Fixed 15-second sleep might not be sufficient for app startup
  2. Using root path (/) for health check might not be reliable
  3. No proper error handling for startup failures
       - name: Run Spring Boot App
         run: mvn spring-boot:run & 
         env:
           SPRING_PROFILES_ACTIVE: "test"
+          MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE: "health"
+          MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED: "true"

       - name: Validate App is Running
         run: |
           echo "Waiting for the app to start..."
-          sleep 15
+          max_attempts=30
+          attempt=1
+          while [ $attempt -le $max_attempts ]; do
+            RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080/actuator/health)
+            if [ "$RESPONSE" -eq 200 ]; then
+              echo "The app is running successfully!"
+              break
+            fi
+            echo "Attempt $attempt: App is not ready yet. Waiting..."
+            sleep 10
+            attempt=$((attempt + 1))
+          done
+          
+          if [ $attempt -gt $max_attempts ]; then
+            echo "The app failed to start after $max_attempts attempts"
+            exit 1
+          fi
-          echo "Checking if the app is running..."
-          RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080)
-          if [ "$RESPONSE" -eq 200 ]; then
-            echo "The app is running successfully!"
-          else
-            echo "The app failed to start. HTTP response code: $RESPONSE"
-            exit 1
-          fi
📝 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
- name: Run Spring Boot App
run: mvn spring-boot:run &
env:
SPRING_PROFILES_ACTIVE: "test"
# Display the IP address and port number for accessing the app
- name: Display IP Address and Port
run: |
echo "Fetching the runner's IP address..."
IP_ADDRESS=$(curl -s ifconfig.me) # This fetches the public IP of the runner
PORT=8080
echo "The app is accessible at: http://$IP_ADDRESS:$PORT"
# Validate that the application is running by sending a request to the app
- name: Validate App is Running
run: |
echo "Waiting for the app to start..."
sleep 15 # Allow some time for the Spring Boot app to fully start
echo "Checking if the app is running..."
RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080)
if [ "$RESPONSE" -eq 200 ]; then
echo "The app is running successfully!"
else
echo "The app failed to start. HTTP response code: $RESPONSE"
exit 1
fi
- name: Run Spring Boot App
run: mvn spring-boot:run &
env:
SPRING_PROFILES_ACTIVE: "test"
MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE: "health"
MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED: "true"
# Display the IP address and port number for accessing the app
- name: Display IP Address and Port
run: |
echo "Fetching the runner's IP address..."
IP_ADDRESS=$(curl -s ifconfig.me) # This fetches the public IP of the runner
PORT=8080
echo "The app is accessible at: http://$IP_ADDRESS:$PORT"
# Validate that the application is running by sending a request to the app
- name: Validate App is Running
run: |
echo "Waiting for the app to start..."
max_attempts=30
attempt=1
while [ $attempt -le $max_attempts ]; do
RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080/actuator/health)
if [ "$RESPONSE" -eq 200 ]; then
echo "The app is running successfully!"
break
fi
echo "Attempt $attempt: App is not ready yet. Waiting..."
sleep 10
attempt=$((attempt + 1))
done
if [ $attempt -gt $max_attempts ]; then
echo "The app failed to start after $max_attempts attempts"
exit 1
fi
🧰 Tools
🪛 yamllint (1.35.1)

[error] 50-50: trailing spaces

(trailing-spaces)


# Wait for 5 minutes
- name: Wait for 5 minutes
run: |
echo "App has been running for 5 minutes. Waiting..."
sleep 300 # Wait for 5 minutes (300 seconds)

# Stop the Spring Boot app gracefully using spring-boot:stop
- name: Gracefully Stop Spring Boot App
run: |
echo "Stopping the app gracefully..."
mvn spring-boot:stop
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve application shutdown process

The current shutdown process has several areas for improvement:

  1. Fixed 5-minute wait time seems arbitrary
  2. No verification of successful shutdown
  3. No timeout for shutdown operation
-      - name: Wait for 5 minutes
-        run: |
-          echo "App has been running for 5 minutes. Waiting..."
-          sleep 300  # Wait for 5 minutes (300 seconds)

       - name: Gracefully Stop Spring Boot App
         run: |
           echo "Stopping the app gracefully..."
           mvn spring-boot:stop
+          
+          # Verify shutdown
+          max_attempts=10
+          attempt=1
+          while [ $attempt -le $max_attempts ]; do
+            if ! curl -s http://localhost:8080/actuator/health > /dev/null; then
+              echo "Application stopped successfully"
+              exit 0
+            fi
+            echo "Attempt $attempt: Waiting for application to stop..."
+            sleep 5
+            attempt=$((attempt + 1))
+          done
+          
+          echo "Failed to verify application shutdown"
+          exit 1
📝 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
# Wait for 5 minutes
- name: Wait for 5 minutes
run: |
echo "App has been running for 5 minutes. Waiting..."
sleep 300 # Wait for 5 minutes (300 seconds)
# Stop the Spring Boot app gracefully using spring-boot:stop
- name: Gracefully Stop Spring Boot App
run: |
echo "Stopping the app gracefully..."
mvn spring-boot:stop
# Stop the Spring Boot app gracefully using spring-boot:stop
- name: Gracefully Stop Spring Boot App
run: |
echo "Stopping the app gracefully..."
mvn spring-boot:stop
# Verify shutdown
max_attempts=10
attempt=1
while [ $attempt -le $max_attempts ]; do
if ! curl -s http://localhost:8080/actuator/health > /dev/null; then
echo "Application stopped successfully"
exit 0
fi
echo "Attempt $attempt: Waiting for application to stop..."
sleep 5
attempt=$((attempt + 1))
done
echo "Failed to verify application shutdown"
exit 1

190 changes: 189 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,189 @@
# e-commerce
sudo apt update
sudo apt install -y openjdk-11-jdk
Verify the installation:

bash
java -version
Install Maven: If Maven is not already installed, use:

bash
sudo apt install -y maven
Verify the installation:

bash
mvn -version
Set Environment Variables: Ensure JAVA_HOME is set to the Java 11 installation path. Add the following lines to your .bashrc or .zshrc file:

bash
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$JAVA_HOME/bin:$PATH
Apply the changes:

bash
source ~/.bashrc
Build the Project
To build the project, use the following commands:

Navigate to the Project Directory:

bash
cd /path/to/simple-parcel-service-app
Clean and Build the Project:

bash
mvn clean install
This command will:
Download dependencies
Compile the source code
Run tests
Package the application into a JAR file (target/simple-parcel-service-app-1.0-SNAPSHOT.jar)
Run the Application
You can run the application in two ways:

1. Using Maven:
bash

mvn spring-boot:run

3. Using the Packaged JAR:
After building the project, run the packaged JAR file:

bash

java -jar target/simple-parcel-service-app-1.0-SNAPSHOT.jar
The application will start and be accessible at http://localhost:8080
Comment on lines +1 to +54
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 installation instructions with security considerations and prerequisites

The installation instructions need several improvements:

  1. Add a prerequisites section mentioning required sudo access
  2. Include package verification steps (checksums/signatures)
  3. Specify the minimum required Maven version
  4. Use proper URL formatting for localhost reference

Apply these changes:

+# Prerequisites
+- sudo access
+- Internet connectivity
+
 sudo apt update
-sudo apt install -y openjdk-11-jdk
+# Download and verify Java 11
+wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
+sudo apt install -y openjdk-11-jdk
 
-Verify the installation:
+# Verify Java installation:
 java -version
 
-Install Maven: If Maven is not already installed, use:
+# Install Maven (minimum version 3.6.3):
+wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz.sha512
+wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
+echo "$(cat apache-maven-3.6.3-bin.tar.gz.sha512) apache-maven-3.6.3-bin.tar.gz" | sha512sum -c
 sudo apt install -y maven
 
-The application will start and be accessible at http://localhost:8080
+The application will start and be accessible at [http://localhost:8080](http://localhost:8080)

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

🧰 Tools
🪛 Markdownlint (0.37.0)

54-54: null
Bare URL used

(MD034, no-bare-urls)

===========================================================================================================================================================================
============================================================================================================================================================================
Shell script to setup the environment.

#!/bin/bash
set -e
echo "Starting Maven project setup..."

# Step 1: Install Java 11
if ! java -version &>/dev/null; then
echo "Installing Java 11..."
sudo apt update
sudo apt install -y openjdk-11-jdk
else
echo "Java is already installed:"
java -version
fi

# Set JAVA_HOME
JAVA_HOME_PATH=$(dirname $(dirname $(readlink -f $(which java))))
if ! grep -q "JAVA_HOME=$JAVA_HOME_PATH" /etc/environment; then
echo "Setting JAVA_HOME..."
echo "JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/environment
echo "export JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' | sudo tee -a /etc/profile
source /etc/profile
echo "JAVA_HOME set to $JAVA_HOME_PATH"
else
echo "JAVA_HOME is already set."
fi

# Step 2: Install Maven
if ! mvn -version &>/dev/null; then
echo "Installing Maven..."
sudo apt install -y maven
else
echo "Maven is already installed:"
mvn -version
fi

# Step 3: Clone the Repository
REPO_URL="https://github.com/your-username/simple-parcel-service-app.git"
PROJECT_DIR="simple-parcel-service-app"
if [ ! -d "$PROJECT_DIR" ]; then
echo "Cloning the repository..."
git clone $REPO_URL
else
echo "Repository already cloned."
fi

echo "Environment setup completed. You can now build and run the application manually."
Comment on lines +59 to +105
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve script robustness and maintainability

The shell script needs several improvements for better reliability and maintainability:

  1. Repository URL should be configurable
  2. Add proper error handling
  3. Validate successful installation of components

Apply these changes:

 #!/bin/bash
-set -e
+set -euo pipefail
+
+# Configuration
+REPO_URL=${REPO_URL:-"https://github.com/your-username/simple-parcel-service-app.git"}
+MIN_JAVA_VERSION="11"
+MIN_MAVEN_VERSION="3.6.3"
+
+# Error handling
+error_exit() {
+    echo "Error: ${1:-"Unknown Error"}" >&2
+    exit 1
+}
+
+# Version validation
+validate_java_version() {
+    local version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d'.' -f1)
+    [[ "$version" -ge $MIN_JAVA_VERSION ]] || error_exit "Java version $MIN_JAVA_VERSION or higher is required"
+}
+
+validate_maven_version() {
+    local version=$(mvn -version | awk 'NR==1{print $3}' | cut -d'.' -f1-2)
+    [[ "$(echo -e "$version\n$MIN_MAVEN_VERSION" | sort -V | head -n1)" == "$MIN_MAVEN_VERSION" ]] || \
+        error_exit "Maven version $MIN_MAVEN_VERSION or higher is required"
+}

 echo "Starting Maven project setup..."
 
 # Step 1: Install Java 11
 if ! java -version &>/dev/null; then
     echo "Installing Java 11..."
     sudo apt update
     sudo apt install -y openjdk-11-jdk
+    validate_java_version || error_exit "Java installation failed"
 else
     echo "Java is already installed:"
     java -version
+    validate_java_version
 fi

 # Step 2: Install Maven
 if ! mvn -version &>/dev/null; then
     echo "Installing Maven..."
     sudo apt install -y maven
+    validate_maven_version || error_exit "Maven installation failed"
 else
     echo "Maven is already installed:"
     mvn -version
+    validate_maven_version
 fi

-REPO_URL="https://github.com/your-username/simple-parcel-service-app.git"
 PROJECT_DIR="simple-parcel-service-app"
 if [ ! -d "$PROJECT_DIR" ]; then
     echo "Cloning the repository..."
-    git clone $REPO_URL
+    git clone "$REPO_URL" || error_exit "Failed to clone repository"
 else
     echo "Repository already cloned."
 fi
📝 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
#!/bin/bash
set -e
echo "Starting Maven project setup..."
# Step 1: Install Java 11
if ! java -version &>/dev/null; then
echo "Installing Java 11..."
sudo apt update
sudo apt install -y openjdk-11-jdk
else
echo "Java is already installed:"
java -version
fi
# Set JAVA_HOME
JAVA_HOME_PATH=$(dirname $(dirname $(readlink -f $(which java))))
if ! grep -q "JAVA_HOME=$JAVA_HOME_PATH" /etc/environment; then
echo "Setting JAVA_HOME..."
echo "JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/environment
echo "export JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' | sudo tee -a /etc/profile
source /etc/profile
echo "JAVA_HOME set to $JAVA_HOME_PATH"
else
echo "JAVA_HOME is already set."
fi
# Step 2: Install Maven
if ! mvn -version &>/dev/null; then
echo "Installing Maven..."
sudo apt install -y maven
else
echo "Maven is already installed:"
mvn -version
fi
# Step 3: Clone the Repository
REPO_URL="https://github.com/your-username/simple-parcel-service-app.git"
PROJECT_DIR="simple-parcel-service-app"
if [ ! -d "$PROJECT_DIR" ]; then
echo "Cloning the repository..."
git clone $REPO_URL
else
echo "Repository already cloned."
fi
echo "Environment setup completed. You can now build and run the application manually."
#!/bin/bash
set -euo pipefail
# Configuration
REPO_URL=${REPO_URL:-"https://github.com/your-username/simple-parcel-service-app.git"}
MIN_JAVA_VERSION="11"
MIN_MAVEN_VERSION="3.6.3"
# Error handling
error_exit() {
echo "Error: ${1:-"Unknown Error"}" >&2
exit 1
}
# Version validation
validate_java_version() {
local version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}' | cut -d'.' -f1)
[[ "$version" -ge $MIN_JAVA_VERSION ]] || error_exit "Java version $MIN_JAVA_VERSION or higher is required"
}
validate_maven_version() {
local version=$(mvn -version | awk 'NR==1{print $3}' | cut -d'.' -f1-2)
[[ "$(echo -e "$version\n$MIN_MAVEN_VERSION" | sort -V | head -n1)" == "$MIN_MAVEN_VERSION" ]] || \
error_exit "Maven version $MIN_MAVEN_VERSION or higher is required"
}
echo "Starting Maven project setup..."
# Step 1: Install Java 11
if ! java -version &>/dev/null; then
echo "Installing Java 11..."
sudo apt update
sudo apt install -y openjdk-11-jdk
validate_java_version || error_exit "Java installation failed"
else
echo "Java is already installed:"
java -version
validate_java_version
fi
# Set JAVA_HOME
JAVA_HOME_PATH=$(dirname $(dirname $(readlink -f $(which java))))
if ! grep -q "JAVA_HOME=$JAVA_HOME_PATH" /etc/environment; then
echo "Setting JAVA_HOME..."
echo "JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/environment
echo "export JAVA_HOME=$JAVA_HOME_PATH" | sudo tee -a /etc/profile
echo 'export PATH=$JAVA_HOME/bin:$PATH' | sudo tee -a /etc/profile
source /etc/profile
echo "JAVA_HOME set to $JAVA_HOME_PATH"
else
echo "JAVA_HOME is already set."
fi
# Step 2: Install Maven
if ! mvn -version &>/dev/null; then
echo "Installing Maven..."
sudo apt install -y maven
validate_maven_version || error_exit "Maven installation failed"
else
echo "Maven is already installed:"
mvn -version
validate_maven_version
fi
# Step 3: Clone the Repository
PROJECT_DIR="simple-parcel-service-app"
if [ ! -d "$PROJECT_DIR" ]; then
echo "Cloning the repository..."
git clone "$REPO_URL" || error_exit "Failed to clone repository"
else
echo "Repository already cloned."
fi
echo "Environment setup completed. You can now build and run the application manually."
🧰 Tools
🪛 LanguageTool

[style] ~98-~98: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 3282 characters long)
Context: ...CT_DIR="simple-parcel-service-app" if [ ! -d "$PROJECT_DIR" ]; then echo "Clo...

(EN_EXCESSIVE_EXCLAMATION)

🪛 Markdownlint (0.37.0)

63-63: Expected: setext; Actual: atx
Heading style

(MD003, heading-style)


73-73: Expected: setext; Actual: atx
Heading style

(MD003, heading-style)


86-86: Expected: setext; Actual: atx
Heading style

(MD003, heading-style)


95-95: Expected: setext; Actual: atx
Heading style

(MD003, heading-style)


59-59: null
No space after hash on atx style heading

(MD018, no-missing-space-atx)


96-96: null
Bare URL used

(MD034, no-bare-urls)

==============================================================================================================================================================================
Build the project manually
mvn clean install

Run the application manually
mvn spring-boot:run
OR
java -jar target/simple-parcel-service-app-1.0-SNAPSHOT.jar
==============================================================================================================================================================================

name: Build, Deploy, and Run

on:
push:
branches:
- main
pull_request:

jobs:
build-and-run:
runs-on: ubuntu-latest

steps:
# Step 1: Checkout code
- name: Checkout Code
uses: actions/checkout@v4

# Step 2: Set up Java 11
- name: Set up Java 11
uses: actions/setup-java@v3
with:
java-version: '11'

# Step 3: Set up Maven
- name: Set up Maven
uses: actions/setup-java@v3
with:
distribution: 'maven'

# Step 4: Build the project
- name: Build with Maven
run: mvn clean package

# Step 5: Upload the built JAR as an artifact
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: built-jar
path: target/simple-parcel-service-app-1.0-SNAPSHOT.jar

# Step 6: Run the Spring Boot application
- name: Run Application
run: |
nohup mvn spring-boot:run &
sleep 15 # Allow time for the app to fully start

# Step 7: Validate App is Running
- name: Validate App is Running
run: |
echo "Waiting for the app to start..."
sleep 15 # Allow some time for the Spring Boot app to fully start
echo "Checking if the app is running..."
RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080)
if [ "$RESPONSE" -eq 200 ]; then
echo "The app is running successfully!"
else
echo "The app failed to start. HTTP response code: $RESPONSE"
exit 1
fi

# Step 8: Wait for 5 minutes
- name: Wait for 5 minutes
run: |
echo "App has been running for 5 minutes. Waiting..."
sleep 300 # Wait for 5 minutes (300 seconds)

# Step 9: Gracefully Stop Spring Boot App
- name: Gracefully Stop Spring Boot App
run: |
echo "Stopping the app gracefully..."
mvn spring-boot:stop
============================================================================================================================================================================
Comment on lines +116 to +187
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve GitHub Actions workflow reliability and efficiency

The workflow needs several improvements:

  1. Add Maven dependency caching
  2. Replace sleep commands with proper health checks
  3. Add explicit test execution
  4. Configure minimum required permissions

Apply these changes:

 name: Build, Deploy, and Run
 
 on:
   push:
     branches:
       - main
   pull_request:
 
+permissions:
+  contents: read
+  packages: read
+
 jobs:
   build-and-run:
     runs-on: ubuntu-latest
 
     steps:
     - name: Checkout Code
       uses: actions/checkout@v4
 
     - name: Set up Java 11
       uses: actions/setup-java@v3
       with:
         java-version: '11'
+        distribution: 'temurin'
+        cache: 'maven'
 
-    - name: Set up Maven
-      uses: actions/setup-java@v3
-      with:
-        distribution: 'maven'
+    # Step 4: Run tests
+    - name: Test with Maven
+      run: mvn test
 
     - name: Build with Maven
       run: mvn clean package
 
     - name: Upload Artifact
       uses: actions/upload-artifact@v4
       with:
         name: built-jar
         path: target/simple-parcel-service-app-1.0-SNAPSHOT.jar
 
     - name: Run Application
       run: |
         nohup mvn spring-boot:run &
-        sleep 15 # Allow time for the app to fully start
+        echo "Waiting for application to start..."
+        timeout 60 bash -c 'until curl -s http://localhost:8080/actuator/health | grep -q "UP"; do sleep 1; done'
 
     - name: Validate App is Running
       run: |
-        echo "Waiting for the app to start..."
-        sleep 15  # Allow some time for the Spring Boot app to fully start
         echo "Checking if the app is running..."
-        RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080)
+        RESPONSE=$(curl --write-out "%{http_code}" --silent --output /dev/null http://localhost:8080/actuator/health)
         if [ "$RESPONSE" -eq 200 ]; then
           echo "The app is running successfully!"
         else
           echo "The app failed to start. HTTP response code: $RESPONSE"
           exit 1
         fi
 
     - name: Wait for 5 minutes
       run: |
         echo "App has been running for 5 minutes. Waiting..."
-        sleep 300  # Wait for 5 minutes (300 seconds)
+        timeout 300 bash -c 'while curl -s http://localhost:8080/actuator/health | grep -q "UP"; do sleep 10; done'
 
     - name: Gracefully Stop Spring Boot App
       run: |
         echo "Stopping the app gracefully..."
-        mvn spring-boot:stop
+        pkill -f 'java.*simple-parcel-service-app'

Note: This assumes you have the Spring Boot Actuator dependency added to your project for health checks. If not, you'll need to add it to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>



49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>simple-parcel-service-app</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/>
</parent>

<properties>
<java.version>8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

12 changes: 12 additions & 0 deletions src/main/java/com/example/SimpleParcelServiceApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SimpleParcelServiceApp {
public static void main(String[] args) {
SpringApplication.run(SimpleParcelServiceApp.class, args);
}
}

38 changes: 38 additions & 0 deletions src/main/java/com/example/controller/ParcelController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/")
public class ParcelController {

@GetMapping
public String index() {
return "index";
}

@PostMapping("/sendParcel")
public String sendParcel(
@RequestParam("recipientName") String recipientName,
@RequestParam("recipientAddress") String recipientAddress,
@RequestParam("senderName") String senderName,
@RequestParam("senderAddress") String senderAddress,
@RequestParam("parcelContent") String parcelContent,
Model model
) {
// You can do something with the data, e.g., save it to a database
// For now, let's just add it to the model and display a confirmation
model.addAttribute("recipientName", recipientName);
model.addAttribute("recipientAddress", recipientAddress);
model.addAttribute("senderName", senderName);
model.addAttribute("senderAddress", senderAddress);
model.addAttribute("parcelContent", parcelContent);
return "confirmation";
}
}

6 changes: 6 additions & 0 deletions src/main/java/com/example/model/Parcel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.model;

public class Parcel {
// Add fields as needed
}
Comment on lines +3 to +5
Copy link

Choose a reason for hiding this comment

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

The Parcel class is currently empty. To represent parcel data within the application, fields and possibly methods (such as getters and setters) should be implemented.


9 changes: 9 additions & 0 deletions src/main/java/com/example/service/ParcelService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.service;

import org.springframework.stereotype.Service;

@Service
public class ParcelService {
// Add service methods as needed
Comment on lines +6 to +7
Copy link

Choose a reason for hiding this comment

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

The ParcelService class is currently empty. Service methods that contain the business logic for managing parcels should be implemented.

}

Loading
Loading