diff --git a/.github/workflows/spring-cd.yml b/.github/workflows/spring-cd.yml index 0ccfb160..2484d7dd 100644 --- a/.github/workflows/spring-cd.yml +++ b/.github/workflows/spring-cd.yml @@ -1,4 +1,4 @@ -name: Deploy on PR Merged to develop +name: CD on PR Merged to develop on: pull_request: @@ -7,71 +7,53 @@ on: jobs: build: + if: github.event.pull_request.merged == true runs-on: ubuntu-latest steps: - - name: Checkout repository - uses: actions/checkout@v3 + - name: Checkout Repository + uses: actions/checkout@v3 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - - name: Cache Gradle packages - uses: actions/cache@v3 - with: - path: | - ~/.gradle/caches - ~/.gradle/wrapper - key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - ${{ runner.os }}-gradle - - - name: Grant permission to gradlew - run: chmod +x gradlew + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' - - name: Build with Gradle - run: ./gradlew build + - name: Grant permission to gradlew + run: chmod +x gradlew - - name: Run tests - run: ./gradlew test + - name: Build with Gradle + run: ./gradlew build - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - token: ${{ secrets.CODECOV_TOKEN }} + - name: Upload JAR as artifact + uses: actions/upload-artifact@v3 + with: + name: app-jar + path: build/libs/*.jar - deploy-on-merge: - if: github.event.pull_request.merged == true # ✔ merge된 PR일 때만 실행 + deploy: + needs: build runs-on: ubuntu-latest steps: - - name: Checkout source code - uses: actions/checkout@v4 - - - name: Set up JDK 17 - uses: actions/setup-java@v4 + - name: Download JAR artifact + uses: actions/download-artifact@v3 with: - distribution: 'temurin' - java-version: 17 - - - name: Build with Gradle - run: ./gradlew clean build -x test + name: app-jar - - name: Set up SSH + - name: Set up SSH Agent uses: webfactory/ssh-agent@v0.9.0 with: ssh-private-key: ${{ secrets.EC2_SSH_KEY }} - name: Copy JAR to EC2 run: | - scp -i ${{secrets.EC2_SSH_KEY}} -o StrictHostKeyChecking=no build/libs/*.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/${{ secrets.EC2_USER }}/app/app.jar + scp -o StrictHostKeyChecking=no app-jar/*.jar ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }}:/home/ubuntu/app/app.jar - name: Restart App with Docker Compose run: | - ssh -i ${{secrets.EC2_SSH_KEY}} -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' - cd /home/${{ secrets.EC2_USER }}/app + ssh -o StrictHostKeyChecking=no ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' + cd /home/ubuntu/app docker compose up -d --force-recreate EOF