ci cd 수정 #39
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: Java CI with Gradle | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# Gradle caching - 빌드 시간 향상 | |
- name: Gradle Caching | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Run chmod to make gradlew executable | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: ./gradlew build -x test | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
- name: Build Docker Image | |
run: | | |
docker build -t ${{ secrets.IMAGE_NAME }}:latest . | |
- name: Push Docker Image to Docker Hub | |
run: | | |
docker push ${{ secrets.IMAGE_NAME }}:latest | |
- name: Deploy | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USER }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
port: 22 | |
script: | | |
docker stop gratitude-container || true | |
docker rm -f gratitude-container || true | |
docker pull ${{ secrets.IMAGE_NAME }}:latest | |
docker run -d --name gratitude-container -p 8080:8080 -e DB_URL="${{ secrets.DB_URL }}" -e DB_USERNAME="${{ secrets.DB_USERNAME }}" -e DB_PASSWORD="${{ secrets.DB_PASSWORD }}" -e JWT_SECRET="${{ secrets.JWT_SECRET }}" -e AI_SERVER="${{ secrets.AI_SERVER }}" ${{ secrets.IMAGE_NAME }}:latest | |
docker image prune -f | |
# 8. 성공 시 디스코드 알림 | |
- name: Send success Discord notification | |
if: success() | |
run: | | |
curl -H "Content-Type: application/json" \ | |
-X POST \ | |
-d "{\"content\": \"🚀 Backend Server 배포가 성공적으로 완료되었습니다!\"}" \ | |
${{ secrets.DISCORD_WEBHOOK_URL }} | |
# 9. 실패 시 디스코드 알림 | |
- name: Send failure Discord notification | |
if: failure() | |
run: | | |
curl -H "Content-Type: application/json" \ | |
-X POST \ | |
-d "{\"content\": \"❌ Backend Server 배포가 실패했습니다. 문제가 발생했습니다.\"}" \ | |
${{ secrets.DISCORD_WEBHOOK_URL }} |