我需要自动化分支管道来执行以下步骤:
如何实现这一点?
使用门并调用其余API(补丁)来更新(活动/关闭)工作项
可以参考以下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