Jenkinsfile 4.34 KB
pipeline {
    agent any

    parameters {
        choice(
            name: 'DEPLOY_ENV',
            choices: ['test', 'uat', 'prod'],
            description: '选择部署环境'
        )
    }

    triggers {
        cron('0 10 * * *')
    }

    environment {
        PYTHON_VERSION = '3.8'
        PROJECT_NAME = 'api_test'
        DEPLOY_ENV = "${params.DEPLOY_ENV ?: 'test'}"
        IP_HOST_TEST = 'ai-test.hikoon.com'
        IP_HOST_UAT = 'ai-uat.hikoon.com'
        IP_HOST_PROD = 'api.hikoon.com'
    }

    stages {
        stage('检出代码') {
            steps {
                echo "开始检出代码..."
                checkout scm
            }
        }

        stage('环境准备') {
            steps {
                echo "准备Python环境..."
                sh '''
                    python3 --version
                    if [ ! -d "venv" ]; then
                        python3 -m venv venv
                    fi
                    . venv/bin/activate
                    pip install --upgrade pip -q
                    pip install -r requirements.txt -q
                    echo "依赖安装完成"
                '''
            }
        }

        stage('执行测试') {
            steps {
                echo "开始执行API自动化测试 (环境: ${DEPLOY_ENV})..."
                script {
                    def ipHost = 'ai-test.hikoon.com'
                    if (env.DEPLOY_ENV == 'uat') {
                        ipHost = env.IP_HOST_UAT
                    } else if (env.DEPLOY_ENV == 'prod') {
                        ipHost = env.IP_HOST_PROD
                    } else {
                        ipHost = env.IP_HOST_TEST
                    }
                    env.IP_HOST = ipHost
                }
                sh '''
                    . venv/bin/activate
                    export DEPLOY_ENV="${DEPLOY_ENV}"
                    export IP_HOST="${IP_HOST}"
                    echo "当前执行环境: ${DEPLOY_ENV}"
                    echo "IP_HOST: ${IP_HOST}"
                    python3 api_test.py
                '''
            }
        }

        stage('生成报告') {
            steps {
                echo "正在生成测试报告..."
                sh '''
                    echo "查找报告文件..."
                    ls -lh reports/${DEPLOY_ENV}/ 2>/dev/null || echo "未找到报告文件"
                '''
            }
        }
    }

    post {
        always {
            echo "正在收集测试结果..."

            // 列出所有报告文件(调试用)
            sh '''
                echo "=== 报告文件清单 ==="
                if [ -d "reports" ]; then
                    find reports -type f -name "*.html" -o -name "*.xlsx" | sort
                else
                    echo "reports 目录不存在"
                fi
                echo "===================="
            '''

            // 归档报告文件
            archiveArtifacts artifacts: 'reports/**/*',
                            allowEmptyArchive: true,
                            fingerprint: true

            // 发布 HTML 报告到 Jenkins
            script {
                def reportDir = "reports/${env.DEPLOY_ENV}"
                sh "ls -la ${reportDir} 2>/dev/null || echo 'Directory not found'"

                def htmlFile = sh(
                    script: "find ${reportDir} -maxdepth 1 -name '*.html' -type f 2>/dev/null | sort -r | head -1 | xargs basename",
                    returnStdout: true
                ).trim()

                if (htmlFile) {
                    echo "找到HTML报告: ${htmlFile}"
                    publishHTML([
                        reportDir: reportDir,
                        reportFiles: htmlFile,
                        reportName: "Test Report [${env.DEPLOY_ENV}]",
                        keepAll: true,
                        alwaysLinkToLastBuild: true,
                        allowMissing: false
                    ])
                    echo "HTML报告已发布到Jenkins"
                } else {
                    echo "警告: 未找到HTML报告文件 (路径: ${reportDir})"
                }
            }
        }

        success {
            echo "[SUCCESS] [${DEPLOY_ENV}环境] 所有测试用例运行成功!"
        }

        failure {
            echo "[FAILURE] [${DEPLOY_ENV}环境] 有测试用例执行失败!"
        }
    }
}