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

๐Ÿ‘ทci:script๋ณ€๊ฒฝ #39

Merged
merged 2 commits into from
Nov 19, 2024
Merged
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
9 changes: 6 additions & 3 deletions appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ version: 0.0
os: linux

files:
- source: ./
destination: /home/ubuntu/libs
- source: /
destination: /home/ubuntu/app
overwrite: yes

permissions:
- object: /home/ubuntu/libs
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu

hooks:
AfterInstall:
- location: scripts/stop.sh
timeout: 60
runas: ubuntu
ApplicationStart:
- location: scripts/start.sh
timeout: 60
runas: ubuntu
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ java {
languageVersion = JavaLanguageVersion.of(21)
}
}
jar {
enabled=false
}

configurations {
compileOnly {
Expand Down
38 changes: 15 additions & 23 deletions scripts/start.sh
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
#!/bin/bash
#!/usr/bin/env bash

# Set the path where your JAR files are located
ROOT_PATH="/home/ubuntu/libs" # Adjust to the correct directory
JAR="$ROOT_PATH/application.jar"
PROJECT_ROOT="/home/ubuntu/app"
JAR_FILE="$PROJECT_ROOT/spring-webapp.jar"

APP_LOG="$ROOT_PATH/application.log"
ERROR_LOG="$ROOT_PATH/error.log"
START_LOG="$ROOT_PATH/start.log"
APP_LOG="$PROJECT_ROOT/application.log"
ERROR_LOG="$PROJECT_ROOT/error.log"
DEPLOY_LOG="$PROJECT_ROOT/deploy.log"

NOW=$(date +%c)
TIME_NOW=$(date +%c)

# Log the JAR copy action
echo "[$NOW] $JAR ๋ณต์‚ฌ" >> $START_LOG
# build ํŒŒ์ผ ๋ณต์‚ฌ
echo "$TIME_NOW > $JAR_FILE ํŒŒ์ผ ๋ณต์‚ฌ" >> $DEPLOY_LOG
cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE

# Adjusted copy path to the build directory, assuming your JAR is in /libs/
cp $ROOT_PATH/spring-github-action-1.0.0.jar $JAR
# jar ํŒŒ์ผ ์‹คํ–‰
echo "$TIME_NOW > $JAR_FILE ํŒŒ์ผ ์‹คํ–‰" >> $DEPLOY_LOG
nohup java -jar $JAR_FILE > $APP_LOG 2> $ERROR_LOG &

# Log the JAR execution
echo "[$NOW] > $JAR ์‹คํ–‰" >> $START_LOG

# Run the application in the background
nohup java -jar $JAR > $APP_LOG 2> $ERROR_LOG &

# Get the service PID
SERVICE_PID=$(pgrep -f $JAR)

# Log the service PID
echo "[$NOW] > ์„œ๋น„์Šค PID: $SERVICE_PID" >> $START_LOG
CURRENT_PID=$(pgrep -f $JAR_FILE)
echo "$TIME_NOW > ์‹คํ–‰๋œ ํ”„๋กœ์„ธ์Šค ์•„์ด๋”” $CURRENT_PID ์ž…๋‹ˆ๋‹ค." >> $DEPLOY_LOG
27 changes: 16 additions & 11 deletions scripts/stop.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/bin/bash
#!/usr/bin/env bash

ROOT_PATH="/home/ubuntu/libs" # JAR ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ๋””๋ ‰ํ† ๋ฆฌ
JAR="$ROOT_PATH/demo-0.0.1-SNAPSHOT.jar" # ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ๋‚ด JAR ํŒŒ์ผ
STOP_LOG="$ROOT_PATH/stop.log"
SERVICE_PID=$(pgrep -f $JAR) # ์‹คํ–‰์ค‘์ธ Spring ์„œ๋ฒ„์˜ PID
PROJECT_ROOT="/home/ubuntu/app"
JAR_FILE="$PROJECT_ROOT/spring-webapp.jar"

if [ -z "$SERVICE_PID" ]; then
echo "์„œ๋น„์Šค NouFound" >> $STOP_LOG
DEPLOY_LOG="$PROJECT_ROOT/deploy.log"

TIME_NOW=$(date +%c)

# ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ
CURRENT_PID=$(pgrep -f $JAR_FILE)

# ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผœ์ ธ ์žˆ์œผ๋ฉด ์ข…๋ฃŒ
if [ -z $CURRENT_PID ]; then
echo "$TIME_NOW > ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์Šต๋‹ˆ๋‹ค" >> $DEPLOY_LOG
else
echo "์„œ๋น„์Šค ์ข…๋ฃŒ " >> $STOP_LOG
kill "$SERVICE_PID"
# kill -9 $SERVICE_PID # ๊ฐ•์ œ ์ข…๋ฃŒ๋ฅผ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด ๋ช…๋ น์–ด ์‚ฌ์šฉ
fi
echo "$TIME_NOW > ์‹คํ–‰์ค‘์ธ $CURRENT_PID ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ " >> $DEPLOY_LOG
kill -15 $CURRENT_PID
fi
Loading