Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore #10 개발서버 cicd 구축 #10

Merged
merged 16 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: GachTaxi-BE dev CI/CD

on:
push:
branches: [ "dev" ] # develop 브랜치에 push 시 트리거
pull_request:
branches: [ "dev" ] # develop 브랜치에 대한 PR 시 트리거
types: [opened, synchronize, reopened]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read

steps:
- uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'

# Gradle 캐시 설정
- name: Gradle Caching
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: Build with Gradle Wrapper
run: ./gradlew build -x test

# 빌드된 JAR 파일 확인
- name: List JAR files
run: ls build/libs

# Docker 이미지 빌드 및 푸시
- name: Docker build & push
run: |
docker login -u ${{ secrets.DEV_DOCKER_USER_NAME }} -p ${{ secrets.DEV_DOCKER_USER_TOKEN }}
docker buildx create --use
docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile-dev -t ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi:latest --push .

deploy:
runs-on: ubuntu-latest
needs: build

steps:
# SSH를 사용하여 원격 서버에 배포
- name: Deploy to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEV_SSH_SECRET_HOST }}
username: ${{ secrets.DEV_SSH_SECRET_USER }}
port: 30922
key: ${{ secrets.DEV_SSH_SECRET_PRIVATE_KEY }}
script: |
# Blue-Green Deployment 포트 및 컨테이너 이름 설정
BLUE_PORT=8080
GREEN_PORT=8081
BLUE_NAME="blue"
GREEN_NAME="green"

# 현재 실행 중인 컨테이너 확인
IS_BLUE_ON=$(sudo docker ps --filter "name=$BLUE_NAME" --filter "status=running" -q)

if [ -n "$IS_BLUE_ON" ]; then
echo "** ${GREEN_PORT} 포트에서 GREEN 컨테이너 실행"
sudo docker run --name $GREEN_NAME -d -p $GREEN_PORT:$GREEN_PORT \
--env-file ./gachtaxi-dev.env -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi
BEFORE_NAME=$BLUE_NAME
AFTER_NAME=$GREEN_NAME
BEFORE_PORT=$BLUE_PORT
AFTER_PORT=$GREEN_PORT
else
echo "** ${BLUE_PORT} 포트에서 BLUE 컨테이너 실행"
sudo docker run --name $BLUE_NAME -d -p $BLUE_PORT:$BLUE_PORT \
--env-file ./gachtaxi-dev.env -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi
Comment on lines +84 to +85
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

도커 컨테이너를 가동할 때 도커 네트워크 설정을 안해도 괜찮나요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저희가 현재는 컨테이너 끼리 통신을 요구하지않고 gachtaxi-dev.env를 통해 환경 변수를 주입하고있는 형태라서 별도의 추가적인 네트워크 설정이 필요가 없는걸로 판단하였습니다
도커가 기본적으로 기본 네트워크를 사용하여 컨테이너 간 통신을 지원하는 것으로 알고있어, 별도의 네트워크 설정 없이 배포에는 문제가 없을거같습니다

BEFORE_NAME=$GREEN_NAME
AFTER_NAME=$BLUE_NAME
BEFORE_PORT=$GREEN_PORT
AFTER_PORT=$BLUE_PORT
fi

echo "** ${AFTER_NAME} 컨테이너 실행 완료 (포트: ${AFTER_PORT})"

# 이전 컨테이너 중지 및 삭제
echo "** 이전 컨테이너(${BEFORE_NAME}) 종료 및 삭제"
sudo docker stop $BEFORE_NAME || true
sudo docker rm $BEFORE_NAME || true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFTER_NAME과 BEFORE_NAME으로 새로운 컨테이너와 이전에 사용한 컨테이너를 분리한 게 정말 좋네요!
새로운 컨테이너 관련 쉘 스크립트를 수정할 때, 한 눈에 수정해야 하는 부분을 찾을 수 있어 좋네요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 아직 방법은 모릅니다만, 더이상 사용하지 않는 도커 이미지를 삭제하도록 해도 좋을 거 같아요.
CI/CD 구축 후 개발을 하면 항상 해당 서버에 도커 이미지가 잔뜩 쌓여있어 직접 일일이 지우기 번거롭더라고요

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docker image prune-a -f 로 지울 수 있어요!!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

배포 후 불필요한 이미지까지 정리하는 로직도 추가해서, 디스크 공간을 추가적으로 확보할 수 있도록 설정해놓겠습니다

7 changes: 7 additions & 0 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM eclipse-temurin:17-jre-focal

ARG JAR_FILE=build/libs/*.jar

COPY ${JAR_FILE} docker-springboot.jar

ENTRYPOINT ["java", "-jar", "-Dspring.profiles.active=dev", "/docker-springboot.jar"]
Empty file modified gradlew
100644 → 100755
Empty file.
Loading