From d899e34f1f8254737e5c74ae70a4712d8f5a4fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=A5=EC=98=81=EC=9E=AC?= Date: Fri, 22 Nov 2024 19:26:50 +0900 Subject: [PATCH] Update main.yml --- .github/workflows/main.yml | 145 +++++++++++++++++++++---------------- 1 file changed, 83 insertions(+), 62 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 033bfa2b..9782b0b3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,73 +1,94 @@ -name: Backend CI/CD using GitHub Actions & Docker +name: Full Stack CI/CD using GitHub Actions & Docker # 워크플로우 실행 조건 설정 on: - push: - branches: [ "production" ] - pull_request: - branches: [ "production" ] + push: + branches: [ "production" ] + pull_request: + branches: [ "production" ] jobs: - build: - runs-on: ubuntu-latest - permissions: - contents: read + build: + runs-on: ubuntu-latest + permissions: + contents: read + steps: + # 코드 체크아웃 + - uses: actions/checkout@v4 - steps: - # 코드 체크아웃 - - uses: actions/checkout@v4 + # 백엔드 빌드 환경 설정 (JDK 21) + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'temurin' - # 백엔드 빌드 환경 설정 (JDK 21) - - name: Set up JDK 21 - uses: actions/setup-java@v4 - with: - java-version: '21' - distribution: 'temurin' + # 백엔드 설정 파일 생성 + - name: Create backend configuration files + run: | + mkdir -p ./backend/src/main/resources + cd ./backend/src/main/resources + echo "${{ secrets.APPLICATION_YML }}" > application.yml + echo "${{ secrets.APPLICATION_AWS_YML }}" > application-aws.yml - # 백엔드 설정 파일 생성 - - name: Create backend configuration files - run: | - mkdir -p ./backend/src/main/resources - cd ./backend/src/main/resources - echo "${{ secrets.APPLICATION_YML }}" > application.yml - echo "${{ secrets.APPLICATION_AWS_YML }}" > application-aws.yml + # Gradle 설정 + - name: Setup Gradle + uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 + + # 백엔드 빌드 실행 + - name: Build Backend + run: | + cd backend + chmod +x ./gradlew + ./gradlew build -x test - # Gradle 설정 - - name: Setup Gradle - uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 - - # 백엔드 빌드 실행 - - name: Build Backend - run: | - cd backend - chmod +x ./gradlew - ./gradlew build -x test + # 프론트엔드 빌드 환경 설정 + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '18' - # 도커 이미지 빌드 및 푸시 - - name: Docker build & push - run: | - docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} - cd backend - docker build -t ${{ secrets.DOCKER_USERNAME }}/backend:latest . - docker push ${{ secrets.DOCKER_USERNAME }}/backend:latest + # 프론트엔드 빌드 실행 + - name: Build Frontend + run: | + cd frontend + npm install + npm run build - # EC2 인스턴스에 배포 - - name: Deploy to EC2 - uses: appleboy/ssh-action@master - with: - host: ${{ secrets.EC2_HOST }} - username: ubuntu - key: ${{ secrets.EC2_PRIVATE_KEY }} - script: | - # 최신 도커 이미지 받아오기 - sudo docker pull ${{ secrets.DOCKER_USERNAME }}/backend:latest - - # 실행 중인 컨테이너 중지 및 제거 - sudo docker stop $(sudo docker ps -a -q) || true - sudo docker rm $(sudo docker ps -a -q) || true - - # 새 컨테이너 실행 - sudo docker run -d -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/backend:latest - - # 사용하지 않는 이미지 정리 - sudo docker image prune -a -f + # 백엔드 도커 이미지 빌드 및 푸시 + - name: Backend Docker build & push + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + cd backend + docker build -t ${{ secrets.DOCKER_USERNAME }}/backend:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/backend:latest + + # 프론트엔드 도커 이미지 빌드 및 푸시 + - name: Frontend Docker build & push + run: | + cd frontend + docker build -t ${{ secrets.DOCKER_USERNAME }}/frontend:latest . + docker push ${{ secrets.DOCKER_USERNAME }}/frontend:latest + + # EC2 인스턴스에 배포 + - name: Deploy to EC2 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.EC2_HOST }} + username: ubuntu + key: ${{ secrets.EC2_PRIVATE_KEY }} + script: | + # 최신 도커 이미지 받아오기 + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/backend:latest + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/frontend:latest + + # 실행 중인 컨테이너 중지 및 제거 + sudo docker stop $(sudo docker ps -a -q) || true + sudo docker rm $(sudo docker ps -a -q) || true + + # 새 컨테이너 실행 + sudo docker run -d -p 8080:8080 --name backend ${{ secrets.DOCKER_USERNAME }}/backend:latest + sudo docker run -d -p 80:80 --name frontend ${{ secrets.DOCKER_USERNAME }}/frontend:latest + + # 사용하지 않는 이미지 정리 + sudo docker image prune -a -f