[FEAT] CI/CD 파일 수정 #4
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 Backend to EC2 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| - name: Build with Gradle | |
| run: ./gradlew clean build -x test | |
| - name: Create SSH key file | |
| run: | | |
| echo "${{ secrets.EC2_SSH_KEY }}" > private_key.pem | |
| chmod 600 private_key.pem | |
| - name: Deploy to EC2 | |
| run: | | |
| max_retries=3 | |
| retry_count=0 | |
| while [ $retry_count -lt $max_retries ]; do | |
| if scp -i private_key.pem -o StrictHostKeyChecking=no -o ConnectTimeout=10 \ | |
| build/libs/*.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/ubuntu/app.jar; then | |
| break | |
| fi | |
| retry_count=$((retry_count + 1)) | |
| echo "SCP failed. Retrying ($retry_count/$max_retries)..." | |
| sleep 5 | |
| done | |
| if [ $retry_count -eq $max_retries ]; then | |
| echo "SCP failed after $max_retries attempts" | |
| exit 1 | |
| fi | |
| ssh -i private_key.pem -o StrictHostKeyChecking=no -o ConnectTimeout=10 \ | |
| ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' | |
| sudo systemctl restart studylink | |
| sudo systemctl status studylink | |
| EOF | |
| - name: Clean up | |
| if: always() | |
| run: rm -f private_key.pem | |
| - name: Send deployment notification | |
| if: success() | |
| run: echo "Deployment successful!" |