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

Azure DevOps经典编辑器下,如何避免"Create work item"任务创建同名重复工作项

Avoid Duplicate Work Items with Same Name in Azure DevOps Classic Pipeline

Great question! When using the classic editor to build your Azure DevOps pipeline, preventing duplicate work items (with identical names) requires adding a quick pre-check before your "Create work item" task. Here's a straightforward, step-by-step approach:

Step 1: Add a PowerShell Task to Check for Existing Work Items

First, insert a PowerShell task right before your "Create work item" task. This script will query your Azure DevOps project to see if a work item with the target name already exists.

Configure the PowerShell Task:

  • Go to the Control Options tab and check Allow scripts to access OAuth token (this lets the script authenticate against the Azure DevOps API).
  • Paste the following script into the Script field, replacing placeholders with your project details:
# Set your project details
$projectName = "YourProjectName"
$workItemType = "Bug" # Or User Story, Task, etc.
$targetTitle = "Your Work Item Title" # Use pipeline variables here, e.g., $(Build.DefinitionName)

# Get OAuth token from pipeline environment
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:SYSTEM_ACCESSTOKEN)"))
$headers = @{Authorization = "Basic $token"}

# Build WIQL query to check for existing work items
$wiqlQuery = @"
SELECT [System.Id]
FROM WorkItems
WHERE [System.Title] = '$targetTitle'
AND [System.WorkItemType] = '$workItemType'
AND [System.TeamProject] = '$projectName'
AND [System.State] NOT IN ('Closed', 'Removed')
"@

# Execute the query against Azure DevOps API
$apiUrl = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$projectName/_apis/wit/wiql?api-version=7.1-preview.2"
$response = Invoke-RestMethod -Uri $apiUrl -Headers $headers -Method Post -ContentType "application/json" -Body (@{query = $wiqlQuery} | ConvertTo-Json)

# Set pipeline variable to control work item creation
if ($response.workItems.Count -gt 0) {
    Write-Host "##vso[task.setvariable variable=CREATE_WORK_ITEM;]false"
    Write-Host "Duplicate work item found with title: $targetTitle. Skipping creation."
} else {
    Write-Host "##vso[task.setvariable variable=CREATE_WORK_ITEM;]true"
    Write-Host "No existing work item found. Proceeding to create."
}
  • Pro Tip: Make the title dynamic by using pipeline variables (e.g., $(Build.DefinitionName) - $(Build.BuildNumber)) instead of hardcoding it.

Step 2: Add a Condition to the "Create Work Item" Task

Update your existing "Create work item" task to only run if no duplicate was found:

  1. Open the task's Control Options tab.
  2. Under Run this task, select Custom condition.
  3. Enter the following condition:
and(succeeded(), eq(variables['CREATE_WORK_ITEM'], 'true'))

This ensures the task only executes if the PowerShell script confirmed no matching work item exists, and all prior pipeline steps succeeded.

Optional Tweaks

  • Case Insensitivity: To ignore case when checking titles, modify the WIQL query to use LIKE instead of = (e.g., [System.Title] LIKE '$targetTitle' — WIQL's LIKE is case-insensitive by default).
  • Adjust State Filters: Tweak the [System.State] NOT IN clause if you want to exclude additional states from the duplicate check (e.g., add 'Resolved' if you don't want to recreate resolved items).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 15:37:34