diff --git a/.dockerignore b/.dockerignore index a51ce185..5bc11e9a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,6 +11,8 @@ frontend/dist backend/venv backend/local_test.db deploy -docs +docs/* +!docs/ +!docs/(表+字段).md tmp_restore -tests \ No newline at end of file +tests diff --git a/Jenkinsfile b/Jenkinsfile index 635d0cd6..9a6bbe40 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -51,6 +51,35 @@ pipeline { } } + stage('Validate Docker Inputs') { + steps { + sh ''' + set -e + test -f docker-compose.yml + test -f backend/Dockerfile + test -f "docs/(表+字段).md" + + if grep -Eq '^docs/?$' .dockerignore 2>/dev/null && ! grep -Fxq '!docs/(表+字段).md' .dockerignore; then + echo "[Validate Docker Inputs] .dockerignore excludes docs, but docs/(表+字段).md is not re-included." + echo "[Validate Docker Inputs] Please keep docs/(表+字段).md in the Docker build context for Text2SQL." + exit 1 + fi + + if ! grep -Fq 'context: .' docker-compose.yml || ! grep -Fq 'dockerfile: backend/Dockerfile' docker-compose.yml; then + echo "[Validate Docker Inputs] docker-compose.yml must build hxops-backend from repository root with backend/Dockerfile." + exit 1 + fi + + if ! grep -Fq 'COPY docs/(表+字段).md /app/docs/(表+字段).md' backend/Dockerfile; then + echo "[Validate Docker Inputs] backend/Dockerfile must copy docs/(表+字段).md into the image." + exit 1 + fi + + echo "[Validate Docker Inputs] Docker build context and Text2SQL schema inputs are ready." + ''' + } + } + stage('Detect Changes') { steps { script { @@ -111,10 +140,13 @@ pipeline { // 目录级变更识别:用于前后端增量构建 / Directory-level change detection for incremental builds def frontendChanged = changedFiles.any { it.startsWith('frontend/') } - def backendChanged = changedFiles.any { it.startsWith('backend/') } + def backendChanged = changedFiles.any { + it.startsWith('backend/') || it == 'docs/(表+字段).md' + } def deployConfigChanged = changedFiles.any { it == 'docker-compose.yml' || it == 'Jenkinsfile' || + it == '.dockerignore' || it == 'scripts/deploy_compose.sh' || it == 'scripts/healthcheck.sh' || it == '.env.dev' || diff --git a/backend/Dockerfile b/backend/Dockerfile index a5516ddc..19d0bcff 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -13,9 +13,10 @@ RUN apt-get update \ COPY backend/requirements.txt . RUN pip install --no-cache-dir -r requirements.txt -# Copy backend code and bundled Text2SQL schema docs +# Copy backend code and the Text2SQL schema required at runtime COPY backend/ . -COPY docs /app/docs +RUN mkdir -p /app/docs +COPY docs/(表+字段).md /app/docs/(表+字段).md ENV ORACLE_HOME=/app/oracle_client/instantclient_19_31 ENV LD_LIBRARY_PATH=/app/oracle_client/instantclient_19_31:/app/oracle_client:${LD_LIBRARY_PATH} diff --git a/scripts/deploy_compose.sh b/scripts/deploy_compose.sh index 2cf4c0aa..65a63614 100644 --- a/scripts/deploy_compose.sh +++ b/scripts/deploy_compose.sh @@ -20,6 +20,9 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" COMPOSE_FILE="${ROOT_DIR}/docker-compose.yml" ENV_FILE="${ROOT_DIR}/.env.${ENV_NAME}" ORACLE_CLIENT_DIR="${ROOT_DIR}/oracle_client" +BACKEND_DOCKERFILE="${ROOT_DIR}/backend/Dockerfile" +SCHEMA_DOC_PATH="${ROOT_DIR}/docs/(表+字段).md" +ROOT_DOCKERIGNORE="${ROOT_DIR}/.dockerignore" check_oracle_client() { if [[ ! -d "${ORACLE_CLIENT_DIR}" ]]; then @@ -35,6 +38,49 @@ check_oracle_client() { fi } +check_backend_build_inputs() { + echo "Validating backend Docker build inputs..." + echo "- compose file: ${COMPOSE_FILE}" + echo "- backend Dockerfile: ${BACKEND_DOCKERFILE}" + echo "- schema doc: ${SCHEMA_DOC_PATH}" + echo "- dockerignore: ${ROOT_DOCKERIGNORE}" + + if [[ ! -f "${BACKEND_DOCKERFILE}" ]]; then + echo "Missing backend Dockerfile: ${BACKEND_DOCKERFILE}" + exit 1 + fi + + if [[ ! -f "${SCHEMA_DOC_PATH}" ]]; then + echo "Missing Text2SQL schema doc: ${SCHEMA_DOC_PATH}" + echo "Please ensure docs/(表+字段).md exists in the repository root before building hxops-backend." + exit 1 + fi + + if ! grep -Fq 'context: .' "${COMPOSE_FILE}" || ! grep -Fq 'dockerfile: backend/Dockerfile' "${COMPOSE_FILE}"; then + echo "Invalid backend build settings in ${COMPOSE_FILE}" + echo "hxops-backend must be built from repository root with:" + echo " context: ." + echo " dockerfile: backend/Dockerfile" + exit 1 + fi + + if ! grep -Fq 'COPY docs/(表+字段).md /app/docs/(表+字段).md' "${BACKEND_DOCKERFILE}"; then + echo "Invalid backend Dockerfile: schema doc is not copied into the image." + echo "Expected line:" + echo " COPY docs/(表+字段).md /app/docs/(表+字段).md" + exit 1 + fi + + if [[ -f "${ROOT_DOCKERIGNORE}" ]] && grep -Eq '^(docs|docs/\*)$' "${ROOT_DOCKERIGNORE}"; then + if ! grep -Fxq '!docs/(表+字段).md' "${ROOT_DOCKERIGNORE}"; then + echo "Invalid .dockerignore: docs is excluded but docs/(表+字段).md is not re-included." + echo "Please add the following line to ${ROOT_DOCKERIGNORE}:" + echo " !docs/(表+字段).md" + exit 1 + fi + fi +} + # 基础文件检查:compose 文件必须存在 if [[ ! -f "${COMPOSE_FILE}" ]]; then echo "Missing compose file: ${COMPOSE_FILE}" @@ -109,6 +155,11 @@ echo "Prebuilt image mode: ${PREBUILT_MODE}" if printf '%s\n' "${services[@]}" | grep -qx "hxops-backend"; then echo "Checking Oracle Instant Client directory..." check_oracle_client + if [[ "${PREBUILT_MODE}" != "true" ]]; then + check_backend_build_inputs + else + echo "Skipping backend Docker input validation because prebuilt image mode is enabled." + fi fi # 预构建镜像模式:只启动,不构建