Skip to content

Merge pull request #3 from TEAM-ROMROM/20250910_#2_기능추가_대시보드_홈화면_UI_개발 #4

Merge pull request #3 from TEAM-ROMROM/20250910_#2_기능추가_대시보드_홈화면_UI_개발

Merge pull request #3 from TEAM-ROMROM/20250910_#2_기능추가_대시보드_홈화면_UI_개발 #4

name: RomRom-ADMIN-CICD.yml
on:
push:
branches:
- main
- test
env:
APP_BASE: romrom-admin
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# 브랜치별 .env.local 생성
- name: Create .env.local file
run: |
if [ "${{ github.ref_name }}" == "main" ]; then
echo -e "${{ secrets.ENV_MAIN }}" > .env.local
elif [ "${{ github.ref_name }}" == "test" ]; then
echo -e "${{ secrets.ENV_TEST }}" > .env.local
fi
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20.15.0"
cache: "npm"
- name: Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Cache node_modules
id: node-modules-cache
uses: actions/cache@v4
with:
path: "**/node_modules"
key: ${{ runner.os }}-node-modules-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-modules-
- name: Install dependencies
if: steps.node-modules-cache.outputs.cache-hit != 'true'
run: npm ci
- name: Cache Next.js build
uses: actions/cache@v4
with:
path: .next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
restore-keys: |
${{ runner.os }}-nextjs-
- name: Build project
run: npm run build
- name: Docker setup
uses: docker/setup-buildx-action@v3
- name: Docker login
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_BASE }}:${{ github.ref_name }}
cache-from: type=gha
cache-to: type=gha,mode=max
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy
uses: appleboy/ssh-action@v1.0.3
env:
APP_BASE: ${{ env.APP_BASE }}
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
password: ${{ secrets.SERVER_PASSWORD }}
port: ${{ secrets.SERVER_PORT }}
envs: APP_BASE
script: |
set -e
echo "환경변수 설정.."
export PATH=$PATH:/usr/local/bin
export PW=${{ secrets.SERVER_PASSWORD }}
BRANCH=${{ github.ref_name }}
PORT=3000
CONTAINER_NAME="${APP_BASE}"
if [ "$BRANCH" == "main" ]; then
PORT=3005
elif [ "$BRANCH" == "test" ]; then
CONTAINER_NAME="${APP_BASE}-test"
PORT=3006
fi
echo "브랜치: $BRANCH"
echo "컨테이너 이름: $CONTAINER_NAME"
echo "포트: $PORT"
echo "도커 이미지 풀 : ${{ secrets.DOCKERHUB_USERNAME }}/${APP_BASE}:${BRANCH}"
echo $PW | sudo -S docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${APP_BASE}:${BRANCH}
echo "컨테이너 $CONTAINER_NAME 존재 여부 확인 중..."
if sudo docker ps -a --format '{{.Names}}' | grep -Eq "^${CONTAINER_NAME}\$"; then
echo "컨테이너 $CONTAINER_NAME 이(가) 존재합니다. 중지 및 삭제 중..."
echo $PW | sudo -S docker rm -f $CONTAINER_NAME
echo "컨테이너 $CONTAINER_NAME 이(가) 삭제되었습니다."
else
echo "존재하는 컨테이너 $CONTAINER_NAME 이(가) 없습니다."
fi
echo "새로운 컨테이너 $CONTAINER_NAME 실행 중..."
echo $PW | sudo -S docker run -d -p ${PORT}:3000 --name $CONTAINER_NAME \
${{ secrets.DOCKERHUB_USERNAME }}/${APP_BASE}:${BRANCH}
# <none> 태그로 남은 이미지 정리
echo "불필요한 dangling(<none>) 이미지 정리..."
echo $PW | sudo -S docker image prune -af
echo "배포가 성공적으로 완료되었습니다."