deploy: CI/CD workflow 수정 #21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Workflow for Develop | |
on: | |
# PR 작성 및 업데이트 시 CI 실행 | |
pull_request: | |
branches: [ "develop" ] | |
# develop 브랜치로의 푸시 시 CD 실행 | |
push: | |
branches: [ "develop" ] | |
jobs: | |
# Gradle 빌드만 수행하는 CI 작업 (PR에서만 실행) | |
ci-gradle-build: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'pull_request' | |
permissions: | |
contents: read | |
steps: | |
# 소스 코드 체크아웃 | |
- uses: actions/checkout@v4 | |
# JDK 설정 | |
- 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: | |
gradle-version: '7.3' | |
# Gradle Wrapper 실행 권한 추가 | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
# Gradle 빌드 | |
- name: Build with Gradle Wrapper | |
run: ./gradlew build | |
# CD 작업 - Gradle 빌드, Docker 이미지 빌드 및 배포 (develop 브랜치로의 push 시에만 실행) | |
build-push-deploy: | |
runs-on: ubuntu-latest | |
if: github.event_name == 'push' | |
permissions: | |
contents: read | |
steps: | |
# 소스 코드 체크아웃 | |
- uses: actions/checkout@v4 | |
# JDK 설정 | |
- 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: | |
gradle-version: '7.3' | |
# Gradle 빌드 | |
- name: Build with Gradle Wrapper | |
run: ./gradlew build | |
# 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 }} | |
# Docker 이미지 푸시 | |
- name: Push Docker image to Docker Hub | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
# SSH 비밀 키 설정 | |
- name: Install SSH Key | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
# 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 파일 위치로 이동 | |
# 기존 컨테이너 중지 | |
docker-compose down | |
# 사용 중인 이미지 제거 (캐시 무효화를 위해) | |
docker images -q ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | xargs -r sudo docker rmi | |
# 최신 이미지 pull | |
docker-compose pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_IMAGE_NAME }} | |
# 모든 서비스 다시 실행 | |
docker-compose up -d | |
EOF |