Update README.md #41
This file contains hidden or 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: Deploy to EC2 | |
| on: | |
| push: | |
| branches: [ main ] # main 브랜치에 푸시할 때 자동 배포 | |
| pull_request: | |
| branches: [ main ] | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 21 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: Build with Gradle | |
| run: ./gradlew build -x test | |
| - name: Upload JAR to EC2 | |
| uses: appleboy/scp-action@v0.1.4 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: "build/libs/memory-0.0.1-SNAPSHOT.jar" | |
| target: "~/app/" | |
| - name: Deploy and Start Application | |
| uses: appleboy/ssh-action@v0.1.5 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| # 기존 애플리케이션 중지 | |
| sudo pkill -f "memory-0.0.1-SNAPSHOT.jar" || true | |
| # 환경변수 로드 | |
| cd ~/app | |
| source ~/.env | |
| # 새 애플리케이션 시작 | |
| nohup java -Xmx350m -Xms256m -XX:+UseG1GC \ | |
| -Dspring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME} \ | |
| -Dspring.datasource.username=${DB_USERNAME} \ | |
| -Dspring.datasource.password=${DB_PASSWORD} \ | |
| -Dcloud.aws.credentials.access-key=${AWS_ACCESS_KEY_ID} \ | |
| -Dcloud.aws.credentials.secret-key=${AWS_SECRET_ACCESS_KEY} \ | |
| -Dcloud.aws.s3.bucket=${S3_BUCKET_NAME} \ | |
| -jar build/libs/memory-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod > ~/logs/app.log 2>&1 & | |
| # 애플리케이션 시작 확인 | |
| sleep 10 | |
| if pgrep -f "memory-0.0.1-SNAPSHOT.jar" > /dev/null; then | |
| echo "Application started successfully" | |
| else | |
| echo "Application failed to start" | |
| exit 1 | |
| fi |