AzureDevOps - 自动化分支管道:工作项中的任务状态检查

问题描述 投票:0回答:1

我需要自动化分支管道来执行以下步骤:

  • 检查特定任务ID的状态。
  • 如果任务 ID 为“已关闭”,请重新打开它并将其状态设置为“活动”。
  • 继续管道部署的下一阶段。
  • 部署完成后,再次关闭任务ID。

如何实现这一点?

使用门并调用其余API(补丁)来更新(活动/关闭)工作项

azure-devops azure-devops-rest-api
1个回答
0
投票

可以参考以下yaml:

variables:
  workitemid: {Your target work item id}

trigger:
- none

pool:
  vmImage: windows-latest

stages:
- stage: stageA
  jobs:
  - job: jobA
    steps:
    - task: PowerShell@2
      displayName: Check the state of target work item
      name: MyOutputVar
      inputs:
        targetType: 'inline'
        script: |
          $header = @{'Authorization' = 'Bearer ' + "$(System.AccessToken)"}
          
          # Get the work item
          $url = "$(System.CollectionUri)$(System.TeamProjectId)/_apis/wit/workitems/$(workitemid)?api-version=7.1-preview.3"
          $workItem = Invoke-RestMethod -Uri $url -Headers $header -ContentType "application/json" -Method Get
          
          # Check the state of the work item
          if ($workItem.fields.'System.State' -eq "Closed") {
          # If the state is "Closed", change it to "Active"
          $body = @'
          [
            {
            "op": "add",
            "path": "/fields/System.State",
            "value": "Active"
            }
          ]
          '@
          # Update the work item
          Invoke-RestMethod -Uri $url -Headers $header -ContentType "application/json-patch+json" -Body $body -Method Patch
          } else {
            Write-Host "The stage of work item is" $workItem.fields.'System.State'
          }
- stage: stageB
  jobs:
  - job: Job1
    steps:
      - script: echo Running Stage B
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            $header = @{'Authorization' = 'Bearer ' + "$(System.AccessToken)"}
            $url = "$(System.CollectionUri)$(System.TeamProjectId)/_apis/wit/workitems/$(workitemid)?api-version=7.1-preview.3"
            $body = @'
            [
                {
                  "op": "add",
                  "path": "/fields/System.State",
                  "value": "Closed"
                }
            ]
            '@
            Invoke-RestMethod -Uri $url -Headers $header -ContentType "application/json-patch+json" -Body $body -Method Patch
            
© www.soinside.com 2019 - 2024. All rights reserved.