Skip to content

Merge pull request #134 from CampusTable/20260115_#131_장바구니_빈_페이지_개발 #108

Merge pull request #134 from CampusTable/20260115_#131_장바구니_빈_페이지_개발

Merge pull request #134 from CampusTable/20260115_#131_장바구니_빈_페이지_개발 #108

name: campus-table-cicd.yml
on:
push:
branches:
- main
- test
paths-ignore:
- 'CHANGELOG.md'
- 'package.json'
- 'package-lock.json'
env:
APP_BASE: campus-table
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
# 브랜치별 .env.production 생성
- name: Create .env.production file
run: |
if [ "${{ github.ref_name }}" = "main" ]; then
printf '%s\n' "${{ secrets.ENV_MAIN }}" > .env.production
elif [ "${{ github.ref_name }}" = "test" ]; then
printf '%s\n' "${{ secrets.ENV_TEST }}" > .env.production
else
printf '%s\n' "${{ secrets.ENV_MAIN }}" > .env.production
fi
- 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 }}-web:${{ 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}-web
IMAGE=${{ secrets.DOCKERHUB_USERNAME }}/${APP_BASE}-web:${BRANCH}
if [ "$BRANCH" = "main" ]; then
PORT=3003
elif [ "$BRANCH" = "test" ]; then
CONTAINER_NAME="${APP_BASE}-web-test"
PORT=3004
fi
echo "브랜치: $BRANCH"
echo "컨테이너 이름: $CONTAINER_NAME"
echo "포트: $PORT"
echo "도커 이미지 풀 : $IMAGE"
echo $PW | sudo -S docker pull $IMAGE
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 "브랜치별 .env.production 생성..."
if [ "$BRANCH" = "main" ]; then
echo "${{ secrets.ENV_MAIN }}" > .env.production
elif [ "$BRANCH" = "test" ]; then
echo "${{ secrets.ENV_TEST }}" > .env.production
else
echo "${{ secrets.ENV_MAIN }}" > .env.production
fi
echo "새로운 컨테이너 $CONTAINER_NAME 실행 중..."
echo $PW | sudo -S docker run -d -p ${PORT}:3000 --name $CONTAINER_NAME \
--restart unless-stopped \
--env-file .env.production \
${IMAGE}
# <none> 태그로 남은 이미지 정리
echo "불필요한 dangling(<none>) 이미지 정리..."
echo $PW | sudo -S docker image prune -af
echo "배포가 성공적으로 완료되었습니다."