diff --git a/Jenkinsfile b/Jenkinsfile index e04f073..1d263c2 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,91 +1,43 @@ pipeline { agent any - environment { - APP_DIR = '/opt/guardia/app' - VENV = '/opt/guardia/venv' - SERVICE = 'guardia' - GITEA_URL = 'http://localhost:3000/zio/guardia-itsm.git' + APP = '/opt/guardia/app' + VENV = '/opt/guardia/venv' + NOTIFY = "${ITSM_BASE_URL}/api/messenger/webhook" } - options { buildDiscarder(logRotator(numToKeepStr: '5')) timeout(time: 15, unit: 'MINUTES') + timestamps() } - stages { - stage('Checkout') { + stage('Checkout') { steps { checkout scm } } + stage('Install') { + steps { sh "${VENV}/bin/pip install -r requirements.txt -q" } + } + stage('Test') { + when { expression { fileExists('tests/') } } steps { - echo "체크아웃: ${env.GIT_BRANCH ?: 'main'}" - checkout scm + sh "${VENV}/bin/pytest tests/ -q --tb=short --junitxml=test-results.xml || true" + junit allowEmptyResults: true, testResults: 'test-results.xml' } } - - stage('Python Lint') { - steps { - sh ''' - echo "=== Python 문법 검사 ===" - python3 -m py_compile main.py models.py database.py - find routers/ -name "*.py" -exec python3 -m py_compile {} \\; - find core/ -name "*.py" -exec python3 -m py_compile {} \\; - echo "문법 검사 통과" - ''' - } - } - - stage('Install Dependencies') { - steps { - sh '${VENV}/bin/pip install -r requirements.txt -q && echo "의존성 OK"' - } - } - - stage('Health Check') { - steps { - sh ''' - if systemctl is-active ${SERVICE} >/dev/null 2>&1; then - HTTP=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8001/docs) - echo "현재 서비스 HTTP: $HTTP" - else - echo "서비스 미실행" - fi - ''' - } - } - stage('Deploy') { when { branch 'main' } steps { - sh ''' - echo "=== GUARDiA ITSM 배포 ===" - # 백업 - BACKUP=/opt/guardia/backups/$(date +%Y%m%d_%H%M%S) - mkdir -p $BACKUP - cp -r ${APP_DIR}/*.py ${APP_DIR}/routers ${APP_DIR}/core $BACKUP/ 2>/dev/null || true - - # 파일 복사 - rsync -av --exclude="__pycache__" --exclude="test_*.py" \\ - --exclude="*.db" --exclude=".git" \\ - ./ ${APP_DIR}/ - - # 패키지 업데이트 - ${VENV}/bin/pip install -r requirements.txt -q - - # 서비스 재시작 - systemctl restart ${SERVICE} - sleep 5 - - # 헬스체크 - HTTP=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8001/docs) - echo "배포 후 HTTP: $HTTP" - [ "$HTTP" = "200" ] && echo "배포 성공" || (echo "배포 실패"; exit 1) - ''' + sh """ + rsync -a --exclude=__pycache__ --exclude=.git \ + --exclude=rpa_rules.json --exclude='*.pyc' \ + . ${APP}/ + systemctl restart guardia + sleep 4 + systemctl is-active guardia || exit 1 + """ } } } - post { - success { echo "✅ GUARDiA 배포 성공: ${currentBuild.displayName}" } - failure { echo "❌ GUARDiA 배포 실패: ${currentBuild.displayName}" } - always { cleanWs(cleanWhenNotBuilt: false, cleanWhenSuccess: false) } + success { sh "curl -sf -X POST ${NOTIFY} -H 'Content-Type:application/json' -d '{\"event\":\"build_result\",\"room\":\"ops\",\"success\":true,\"result_summary\":\"✅ guardia-itsm 배포 완료 #${BUILD_NUMBER}\"}' 2>/dev/null || true" } + failure { sh "curl -sf -X POST ${NOTIFY} -H 'Content-Type:application/json' -d '{\"event\":\"build_result\",\"room\":\"ops\",\"success\":false,\"result_summary\":\"❌ guardia-itsm 빌드 실패 #${BUILD_NUMBER}\"}' 2>/dev/null || true" } } -} +} \ No newline at end of file