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

Azure Pipelines重跑失败作业时仅执行未完成任务的需求实现

解决方案

核心思路

针对你的需求,我们可以通过Azure Pipelines的内置变量和artifact检查机制实现:

  • 用$(System.JobAttempt)变量判断是否为重跑(尝试次数≥2)
  • 每个成功的job发布唯一名称的artifact,重跑时通过检查该artifact是否存在决定是否执行job
  • 重跑时若artifact已存在,执行一个空步骤并标记job成功,避免显示「跳过」状态

1. 修改主Pipeline配置

更新每个模板调用,传递唯一的jobIdentifier参数,用于生成专属artifact名称:

stages:
  - stage: Stage1
    jobs:
    - template: execution_template1.yml
      parameters:
        value: value1
        jobIdentifier: "Stage1-Job1"
    - template: execution_template2.yml
      parameters:
        value: value1
        jobIdentifier: "Stage1-Job2"

  - stage: Stage2
    condition: not(canceled())
    jobs:
    - template: execution_template3.yml
      parameters:
        value: value1
        jobIdentifier: "Stage2-Job1"
    - template: execution_template4.yml
      parameters:
        value: value1
        jobIdentifier: "Stage2-Job2"

  - stage: Stage3
    condition: not(canceled())
    jobs:
    - template: execution_template5.yml
      parameters:
        value: value1
        jobIdentifier: "Stage3-Job1"
    - template: execution_template6.yml
      parameters:
        value: value1
        jobIdentifier: "Stage3-Job2"

  - stage: Stage4
    condition: not(canceled())
    jobs:
    - template: consolidate.yml
      parameters:
        result: value1

2. 更新执行模板(如execution_template1.yml)

在模板中添加artifact检查逻辑、条件判断和状态处理:

parameters:
  value: ''
  jobIdentifier: ''

jobs:
- job: ${{ parameters.jobIdentifier }}
  displayName: ${{ parameters.jobIdentifier }}
  condition: |
    or(
      eq(variables['System.JobAttempt'], 1),
      not(existsArtifacts('${{ parameters.jobIdentifier }}-output'))
    )
  steps:
  # 重跑时若artifact已存在,直接输出并标记成功,避免显示「跳过」
  - script: |
      echo "Job ${{ parameters.jobIdentifier }} already succeeded in previous attempt, artifact exists."
    condition: and(ge(variables['System.JobAttempt'], 2), existsArtifacts('${{ parameters.jobIdentifier }}-output'))
    displayName: "Skip execution (artifact exists)"

  # 首次尝试或artifact不存在时,执行原有业务逻辑
  - script: |
      # 替换为你的实际执行逻辑,比如编译、测试等
      echo "Executing job with value: ${{ parameters.value }}"
    condition: or(eq(variables['System.JobAttempt'], 1), not(existsArtifacts('${{ parameters.jobIdentifier }}-output')))
    displayName: "Execute main logic"

  # 成功后发布唯一名称的artifact
  - publish: $(Build.ArtifactStagingDirectory)
    artifact: ${{ parameters.jobIdentifier }}-output
    displayName: "Publish unique artifact"
    condition: succeeded()

3. 调整consolidate.yml模板(可选)

如果consolidate job需要依赖所有前置artifact,可以添加检查逻辑,确保所有必要artifact存在再执行:

parameters:
  result: ''

jobs:
- job: ConsolidateJob
  displayName: Consolidate Results
  steps:
  # 检查所有前置artifact是否存在
  - script: |
      required_artifacts=("Stage1-Job1-output" "Stage1-Job2-output" "Stage2-Job1-output" "Stage2-Job2-output" "Stage3-Job1-output" "Stage3-Job2-output")
      for artifact in "${required_artifacts[@]}"; do
        if ! az pipelines runs artifact list --run-id $(Build.BuildId) --artifact-name "$artifact" --output none; then
          echo "Error: Required artifact $artifact not found"
          exit 1
        fi
      done
    displayName: "Validate all required artifacts exist"

  # 原有合并逻辑
  - script: |
      echo "Consolidating results with value: ${{ parameters.result }}"
    displayName: "Consolidate main logic"

关键说明

  • existsArtifacts函数:Azure Pipelines内置函数,用于检查当前运行中是否存在指定名称的artifact
  • System.JobAttempt变量:标记当前job的尝试次数,首次尝试为1,重跑时递增
  • 状态显示优化:重跑时若artifact存在,执行一个成功的脚本步骤,job状态会显示「成功」而非「跳过」,符合你的需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 10:24:56