Merge pull request #358 from 7-umc-GrowIT/fix/#357 #166
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: GrowIt CI/CD Pipeline | |
| on: | |
| push: | |
| branches: [ develop ] | |
| workflow_dispatch: | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. 코드 + 서브모듈 체크아웃 | |
| - name: Checkout With Submodules | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| submodules: 'recursive' | |
| token: ${{ secrets.ACCESS_TOKEN }} | |
| # 2. JDK 17 설정 (Gradle 캐시 포함) | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' | |
| cache: 'gradle' | |
| # 3. gradlew 실행 권한 부여 | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x gradlew | |
| # 4. prod 프로필로 JAR 빌드 | |
| - name: Build JAR with Gradle | |
| uses: gradle/gradle-build-action@v2 | |
| with: | |
| arguments: clean bootJar -x test --parallel --build-cache --no-daemon -Dorg.gradle.jvmargs="-Xmx2048m" | |
| # 5. 아티팩트 업로드 | |
| - name: Upload build artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: GrowItServer | |
| path: build/libs/*.jar | |
| retention-days: 1 | |
| if-no-files-found: error | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| environment: production | |
| steps: | |
| - name: Download build artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: GrowItServer | |
| path: build/libs/ | |
| - name: Deploy to EC2 | |
| env: | |
| EC2_SSH_KEY: ${{ secrets.EC2_SSH_KEY }} | |
| EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
| EC2_HOST: ${{ secrets.EC2_HOST }} | |
| run: | | |
| # SSH 키 설정 | |
| mkdir -p ~/.ssh | |
| echo "$EC2_SSH_KEY" > ~/.ssh/deploy_key | |
| chmod 600 ~/.ssh/deploy_key | |
| echo -e "Host ec2\n\tHostName ${{ secrets.EC2_HOST }}\n\tUser ${{ secrets.EC2_USERNAME }}\n\tIdentityFile ~/.ssh/deploy_key\n\tStrictHostKeyChecking no" > ~/.ssh/config | |
| # JAR 파일 찾기 | |
| jar_file=$(find build/libs -name '*.jar' ! -name '*plain.jar' | head -n 1) | |
| # 배포 스크립트 실행 | |
| ssh ec2 " | |
| # 기존 JAR 파일 백업 | |
| if [ -f ~/GrowItServer.jar ]; then | |
| mv ~/GrowItServer.jar ~/GrowItServer.jar.backup | |
| fi | |
| # 실행 중인 프로세스 종료 | |
| if pgrep java; then | |
| pgrep java | xargs -r kill -15 | |
| for i in {1..12}; do | |
| if ! pgrep java > /dev/null; then | |
| break | |
| fi | |
| sleep 5 | |
| done | |
| pgrep java | xargs -r kill -9 2>/dev/null || true | |
| fi | |
| " | |
| # 새로운 JAR 파일 전송 | |
| scp -F ~/.ssh/config "$jar_file" ec2:~/GrowItServer.jar | |
| # 애플리케이션 실행 | |
| ssh ec2 " | |
| nohup java -Dspring.profiles.active=prod -jar -Xmx1024m ~/GrowItServer.jar > app.log 2>&1 & | |
| # 실행 확인 | |
| sleep 30 | |
| if ! pgrep java > /dev/null; then | |
| echo '애플리케이션 실행 실패' | |
| if [ -f ~/GrowItServer.jar.backup ]; then | |
| echo '백업에서 복구 시도' | |
| mv ~/GrowItServer.jar.backup ~/GrowItServer.jar | |
| nohup java -jar -Xmx1024m ~/GrowItServer.jar > app.log 2>&1 & | |
| fi | |
| exit 1 | |
| fi | |
| echo '애플리케이션 정상 실행 확인' | |
| " | |
| - name: Cleanup | |
| if: always() | |
| run: rm -rf ~/.ssh |