From c2e417dc28e0e9db5649a4d5cbf064c4fe00354d Mon Sep 17 00:00:00 2001 From: Jin Hyuk Date: Mon, 4 Nov 2024 22:19:19 +0900 Subject: [PATCH] =?UTF-8?q?deploy:=20CI/CD=20workflow=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/prod-deploy-container.yml | 75 +++++++++++---------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/.github/workflows/prod-deploy-container.yml b/.github/workflows/prod-deploy-container.yml index b6a701d..718bc74 100644 --- a/.github/workflows/prod-deploy-container.yml +++ b/.github/workflows/prod-deploy-container.yml @@ -1,13 +1,17 @@ -name: Deploy to EC2 on develop +name: CI/CD Workflow for Develop on: - push: - branches: [ "develop" ] + # PR 작성 및 업데이트 시 CI 실행 pull_request: branches: [ "develop" ] + # develop 브랜치로의 푸시 시 CD 실행 + push: + branches: [ "develop" ] + jobs: - build-and-push-image: + # 공통 설정 및 Gradle 빌드용 Setup job + gradle-setup: runs-on: ubuntu-latest permissions: contents: read @@ -16,14 +20,13 @@ jobs: # 소스 코드 체크아웃 - uses: actions/checkout@v4 - # JDK 설정 + # JDK 및 Gradle 설정 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' - # Gradle 설정 - name: Setup Gradle uses: gradle/actions/setup-gradle@v4 with: @@ -35,42 +38,63 @@ jobs: # Gradle 빌드 - name: Build with Gradle Wrapper + id: build run: ./gradlew build + # 빌드 결과물 업로드 + - name: Upload build artifacts + uses: actions/upload-artifact@v3 + with: + name: application-jar + path: build/libs/*.jar + + + # CD 작업 - Docker 빌드, 이미지 푸시 및 배포 (develop 브랜치에 머지 시) + build-push-deploy: + runs-on: ubuntu-latest + needs: gradle-setup + if: github.event_name == 'push' + permissions: + contents: read + + steps: + # 소스 코드 체크아웃 + - uses: actions/checkout@v4 + + # 아티팩트 다운로드 + - name: Download build artifacts + uses: actions/download-artifact@v3 + with: + name: application-jar + path: build/libs + # Docker 이미지 빌드 - name: Build Docker image - run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} . + run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} . - # Docker Hub에 로그인 + # Docker Hub에 로그인 및 이미지 푸시 - name: Log in to Docker Hub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_PASSWORD }} - # Docker 이미지 푸시 - name: Push Docker image to Docker Hub run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} - deploy-to-ec2: - needs: build-and-push-image - runs-on: ubuntu-latest - - steps: # SSH 비밀 키 설정 - name: Install SSH Key uses: webfactory/ssh-agent@v0.5.3 with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - # EC2에 접속하여 Docker Compose 실행 + # EC2 서버에 Docker Compose로 배포 - name: Deploy on EC2 env: EC2_HOST: ${{ secrets.EC2_HOST }} EC2_USER: ${{ secrets.EC2_USER }} run: | ssh -o StrictHostKeyChecking=no $EC2_USER@$EC2_HOST << EOF - cd /home/ubuntu # docker-compose.yml 파일 위치로 이동 # 기존 컨테이너 중지 @@ -85,22 +109,3 @@ jobs: # 모든 서비스 다시 실행 docker-compose up -d EOF - - dependency-submission: - - runs-on: ubuntu-latest - permissions: - contents: write - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v4 - with: - java-version: '17' - distribution: 'temurin' - - # Generates and submits a dependency graph, enabling Dependabot Alerts for all project dependencies. - # See: https://github.com/gradle/actions/blob/main/dependency-submission/README.md - - name: Generate and submit dependency graph - uses: gradle/actions/dependency-submission@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0