From 8d548075b622ee8a07dd6357e256dc99a9fcfc48 Mon Sep 17 00:00:00 2001 From: Kimdongjun Date: Tue, 27 Aug 2024 17:19:56 +0900 Subject: [PATCH] Update gradle.yml --- .github/workflows/gradle.yml | 37 ++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 9d6de70..0a1fcdc 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -18,35 +18,40 @@ jobs: PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }} HOSTNAME: ${{ secrets.SERVER_IP }} USER_NAME: ${{ secrets.SSH_USERNAME }} - run: | - echo "$PRIVATE_KEY" > private_key && chmod 600 private_key - ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} ' - - # Navigate to the application directory - cd /home/ubuntu/FOREGG_SERVER && + echo "$PRIVATE_KEY" > private_key + chmod 600 private_key + ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} << 'EOF' + # Navigate to the application directory + cd /home/ubuntu/FOREGG_SERVER || + { echo "Failed to navigate to directory"; exit 1; } + # Fetch the latest code git checkout main && git fetch --all && git reset --hard origin/main && - git pull origin main && - + git pull origin main || + { echo "Failed to update code"; exit 1; } + # Build the Spring application using Gradle - ./gradlew clean build && - + ./gradlew clean build || + { echo "Gradle build failed"; exit 1; } + # Find the process ID (PID) using port 8080 and kill it - PID=$(sudo netstat -tnlp | grep ":8080" | awk \'{print $7}\' | cut -d/ -f1) && + PID=$(sudo netstat -tnlp | grep ":8080" | awk '{print $7}' | cut -d/ -f1) && if [ -n "$PID" ]; then echo "Stopping process with PID $PID" && sudo kill -9 $PID else echo "No process found on port 8080" - fi && - + fi || + { echo "Failed to stop old process"; exit 1; } + # Replace the old JAR with the new one - sudo cp build/libs/*.jar /cicd && - + sudo cp build/libs/*.jar /cicd || + { echo "Failed to copy JAR file"; exit 1; } + # Start the application again nohup java -jar /cicd/*.jar > /dev/null 2>&1 & - ' + EOF