Merge pull request #138 from woowa-techcamp-2024/feature/135-search-r… #43
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 개발 서버 배포 | |
on: | |
push: | |
branches: | |
- dev | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: JDK 21 설정 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '21' | |
distribution: 'zulu' | |
- name: gradlew 실행 권한 부여 | |
run: chmod +x gradlew | |
- name: JAR 빌드 | |
run: ./gradlew clean build | |
- name: 빌드된 JAR 파일 확인 | |
run: | | |
echo "빌드된 JAR 파일 목록:" | |
find service/*/build/libs -name "*.jar" | |
- name: 변경된 서비스 확인 | |
id: check_changes | |
uses: dorny/paths-filter@v2 | |
with: | |
filters: | | |
restaurant-exposure: | |
- 'service/restaurant-exposure-service/**' | |
search: | |
- 'service/search-service/**' | |
cache: | |
- 'service/cache-service/**' | |
advertisement: | |
- 'service/advertisement-service/**' | |
coupon: | |
- 'service/coupon-service/**' | |
delivery-time: | |
- 'service/delivery-time-service/**' | |
restaurant: | |
- 'service/restaurant-service/**' | |
- name: SSH 키 설정 | |
env: | |
PRIVATE_KEY: ${{ secrets.DEV_EC2_SSH_KEY }} | |
run: | | |
echo "$PRIVATE_KEY" > private_key | |
chmod 600 private_key | |
- name: 서비스 배포 함수 정의 | |
run: | | |
deploy_service() { | |
local service_name="$1" | |
local host="$2" | |
local jar_path="$3" | |
local user="${{ secrets.DEV_EC2_USER }}" | |
echo "Deploying $service_name to $host" | |
scp -o StrictHostKeyChecking=no -i private_key "$jar_path" "${user}@${host}:~/${service_name}.jar" | |
ssh -o StrictHostKeyChecking=no -i private_key "${user}@${host}" << EOF | |
# 기존 프로세스 종료 | |
pid=\$(pgrep -f "java -jar ${service_name}.jar") | |
if [ ! -z "\$pid" ]; then | |
echo "Stopping existing process..." | |
kill \$pid | |
for i in {1..30}; do | |
if ! ps -p \$pid > /dev/null; then | |
break | |
fi | |
echo "Waiting for process to stop... (\$i seconds)" | |
sleep 1 | |
done | |
if ps -p \$pid > /dev/null; then | |
echo "Force killing the process..." | |
kill -9 \$pid | |
fi | |
fi | |
echo "Removing old log file..." | |
rm -f ${service_name}.log | |
echo "Starting new process..." | |
source ~/.bash_profile | |
nohup java -jar ${service_name}.jar --spring.profiles.active=dev > ${service_name}.log 2>&1 & | |
echo "Waiting for service to start..." | |
for i in {1..60}; do | |
if grep -q "Started .* in .*. seconds" ${service_name}.log; then | |
echo "Service started successfully" | |
exit 0 | |
fi | |
echo "Waiting for service to start... (\$i seconds)" | |
sleep 1 | |
done | |
echo "Service did not start within the expected time" | |
exit 1 | |
EOF | |
} | |
- name: 가게노출 서비스 배포 | |
if: steps.check_changes.outputs.restaurant-exposure == 'true' | |
run: deploy_service "restaurant-exposure-service" "${{ secrets.DEV_EC2_HOST1 }}" "service/restaurant-exposure-service/build/libs/*.jar" | |
- name: 검색 서비스 배포 | |
if: steps.check_changes.outputs.search == 'true' | |
run: deploy_service "search-service" "${{ secrets.DEV_EC2_HOST2 }}" "service/search-service/build/libs/*.jar" | |
- name: 캐시 서비스 배포 | |
if: steps.check_changes.outputs.cache == 'true' | |
run: deploy_service "cache-service" "${{ secrets.DEV_EC2_HOST3 }}" "service/cache-service/build/libs/*.jar" | |
- name: 광고 서비스 배포 | |
if: steps.check_changes.outputs.advertisement == 'true' | |
run: deploy_service "advertisement-service" "${{ secrets.DEV_EC2_HOST3 }}" "service/advertisement-service/build/libs/*.jar" | |
- name: 쿠폰 서비스 배포 | |
if: steps.check_changes.outputs.coupon == 'true' | |
run: deploy_service "coupon-service" "${{ secrets.DEV_EC2_HOST3 }}" "service/coupon-service/build/libs/*.jar" | |
- name: 배달시간 서비스 배포 | |
if: steps.check_changes.outputs.delivery-time == 'true' | |
run: deploy_service "delivery-time-service" "${{ secrets.DEV_EC2_HOST3 }}" "service/delivery-time-service/build/libs/*.jar" | |
- name: 레스토랑 서비스 배포 | |
if: steps.check_changes.outputs.restaurant == 'true' | |
run: deploy_service "restaurant-service" "${{ secrets.DEV_EC2_HOST3 }}" "service/restaurant-service/build/libs/*.jar" |