diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index 356f32c..0000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: CI - -on: - push: - branches: [ main, develop ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - - name: Set up JDK 21 - uses: actions/setup-java@v4 - with: - distribution: 'temurin' - java-version: '21' - - - name: Create application.yml - run: | - mkdir -p src/main/resources - echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml - echo "${{ secrets.APPLICATION_YML_DEV }}" > ./src/main/resources/application-dev.yml - echo "${{ secrets.APPLICATION_YML_PROD }}" > ./src/main/resources/application-prod.yml - echo "${{ secrets.APPLICATION_YML_SECRET }}" > ./src/main/resources/application-secret.yml - - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew - - - name: Build with Gradle - run: ./gradlew build - - - name: Run Tests with Coverage - run: ./gradlew jacocoTestReport - - - name: Upload Coverage Report - uses: actions/upload-artifact@v4 - with: - name: jacoco-html-report - path: build/reports/jacoco/test/html - - - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v5 - with: - token: ${{ secrets.CODECOV_TOKEN }} - diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 31e356c..7246fa8 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,28 +1,31 @@ -name: Deploy with Docker Hub +name: CI/CD Pipeline on: - pull_request: + push: branches: [ develop, main ] - types: [opened, synchronize, reopened, ready_for_review] env: DOCKER_IMAGE: leeeunda/blockcloud-server jobs: - test: + build-and-test: runs-on: ubuntu-latest - continue-on-error: true + steps: - - uses: actions/checkout@v4 + # 코드 체크아웃 + - name: Checkout repository + uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.head.sha }} + # JDK 21 설정 - name: Set up JDK 21 uses: actions/setup-java@v4 with: + distribution: 'temurin' java-version: '21' - distribution: temurin + # Gradle 캐시 설정 - name: Cache Gradle uses: actions/cache@v3 with: @@ -32,26 +35,35 @@ jobs: key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} restore-keys: ${{ runner.os }}-gradle- - - name: Run tests (non-blocking) - run: ./gradlew test || true + # application.yml 생성 + - name: Create application.yml files + run: | + mkdir -p src/main/resources + echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml + echo "${{ secrets.APPLICATION_YML_DEV }}" > ./src/main/resources/application-dev.yml + echo "${{ secrets.APPLICATION_YML_PROD }}" > ./src/main/resources/application-prod.yml + echo "${{ secrets.APPLICATION_YML_SECRET }}" > ./src/main/resources/application-secret.yml - build-and-push: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} + # gradlew 실행 권한 부여 + - name: Grant execute permission for gradlew + run: chmod +x ./gradlew + + # Gradle 빌드 실행 + - name: Build with Gradle + run: ./gradlew clean build -x test + # Docker Hub 로그인 - name: Login to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} + # Docker Buildx 설정 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 + # Docker 이미지 빌드 및 푸시 - name: Build and push Docker image uses: docker/build-push-action@v5 with: @@ -60,13 +72,13 @@ jobs: tags: | ${{ env.DOCKER_IMAGE }}:latest ${{ env.DOCKER_IMAGE }}:${{ github.sha }} - ${{ env.DOCKER_IMAGE }}:${{ github.ref_name }} + ${{ env.DOCKER_IMAGE }}:${{ github.event.pull_request.head.sha }} cache-from: type=gha cache-to: type=gha,mode=max - # platforms: linux/amd64 # Graviton이면 linux/arm64 + platforms: linux/amd64 - deploy-to-ec2: - needs: build-and-push + deploy: + needs: build-and-test runs-on: ubuntu-latest steps: - name: Deploy to EC2 (pull & replace container) @@ -80,22 +92,29 @@ jobs: IMAGE="${{ env.DOCKER_IMAGE }}:${{ github.event.pull_request.head.sha }}" docker pull "$IMAGE" + # 기존 컨테이너 정리 docker stop blockcloud-app || true docker rm blockcloud-app || true + # 새 컨테이너 실행 docker run -d \ --name blockcloud-app \ --restart unless-stopped \ --env-file /srv/app/.env \ + -e SPRING_PROFILES_ACTIVE=prod \ -p 127.0.0.1:8080:8080 \ "$IMAGE" + # 헬스체크 for i in {1..30}; do if curl -fsS http://127.0.0.1:8080/actuator/health >/dev/null 2>&1; then - echo "UP" + echo "✅ Application is UP and healthy!" exit 0 fi + echo "⏳ Waiting for application to start... ($i/30)" sleep 3 done + + echo "❌ Application failed to start properly" docker logs --tail=200 blockcloud-app || true - exit 1 + exit 1 \ No newline at end of file