如何搜索Azure DevOps流水线?非YAML类型未存储仓库流水线检索及重构需求咨询
Got it, let's break down how you can track down those classic (non-YAML, not stored in code repos) Azure DevOps pipelines with repetitive bash script snippets you need to refactor. Here are practical, actionable methods:
1. Use the Azure DevOps REST API to Scan Pipeline Definitions
Classic pipelines are stored directly in Azure DevOps (not your repo), so the REST API is your best bet to pull their full definitions and search for specific content.
Step-by-Step:
List all classic pipelines in your organization/project:
Call this endpoint to get all pipelines, then filter for classic ones (identified byconfiguration.type = designer):GET https://dev.azure.com/{your-org}/{your-project}/_apis/pipelines?api-version=7.1-preview.1Parse the JSON response to collect all
idvalues whereconfiguration.typeisdesigner—these are your classic pipelines.Fetch full definitions for each classic pipeline:
For each pipeline ID you collected, use this endpoint to get detailed build definitions including all steps and script content:GET https://dev.azure.com/{your-org}/{your-project}/_apis/build/definitions/{pipeline-id}?api-version=7.1-preview.7In the response, look under
process.phases.stepsarray. For bash tasks, you'll see entries wheretask.nameequalsBash, and the script content lives intask.inputs.script.Search for your target bash snippets:
Write a simple script (Python, PowerShell, etc.) to parse these JSON responses and check if thescriptfield contains the copy-pasted code you're targeting. Collect the pipeline name and ID whenever a match is found.
2. PowerShell Script for Bulk Scanning
If you prefer automation, a PowerShell script can handle the entire scanning workflow. Here's a simplified example (using a personal access token for authentication):
# Set your variables $orgUrl = "https://dev.azure.com/your-org" $projectName = "your-project" $pat = "your-personal-access-token" $targetSnippet = "your-copy-pasted-bash-code-snippet" # Encode PAT for authentication $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) # Get all classic pipelines $pipelinesUrl = "$orgUrl/$projectName/_apis/pipelines?api-version=7.1-preview.1" $pipelines = Invoke-RestMethod -Uri $pipelinesUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get $classicPipelines = $pipelines.value | Where-Object { $_.configuration.type -eq "designer" } # Scan each classic pipeline for the target snippet foreach ($pipeline in $classicPipelines) { $defUrl = "$orgUrl/$projectName/_apis/build/definitions/$($pipeline.id)?api-version=7.1-preview.7" $definition = Invoke-RestMethod -Uri $defUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get # Check all bash steps for the target snippet $bashSteps = $definition.process.phases.steps | Where-Object { $_.task.name -eq "Bash" } foreach ($step in $bashSteps) { if ($step.task.inputs.script -match $targetSnippet) { Write-Host "Match found in pipeline: $($pipeline.name) (ID: $($pipeline.id))" break } } }
Replace the placeholders with your org, project, PAT, and the bash snippet you're searching for, then run the script to get your list of pipelines to refactor.
3. Use Azure DevOps Analytics to List Classic Pipelines First
If you want a quick overview before deep scanning, use Analytics Views:
- Go to your Azure DevOps project, navigate to Analytics > Views > New View.
- Select Builds as the view type, then add a filter:
Definition TypeequalsClassic. - Add columns like
Definition Name,Definition ID, andCreated Dateto the view. - Export the view to a CSV file. Use this list to prioritize pipelines to scan, or feed it into your API/script workflow to target specific candidates.
4. Portal Search (Limited Use)
The Azure DevOps portal's search bar can help find pipelines by name, but it won't search inside pipeline definitions. If you know part of the pipeline name tied to the bash snippets, this can be a quick way to narrow down candidates before using the more thorough API/script methods.
Once you've compiled your list, you can go through each pipeline to refactor repetitive bash steps into external scripts. This approach ensures you don't miss any instances that need updating.
内容的提问来源于stack exchange,提问作者gaccardo

