Files
auto-stock/Jenkinsfile
shuim 19d25e5528 perf: NAS 도커 빌드 시간 단축 — CPU 전용 torch + 빌드 캐시 마운트
DS1517+(Atom C2538)에서 빌드가 30분 이상 걸리던 원인 두 가지를 제거.

1. PyTorch CPU 전용 휠 사용 (backend)
   PyPI 기본 torch 휠은 nvidia-cublas/cudnn 등 CUDA 패키지를 수 GB
   함께 내려받는데 NAS에는 GPU가 없어 전부 불필요했다.
   download.pytorch.org/whl/cpu 에서 먼저 설치하면 requirements.txt 의
   torch 줄은 충족되어 건너뛴다. ml_ensemble 은 이미 지연 임포트라 동작 동일.

2. pip/npm 캐시를 빌드 간 재사용 (backend, mcp-server, frontend)
   --no-cache-dir 제거하고 RUN --mount=type=cache 로 전환.
   requirements.txt 가 바뀌어도 받아둔 휠은 재다운로드하지 않는다.
   Jenkinsfile 에 DOCKER_BUILDKIT/COMPOSE_DOCKER_CLI_BUILD 추가.

부수: frontend Dockerfile 의 FromAsCasing 경고 정리 (as build → AS build).
세 Dockerfile 모두 docker build --check 통과.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-19 23:57:21 +09:00

127 lines
3.8 KiB
Groovy

pipeline {
agent any
environment {
DEPLOY_DIR = '/volume1/docker/autostock'
ENV_FILE = "${DEPLOY_DIR}/.env"
// Dockerfile 의 pip/npm 캐시 마운트(RUN --mount=type=cache)를 쓰려면 BuildKit 필요
DOCKER_BUILDKIT = '1'
COMPOSE_DOCKER_CLI_BUILD = '1'
}
options {
timeout(time: 30, unit: 'MINUTES')
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Validate') {
steps {
sh '''
echo "=== Syntax Check ==="
python3 -c "
import ast, pathlib
errors = []
for f in pathlib.Path('backend/app').rglob('*.py'):
try:
ast.parse(f.read_text())
except SyntaxError as e:
errors.append(f'{f}: {e}')
for f in pathlib.Path('mcp-server').glob('*.py'):
try:
ast.parse(f.read_text())
except SyntaxError as e:
errors.append(f'{f}: {e}')
if errors:
print('\\n'.join(errors))
exit(1)
print('All files OK')
"
'''
}
}
stage('Sync & Build') {
steps {
sh """
echo "=== Syncing source to deploy directory ==="
mkdir -p ${DEPLOY_DIR}
# .env 존재 확인
if [ ! -f "${ENV_FILE}" ]; then
echo "WARNING: ${ENV_FILE} not found. Creating template..."
cp deploy/setup-nas.sh ${DEPLOY_DIR}/ 2>/dev/null || true
fi
# 소스 동기화 (backend, mcp-server, frontend, docker-compose.yml, docker/)
cp docker-compose.yml ${DEPLOY_DIR}/
cp -r docker/ ${DEPLOY_DIR}/ 2>/dev/null || true
rsync -a --delete backend/ ${DEPLOY_DIR}/backend/ || cp -r backend/ ${DEPLOY_DIR}/backend/
rsync -a --delete mcp-server/ ${DEPLOY_DIR}/mcp-server/ || cp -r mcp-server/ ${DEPLOY_DIR}/mcp-server/
rsync -a --delete frontend/ ${DEPLOY_DIR}/frontend/ || cp -r frontend/ ${DEPLOY_DIR}/frontend/
echo "=== Building Docker Images ==="
cd ${DEPLOY_DIR}
docker-compose build backend mcp-server frontend
"""
}
}
stage('Deploy') {
steps {
sh """
cd ${DEPLOY_DIR}
echo "=== Rolling Deploy (앱만 재시작, DB/Redis 유지) ==="
docker-compose stop backend mcp-server frontend 2>/dev/null || true
docker-compose up -d
echo "=== Waiting for health check ==="
sleep 15
for i in 1 2 3 4 5; do
if curl -sf http://localhost:8000/health > /dev/null 2>&1; then
echo "Backend is healthy!"
break
fi
echo "Waiting for backend... (\$i/5)"
sleep 5
done
docker-compose ps
"""
}
}
stage('DB Migration') {
steps {
sh """
cd ${DEPLOY_DIR}
docker-compose exec -T backend alembic upgrade head || echo "WARNING: Migration failed!"
"""
}
}
}
post {
success {
echo 'Deployment successful!'
}
failure {
echo 'Deployment failed!'
sh """
cd ${DEPLOY_DIR} 2>/dev/null && docker-compose logs --tail=30 backend mcp-server 2>/dev/null || true
"""
}
always {
cleanWs()
}
}
}