HOTFIX: 레디스 환경변수 수정 #45
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: 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 | |
if: github.event_name == 'push' | |
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: | | |
docker pull ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi:latest | |
# 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 --platform linux/arm64 --name $GREEN_NAME -d -p $GREEN_PORT:$GREEN_PORT \ | |
--env-file ./gachtaxi-dev.env --network gachtaxi -e SPRING_PROFILES_ACTIVE=dev -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 --platform linux/arm64 --name $BLUE_NAME -d -p $BLUE_PORT:$BLUE_PORT \ | |
--env-file ./gachtaxi-dev.env --network gachtaxi -e SPRING_PROFILES_ACTIVE=dev -e TZ=Asia/Seoul ${{ secrets.DEV_DOCKER_USER_NAME }}/gachtaxi | |
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 | |
# 사용하지 않는 이미지 정리 | |
echo "** 사용하지 않는 Docker 이미지 정리" | |
sudo docker image prune -f |