接口自动化实现Jenkins持续集成之多环境配置
一、先建立整体概念1 分钟看懂1.1 三套环境叫什么环境键名TEST_ENV含义域名示例test测试环境https://test- example.compre预发布环境https://pre-example.comprod正式环境https://example.com1.2 配置放在哪、用例怎么写config/config.yaml ← 三套环境的域名 登录账号只改这里切换环境数据 ↓ 环境变量 TEST_ENVtest|pre|prod ← 告诉框架「本次用哪一套」 ↓ data/**/*.yaml ← 只写 base_url_key、${login_phone_no}不写完整域名 ↓ 自动请求对应环境的接口1.3 一张图谁决定环境┌─────────────────────────────────────────────────────────────┐ │ 本地TEST_ENVpre python3 run.py │ │ Jenkins 手动Build with Parameters 选 pre │ │ Jenkins 定时固定用 Jenkinsfile 里的 CRON_TEST_ENV │ └───────────────────────────┬─────────────────────────────────┘ ▼ common/config_loader.py 读取 TEST_ENV ▼ config.yaml → environments.pre / test / prod ▼ 用例执行域名 登录变量 auth_token二、改动过的文件清单按实施顺序顺序文件作用是否常改1config/config.yaml定义 test/pre/prod 的域名、登录变量✅ 常改2common/config_loader.py读TEST_ENV取当前环境配置一般不改3conftest.pypytest 启动时注入登录变量、打印当前环境一般不改4test_api.py用base_url_key取域名${变量}替换一般不改5data/**/**_test.yaml用例base_url_key、${login_*}、need_auth新增用例时改6JenkinsfileCI 传环境、定时专用CRON_TEST_ENV改定时环境时改7scripts/ci_run.shJenkins 入口执行python3 run.py一般不改三、每个文件怎么改配置步骤步骤 1改config/config.yaml核心必会作用三套环境的所有「域名 登录账号」集中写在这里。结构示例default_env:test# 本地不设置 TEST_ENV 时默认用 testenvironments:test:admin_url:https://test-example.comsale_admin_url:https://test-example.comlogin_phone_code:86login_phone_no:1700000000login_captcha:1008pre:admin_url:https://pre-example.comsale_admin_url:https://pre-example.comlogin_phone_code:86login_phone_no:预发手机号login_captcha:预发有效验证码prod:admin_url:https://example.comsale_admin_url:https://example.comlogin_phone_code:86login_phone_no:正式环境手机号login_captcha:正式环境验证码改动说明配置项对应用例里说明admin_urlbase_url_key: admin_url后台域名sale_admin_urlbase_url_key: sale_admin_url销售后台域名login_phone_code${login_phone_code}手机区号login_phone_no${login_phone_no}手机号login_captcha${login_captcha}登录验证码注意environments下的键名必须与TEST_ENV一致test、pre、prod。步骤 2common/config_loader.py作用get_current_env()读环境变量TEST_ENV没有则用default_envget_base_url(key)按当前环境取admin_url等域名get_env_variables()取当前环境的login_*等非 URL 配置关键逻辑# 优先 os.getenv(TEST_ENV)否则 config.yaml 的 default_envreturn(os.getenv(TEST_ENV)orcfg.get(default_env)ortest).strip()日常换环境不用改这个文件只改config.yaml和运行时的TEST_ENV。步骤 3conftest.py作用每次 pytest 开始前清空config/extract.yaml避免旧 token把当前环境的login_*写入Context供${login_phone_no}使用打印当前环境和域名日志里可核对步骤 4YAML 用例怎么写data/**/*.yaml域名不写https://...只写逻辑键base_url_key:admin_url# 或 sale_admin_url登录使用配置里的变量跨文件通用靠Contextdata:phoneCode:${login_phone_code}phoneNo:${login_phone_no}captcha:${login_captcha}需要登录态的业务接口request:need_auth:true# 自动从 Context 取 auth_token 注入请求头切换环境时同一条用例 YAML不用改只改config.yaml或TEST_ENV。步骤 5改Jenkinsfile仅 CI / 定时需要作用配置作用parameters { choice(name: TEST_ENV, ...) }手动构建时页面下拉选环境CRON_TEST_ENV pre仅定时构建使用的环境script { isTimer ? CRON_TEST_ENV : params.TEST_ENV }区分定时 vs 手动改定时跑哪套环境只改一行改完需git pushenvironment{CRON_TEST_ENVpre// 改成 test / pre / prod}定时规则可按需改triggers{cron(H 9,13,18 * * *)// 每天 9、13、18 点}注意不要在sh 单引号里写${params.TEST_ENV}会报Bad substitution当前已用environmentEFFECTIVE_TEST_ENV注入。四、环境变量TEST_ENV说明名称含义TEST_ENV本次运行使用哪套环境值test/pre/prodCRON_TEST_ENV仅 Jenkinsfile 内使用给定时构建专用虽然变量名叫TEST_ENV但可以设为pre、prod不限于 test。五、本地如何选择环境跑脚本5.1 进入项目目录cd/path/to/Api_Framework5.2 安装依赖首次python3-mpipinstall-rrequirements.txt5.3 选择环境执行环境命令测试默认python3 run.py测试显式TEST_ENVtest python3 run.py预发布TEST_ENVpre python3 run.py正式慎用TEST_ENVprod python3 run.py5.4 只跑部分用例TEST_ENVtest python3-mpytest-s-vdata/00_公共/TEST_ENVpre python3-mpytest-s-vdata/01_单接口/课程列表/5.5 如何确认环境对了看终端或logs/run_日期.log开头 当前运行环境: TEST_ENVpre admin_url https://pre-example.com login_phone_no 1700000000再看请求日志里的完整 URL 是否带test-/pre-或正式域名。5.6 Windows$env:TEST_ENVpre;python3 run.py六、Jenkins 如何跑脚本6.1 前置条件项要求Job 类型Pipeline脚本来源Pipeline script from SCM仓库本项目 Git 地址分支如*/mainScript PathJenkinsfile6.2 流水线执行顺序Checkout → Setup Envpip install→ Run API Testsrun.py → Archive Artifacts → Send Mail Report实际测试命令链export TEST_ENV... → bash scripts/ci_run.sh → python3 run.py → pytest七、Jenkins 手动构建选环境操作步骤打开 Jenkins → 进入任务如apitest002点击Build with Parameters带参数构建TEST_ENV下拉选择test/pre/prod点击Build打开本次构建 →Console Output日志里应看到[DEBUG] 构建触发: 手动, TEST_ENVtest [DEBUG] TEST_ENVtest 当前运行环境: TEST_ENVtest说明手动构建不会使用CRON_TEST_ENV只用你在页面选的TEST_ENVchoices第一项是test页面默认一般选中 test八、Jenkins 定时构建8.1 原理触发方式使用的环境定时cronJenkinsfile里的CRON_TEST_ENV手动参数TEST_ENVBuild with Parameters两者互不影响定时固定 pre手动仍可选 test。8.2 如何把定时改为预发布 pre编辑JenkinsfileCRON_TEST_ENVpre确认config/config.yaml里environments.pre的域名、验证码正确git add Jenkinsfile config/config.yaml→commit→push origin mainJenkins 无需改 Job 配置从 SCM 拉最新 Jenkinsfile 即可8.3 定时规则在哪改Jenkinsfile→triggers { cron(H 9,13,18 * * *) }8.4 定时构建日志里应看到[DEBUG] 构建触发: 定时(cron), TEST_ENVpre [DEBUG] TEST_ENVpre 当前运行环境: TEST_ENVpre 请求: [POST] https://pre-example.com/...8.5 注意任务Configure里若也勾了「定时构建」可能与 Jenkinsfile 的 cron重复触发建议只保留 Jenkinsfile 里的triggers定时构建不能在网页上选环境只能改CRON_TEST_ENV九、三种运行方式对照表运行方式谁设置环境命令 / 操作实际 TEST_ENV本地默认无python3 run.pytestdefault_env本地指定终端TEST_ENVpre python3 run.pypreJenkins 手动网页参数Build with Parameters → 选 pre你选的值Jenkins 定时Jenkinsfile自动 cron无需点页面CRON_TEST_ENV十、构建结果说明Jenkins结果含义SUCCESS用例全部通过.ci_exit_code0UNSTABLE流水线跑通但有接口用例失败.ci_exit_code1FAILURE流水线脚本错误如依赖安装失败、Bad substitution有用例失败时仍会归档报告、发邮件若已配置 SMTP。十一、常见问题Q1${login_phone_no}能跨 YAML 文件取到值吗能。值来自config.yaml当前环境由conftest写入全局Context任意data/**/*.yaml都可${login_phone_no}。Q2切换环境要不要改几百条用例不用。改config.yaml或设置TEST_ENV即可用例继续用base_url_key和${变量}。Q3定时想跑 test手动想跑 pre 可以吗可以。CRON_TEST_ENV test手动构建时选pre。Q4pre 环境登录失败检查environments.pre的login_captcha是否为预发有效验证码常与 test 不同。Q5Jenkins 报Bad substitution不要在sh 里写${params.TEST_ENV}使用当前 Jenkinsfile 的EFFECTIVE_TEST_ENV写法。