You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在YAML中定时触发特定Testing阶段的流水线部署?

定时仅触发Testing阶段部署的解决方案

要实现每日午夜仅触发Testing阶段的部署,无需调整整条流水线的触发逻辑,只需通过阶段条件判断控制不同触发场景下的阶段执行:

方案1:利用内置触发变量判断

Azure DevOps流水线在定时触发时,内置变量Build.Reason的值为Schedule。我们可以借助这个变量给DEV阶段添加执行条件,让它仅在非定时触发时运行;Testing阶段保持默认执行逻辑,这样定时触发时就只会跑Testing阶段。

完整YAML示例:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight Testing deployment
  branches:
    include:
    - main

stages:
- stage: DEV
  displayName: Deploy to DEV
  # 仅当触发原因不是定时调度时,才执行DEV阶段
  condition: ne(variables['Build.Reason'], 'Schedule')
  jobs:
  - job: DeployDEV
    steps:
    - script: echo "Deploying to DEV environment"

- stage: Testing
  displayName: Deploy to Testing
  # 定时或手动触发都执行该阶段
  condition: or(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.Reason'], 'Manual'))
  jobs:
  - job: DeployTesting
    steps:
    - script: echo "Deploying to Testing environment"

方案2:自定义触发标记变量

如果需要更灵活的触发类型区分,可以给定时任务添加自定义变量,通过这个变量控制阶段执行:

schedules:
- cron: "0 0 * * *"
  displayName: Daily midnight Testing deployment
  branches:
    include:
    - main
  # 给定时触发添加自定义标记变量
  variables:
    triggerSource: 'scheduled'

stages:
- stage: DEV
  displayName: Deploy to DEV
  # 仅当没有scheduled标记时执行
  condition: ne(variables.get('triggerSource', 'manual'), 'scheduled')
  jobs:
  - job: DeployDEV
    steps:
    - script: echo "Deploying to DEV environment"

- stage: Testing
  displayName: Deploy to Testing
  # 无论是否定时都执行(可根据需求调整条件)
  jobs:
  - job: DeployTesting
    steps:
    - script: echo "Deploying to Testing environment"

说明

两种方案都能实现「定时触发仅运行Testing阶段,手动触发正常执行DEV+Testing阶段」的核心需求。若需手动触发时也能单独执行某个阶段,可结合流水线参数进一步扩展。

内容的提问来源于stack exchange,提问作者Alfredo A.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 17:55:18