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

Azure Pipeline基于计划+审批部署指定阶段的配置问题

Azure Pipeline 计划部署阶段配置问题解决

问题背景

现有5个Pipeline阶段:

  • Build
  • Deployment to Test - Country A
  • Deployment to Test - Country B
  • Deployment to Prod - Country A
  • Deployment to Prod - Country B

需求:Test和Prod的四个阶段仅在周一至周四夜间由计划触发,触发后需经审批才能执行,且仅执行一次,不重复运行。

当前已配置的计划如下,但使用condition: eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays')作为阶段条件并未实现预期效果:

schedules:
  - cron: '31 12 * 1-12 1-5'
    displayName: WeekDays
    branches:
      include:
      - AddStage
    always: true
  - cron: '13 20 * 1-12 5-6'
    displayName: Weekend
    branches:
      include:
      - releases/*
    always: true

问题分析

  1. Cron表达式错误:当前WeekDays计划的星期范围是1-5(周一至周五),不符合需求的周一至周四,需要调整为1-4。
  2. 条件不严谨:仅检查计划名称不足以确保是计划触发,需同时验证触发原因是计划,避免手动触发时阶段意外执行。
  3. 缺少审批配置:需求中要求经审批后部署,需通过关联环境并配置审批规则实现。

解决方案

1. 修正计划的Cron表达式

将WeekDays计划的星期段改为1-4,确保仅在周一至周四触发:

schedules:
  - cron: '31 12 * 1-12 1-4' # UTC时间12:31,可根据本地时区调整时间点
    displayName: WeekDays
    branches:
      include:
      - AddStage
    always: true
  - cron: '13 20 * 1-12 5-6'
    displayName: Weekend
    branches:
      include:
      - releases/*
    always: true

2. 配置阶段条件与审批

为Test和Prod阶段添加严谨的触发条件,并通过Deployment作业关联环境配置审批:

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo 执行构建任务...

# Test阶段配置
- stage: Deploy_Test_CountryA
  displayName: Deployment to Test - Country A
  # 仅在计划触发且是WeekDays计划时执行
  condition: and(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays'))
  jobs:
  - deployment: DeployTestA
    environment: 'Test-CountryA' # 在此环境中配置审批规则
    strategy:
      runOnce: # 确保阶段仅执行一次
        deploy:
          steps:
          - script: echo 部署至Test环境(Country A)...

- stage: Deploy_Test_CountryB
  displayName: Deployment to Test - Country B
  condition: and(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays'))
  jobs:
  - deployment: DeployTestB
    environment: 'Test-CountryB' # 配置环境审批
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo 部署至Test环境(Country B)...

# Prod阶段配置
- stage: Deploy_Prod_CountryA
  displayName: Deployment to Prod - Country A
  condition: and(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays'))
  jobs:
  - deployment: DeployProdA
    environment: 'Prod-CountryA' # 配置Prod环境审批
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo 部署至Prod环境(Country A)...

- stage: Deploy_Prod_CountryB
  displayName: Deployment to Prod - Country B
  condition: and(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays'))
  jobs:
  - deployment: DeployProdB
    environment: 'Prod-CountryB' # 配置Prod环境审批
    strategy:
      runOnce:
        deploy:
          steps:
          - script: echo 部署至Prod环境(Country B)...

关键说明

  • 触发条件:通过and(eq(variables['Build.Reason'], 'Schedule'), eq(variables['Build.CronSchedule.DisplayName'], 'WeekDays'))确保阶段仅在WeekDays计划触发时运行,排除手动触发等场景。
  • 审批配置:在Azure DevOps的环境设置中,为Test-CountryA、Test-CountryB、Prod-CountryA、Prod-CountryB添加审批人员,满足经审批后部署的需求。
  • 单次执行:使用runOnce部署策略,确保阶段触发后仅执行一次,不会重复运行。

内容的提问来源于stack exchange,提问作者Sergio

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:20:33