Gitlab CI配置需求:手动执行test-int/stg后自动重跑pages任务
解决GitLab CI重跑手动测试任务后不触发Pages的问题
问题说明
当前流水线结构
| Lint | Test | Deploy |
|---|---|---|
| lint | test-int | pages |
| test-stg |
需求
手动执行test-int或test-stg任务时,自动执行/重跑pages任务以上传最新制品。
当前问题
首次手动执行test-int时能触发pages任务,但再次手动重跑test-int时,pages任务不会再次执行。
原流水线配置
image: mcr.microsoft.com/playwright:v1.27.0-focal before_script: - npm ci stages: - lint - test - deploy lint: stage: lint tags: - build - docker script: - npx eslint . --ext .cjs,.js,.ts - npx gherkin-lint only: - branches - tags test-int: stage: test tags: - dev - gib - docker script: - npm run test:int only: - branches - tags when: manual test-stg: stage: test tags: - dev - gib - docker script: - npm run test:stg only: - branches - tags when: manual pages: stage: deploy needs: - test-int tags: - dev - gib - docker script: - echo "Deploying automation reports to Gitlab Pages..." artifacts: paths: - public only: - branches - tags
解决方案
问题根源是GitLab CI默认会复用之前成功的pages作业,除非其依赖的作业产生了新的制品或状态变化。通过以下调整即可实现需求:
1. 给测试任务添加制品配置
让test-int和test-stg生成public目录的制品,确保pages能获取到最新的测试报告:
test-int: stage: test tags: - dev - gib - docker script: - npm run test:int only: - branches - tags when: manual artifacts: paths: - public expire_in: 1 week # 可选,设置制品过期时间 test-stg: stage: test tags: - dev - gib - docker script: - npm run test:stg only: - branches - tags when: manual artifacts: paths: - public expire_in: 1 week
2. 修改pages任务的依赖与触发规则
调整needs使其支持两个测试任务的可选依赖,并配置规则确保手动重跑测试时pages会自动执行:
pages: stage: deploy needs: - job: test-int artifacts: true # 依赖最新的test-int制品 optional: true # 允许test-int未执行的情况 - job: test-stg artifacts: true # 依赖最新的test-stg制品 optional: true # 允许test-stg未执行的情况 tags: - dev - gib - docker script: - echo "Deploying automation reports to Gitlab Pages..." artifacts: paths: - public rules: # 当流水线是手动触发,或有手动执行的作业时,始终运行pages - if: '$CI_PIPELINE_SOURCE == "web" || $CI_JOB_MANUAL == "true"' when: always # 禁止自动流水线(如push代码)触发pages,除非手动运行测试 - if: '$CI_COMMIT_BRANCH || $CI_COMMIT_TAG' when: never
关键修改点解释
- 制品配置:测试任务生成的
public制品会在重跑时更新,让pages能检测到依赖变化并重新执行。 - 可选依赖:
needs中的optional: true确保pages可以在test-int或test-stg任意一个执行后触发,无需两个都执行。 - 触发规则:通过
rules限制pages仅在手动操作(包括重跑测试任务)时运行,避免自动流水线误触发,同时确保重跑测试时pages会跟进执行。
内容的提问来源于stack exchange,提问作者Ignas Damunskis
相关产品推荐
相关产品推荐

