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

如何配置Azure YAML Pipeline每月最后一个周日4点运行?

每月最后一个周日触发Azure YAML Pipeline的解决方案

首先明确:纯标准cron表达式无法实现“每月最后一个周日凌晨4点运行”的需求。原因是cron的日字段(如25-31)和星期字段(Sun)是逻辑或的关系,而非逻辑与——这意味着你之前尝试的表达式会同时触发:

  • 每月25-31日的所有凌晨4点(不管当天是不是周日)
  • 每月所有周日的凌晨4点(不管日期是否在25-31之间)
    完全不符合“仅当月最后一个周日执行”的要求。

推荐采用「每周日触发管道 + 脚本判断是否为当月最后一个周日」的方案,具体实现如下:

步骤1:保留每周日的cron触发

先让管道每周日凌晨4点按时触发,后续通过脚本过滤非目标日期的执行:

trigger: none

schedules:
- cron: '0 4 * * Sun'
  displayName: 每周日凌晨4点触发
  branches:
    include:
    - main  # 替换为你的目标分支
  always: false

步骤2:添加日期判断脚本

在管道开头加入脚本,判断当天是否为当月最后一个周日,若不是则终止管道执行:

PowerShell版本(适用于Windows代理)

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $today = Get-Date
      $lastDayOfMonth = [datetime]::DaysInMonth($today.Year, $today.Month)
      $lastDay = Get-Date -Year $today.Year -Month $today.Month -Day $lastDayOfMonth
      
      # 从月末往前推,找到第一个周日
      while ($lastDay.DayOfWeek -ne [DayOfWeek]::Sunday) {
          $lastDay = $lastDay.AddDays(-1)
      }
      
      if ($today.Date -ne $lastDay.Date) {
          Write-Host "今天不是当月最后一个周日,终止管道执行"
          exit 1
      }
  displayName: 检查是否为当月最后一个周日

Bash版本(适用于Linux/macOS代理)

steps:
- task: Bash@3
  inputs:
    targetType: 'inline'
    script: |
      # 获取当月最后一天的日期
      last_day_of_month=$(date -d "$(date +%Y-%m-01) +1 month -1 day" +%Y-%m-%d)
      # 计算当月最后一个周日:从最后一天往前推到周日
      last_sunday=$(date -d "$last_day_of_month - $(($(date -d "$last_day_of_month" +%u) % 7)) days" +%Y-%m-%d)
      # 获取今天的日期
      today=$(date +%Y-%m-%d)
      
      if [ "$today" != "$last_sunday" ]; then
          echo "今天不是当月最后一个周日,终止管道执行"
          exit 1
      fi
  displayName: 检查是否为当月最后一个周日

步骤3:控制后续任务执行

如果后续还有核心任务,可以通过条件判断,仅在验证通过时执行:

- script: echo "执行当月最后一个周日的核心任务"
  condition: succeeded()  # 仅当前面的检查步骤成功时执行
  displayName: 执行核心任务

这种方案逻辑清晰、适配所有月份的情况,不需要维护复杂的cron表达式列表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 10:02:50