Jenkinsfile 3.74 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 "正在收集测试结果..."
            // 归档报告文件
            archiveArtifacts artifacts: 'reports/**/*.{html,xlsx}',
                            allowEmptyArchive: true,
                            defaultExcludes: false

            // 发布 HTML 报告到 Jenkins
            script {
                def reportDir = "reports/${env.DEPLOY_ENV}"
                def htmlReports = sh(
                    script: "find ${reportDir} -maxdepth 1 -name '*.html' -type f | sort -r | head -1",
                    returnStdout: true
                ).trim()

                if (htmlReports) {
                    echo "找到最新HTML报告: ${htmlReports}"
                    publishHTML([
                        reportDir: '.',
                        reportFiles: htmlReports,
                        reportName: "API Test Report (${env.DEPLOY_ENV})",
                        keepAll: true,
                        alwaysLinkToLastBuild: true
                    ])
                } else {
                    echo "未找到HTML报告文件"
                }
            }
        }

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

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