Skip to content

deploy: prod-deploy-container 복구 (#51) #58

deploy: prod-deploy-container 복구 (#51)

deploy: prod-deploy-container 복구 (#51) #58

name: CI/CD Workflow for Develop
on:
# PR 작성 및 업데이트 시 CI 실행
pull_request:
branches: [ "develop" ]
# develop 브랜치로의 푸시 시 CD 실행
push:
branches: [ "develop" ]
jobs:
# 공통 설정 및 Gradle 빌드용 Setup job
gradle-setup:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
# 소스 코드 체크아웃
- uses: actions/checkout@v4
# JDK 및 Gradle 설정
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4
with:
gradle-version: '7.3'
# Gradle Wrapper 실행 권한 추가
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
# 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 }} .
# Docker Hub에 로그인 및 이미지 푸시
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Push Docker image to Docker Hub
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }}
# EC2 서버에 Docker Compose로 배포
- name: Deploy to server
uses: appleboy/[email protected]
id: deploy
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.EC2_KEY }}
script: |
cd /home/${{ secrets.EC2_USER }} # docker-compose.yml 파일 위치로 이동
# Spring 서버 컨테이너 중지
sudo docker-compose stop SpringServer
# Spring 서버 컨테이너 삭제
sudo docker-compose rm -f SpringServer
# 사용 중인 이미지 강제 삭제 (캐시 무효화를 위해)
sudo docker images -q ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | xargs -r sudo docker rmi -f
# 최신 이미지 pull
sudo docker-compose pull SpringServer
# Spring 서버만 다시 실행
sudo docker-compose up -d SpringServer