Azure DevOps经典编辑器下,如何避免"Create work item"任务创建同名重复工作项
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:
- Open the task's Control Options tab.
- Under Run this task, select Custom condition.
- 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
LIKEinstead of=(e.g.,[System.Title] LIKE '$targetTitle'— WIQL'sLIKEis case-insensitive by default). - Adjust State Filters: Tweak the
[System.State] NOT INclause 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

