name: Deploy blog-frontend run-name: ${{ gitea.actor }} is deploying Blog # 1. 언제 실행할까요? -> main 브랜치에 코드가 푸시될 때 on: push: branches: - main jobs: build-and-deploy: runs-on: blog-frontend steps: # 👇 [추가] Docker CLI 설치 단계 - name: Install Dependencies run: | apt-get update apt-get install -y git # Docker CLI 수동 설치 (호스트 버전 호환성 위해) curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-26.1.3.tgz -o docker.tgz tar xzvf docker.tgz mv docker/docker /usr/local/bin/ rm -r docker docker.tgz # 설치 확인 docker --version git --version - name: Clone Repository # 환경 변수로 필요한 값들을 주입받습니다. env: GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITEA_SERVER_URL: ${{ gitea.server_url }} GITEA_REPO: ${{ gitea.repository }} run: | # 현재 디렉토리가 비어있는지 확인 ls -al # 쉘 스크립트로 URL 가공 (http://, https:// 제거) # sed 명령어를 사용하여 프로토콜 부분을 안전하게 제거합니다. CLEAN_URL=$(echo "$GITEA_SERVER_URL" | sed 's~http://~~g' | sed 's~https://~~g') echo "Cloning from: $CLEAN_URL/$GITEA_REPO.git" # 가공된 URL과 토큰을 사용하여 클론 수행 git clone "https://user:${GITEA_TOKEN}@${CLEAN_URL}/${GITEA_REPO}.git" . # 클론 후 파일 목록 확인 (디버깅) echo "📂 File List after clone:" ls -al - name: Build Docker Image run: | echo "Building Docker image..." # 파일 존재 여부 재확인 if [ ! -f Dockerfile ]; then echo "❌ Dockerfile not found!" exit 1 fi docker build \ --build-arg NEXT_PUBLIC_API_URL=https://blogserver.wypark.me \ -t blog-frontend:latest . # 단계 3: 기존 컨테이너를 끄고 새 컨테이너를 실행합니다. - name: Deploy Container run: | echo "Deploying new container..." # 기존에 돌고 있는 'blog-frontend' 컨테이너가 있다면 중지하고 삭제합니다. # (|| true는 에러가 나도 무시하고 다음 줄로 넘어가라는 뜻입니다) docker stop blog-frontend || true docker rm blog-frontend || true # 새 이미지를 기반으로 컨테이너를 실행합니다. # -d: 백그라운드 실행 # -p 3005:3000: 호스트의 3005번 포트를 컨테이너의 3000번 포트에 연결 (NPM용) # --restart unless-stopped: 서버 재부팅 시 자동 실행 docker run -d \ --name blog-frontend \ --restart unless-stopped \ -p 3005:3000 \ -e NEXT_PUBLIC_API_URL=https://blogserver.wypark.me \ blog-frontend:latest # 단계 4: 빌드 과정에서 생긴 임시 이미지들을 청소합니다. (용량 관리) - name: Cleanup Old Images run: | docker image prune -f