DEPLOYMENT_GUIDE.md 4.25 KB

部署指南

本文档涵盖Linux服务器部署、Jenkins配置和多环境设置。


:pencil: 多环境支持

该框架支持在三个不同的环境中运行:

环境 DEPLOY_ENV IP_HOST 用途
测试 test ai-test.hikoon.com 开发测试
UAT uat ai-uat.hikoon.com 用户验收
生产 prod api.hikoon.com 生产验证

环境选择方法

本地运行

# 测试环境(默认)
export DEPLOY_ENV=test
python3 api_test.py

# UAT环境
export DEPLOY_ENV=uat
python3 api_test.py

# 生产环境
export DEPLOY_ENV=prod
python3 api_test.py

Jenkins中配置

已在Jenkinsfile中配置参数化支持,使用Build with Parameters选择环境。


:whale: 使用 Docker(推荐)

快速开始

构建镜像

docker-compose build

运行测试

# 测试环境
docker-compose run --rm -e DEPLOY_ENV=test api-test-runner

# UAT环境
docker-compose run --rm -e DEPLOY_ENV=uat api-test-runner

# 生产环境
docker-compose run --rm -e DEPLOY_ENV=prod api-test-runner

详见 DOCKER_GUIDE.md


:desktop:️ Linux 服务器部署(非Docker)

前提条件

  • Python 3.8+
  • pip 和 venv
  • Git

部署步骤

:one: 克隆项目

git clone <repo-url> /opt/api_test
cd /opt/api_test

:two: 创建虚拟环境

python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

:three: 测试运行

export DEPLOY_ENV=test
python3 api_test.py

:four: 生成报告

报告文件位于项目目录:api_test_report_*.html


:link: Jenkins 配置

前提条件

  • Jenkins 已安装
  • Git 插件已启用
  • Python 环境已配置

Jenkins 任务配置

:one: 创建新 Pipeline 任务

  1. Jenkins → New Item
  2. 输入任务名称
  3. 选择 Pipeline
  4. 点击 OK

:two: 配置 Pipeline

  1. Build Triggers → 选择 Poll SCM → 0 10 * * *(每天10点)
  2. Pipeline → 选择 Pipeline script from SCM
  3. SCM → Git
  4. Repository URL<your-repo-url>
  5. Branch*/main
  6. Script PathJenkinsfile
  7. Save

:three: 参数化执行

已在 Jenkinsfile 中配置参数化支持:

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

点击 Build with Parameters 选择环境执行


:wrench: 环境变量配置

env_config.py

定义多个环境的IP配置:

IP_HOST_MAP = {
    'test': 'ai-test.hikoon.com',
    'uat': 'ai-uat.hikoon.com',
    'prod': 'api.hikoon.com'
}

添加新环境

1. 编辑 env_config.py

IP_HOST_MAP = {
    'test': '...',
    'staging': 'ai-staging.hikoon.com',  # 新增
    'uat': '...',
    'prod': '...'
}

2. 编辑 Jenkinsfile

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

:bar_chart: 查看测试报告

报告位置

  • HTML报告:api_test_report_api_cases_[env]_*.html
  • Excel报告:api_test_report_api_cases_[env]_*.xlsx

报告内容

  • :chart_with_upwards_trend: 测试统计:总数、成功、失败、耗时
  • :mag: 筛选功能:按失败、响应时间筛选
  • :clipboard: 详细结果:请求参数、响应数据、错误信息

:sos: 常见问题

依赖安装失败

# 使用国内镜像
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/

Python 版本不对

# 检查版本
python3 --version

# 如需指定版本
python3.8 -m venv venv

权限不足

# 添加执行权限
chmod +x api_test.py

# 修改目录权限
sudo chown -R $USER:$USER /opt/api_test

报告未生成

# 检查 api_cases.xlsx 是否存在
ls -la api_cases.xlsx

# 运行测试查看错误
python3 api_test.py

:telephone_receiver: 获取帮助


:tada: 部署完成!