如何通过脚本/模板自动化创建Azure流水线及解决REST API报错
Hey there! Let's break down how to automate creating Azure DevOps Build and Release pipelines, fix that REST API error you ran into, and explore the most reliable approaches for your workflow.
1. Fixing the REST API ArgumentNullException
The error you got (Value cannot be null. Parameter name: definition.Steps[0]) happens because you’re configuring the pipeline like a classic (GUI-based) pipeline, but you want to use a YAML file stored in Git. For YAML pipelines, you don’t need to define steps in the JSON payload—instead, you point to the YAML file in your repo.
Here’s a corrected build.json for a YAML-based build pipeline:
{ "name": "Automated-YAML-Build", "type": "build", "queue": { "name": "Hosted Ubuntu 22.04" // Use your preferred agent pool }, "repository": { "id": "<your-repo-id>", // Find this in Azure DevOps repo settings "type": "Git", "name": "<your-repo-name>", "url": "https://dev.azure.com/<your-org>/<your-project>/_git/<your-repo>", "defaultBranch": "main" }, "process": { "yamlFilename": "azure-pipelines.yml", // Path to your YAML in the repo "type": 2 // 2 = YAML pipeline type (1 = classic) }, "variables": {} }
Then use this curl command to create the pipeline:
curl -X POST -u "your-username:your-PAT" \ "https://dev.azure.com/<your-org>/<your-project>/_apis/build/definitions?api-version=7.1-preview.7" \ -H "Content-Type: application/json" \ -d @build.json
Replace <your-org>, <your-project>, and other placeholders with your actual values, and use a valid Azure DevOps Personal Access Token (PAT) with Build Definition permissions.
2. Azure CLI (Most Recommended for Simplicity)
Azure CLI has native commands for creating pipelines, which avoids manual JSON wrangling and directly integrates with your Git-hosted YAML. Here’s how to use it:
First, install Azure CLI and authenticate:
az login az devops login --organization "https://dev.azure.com/<your-org>" # Use your PAT when promptedCreate a YAML build pipeline in one command:
az pipelines create \ --name "Automated-YAML-Build" \ --repository "https://dev.azure.com/<your-org>/<your-project>/_git/<your-repo>" \ --branch main \ --yml-path azure-pipelines.yml \ --organization "https://dev.azure.com/<your-org>" \ --project "<your-project>" \ --queue "Hosted Ubuntu 22.04"
For release pipelines, you can use the CLI too. If you have a release definition JSON template, run:
az pipelines release definition create \ --name "Automated-Release" \ --definition-path "release-definition.json" \ --organization "https://dev.azure.com/<your-org>" \ --project "<your-project>"
3. AzurePipelinePS PowerShell Module
If you prefer PowerShell, AzurePipelinePS simplifies interacting with Azure DevOps APIs. Here’s how to use Get-APBuildDefinition and Publish-APBuildDefinition:
Install the module:
Install-Module -Name AzurePipelinePS -Scope CurrentUser -ForceConnect to your Azure DevOps organization:
Connect-APAccount -Organization "https://dev.azure.com/<your-org>" -PersonalAccessToken "your-PAT"Use an existing build definition as a template (or create a new object):
# Grab an existing YAML pipeline to use as a base $templateDef = Get-APBuildDefinition -Project "<your-project>" -DefinitionId 123 # Customize the template $templateDef.Name = "New-Automated-Build" $templateDef.Process.YamlFilename = "azure-pipelines.yml" # Update if needed $templateDef.Repository.DefaultBranch = "main"Publish the new pipeline:
Publish-APBuildDefinition -Project "<your-project>" -Definition $templateDef
For release pipelines, use Get-APReleaseDefinition and Publish-APReleaseDefinition following the same pattern.
Final Recommendations
- Azure CLI is the easiest and most maintainable option for most workflows—no complex JSON or script setup required, and it’s directly supported by Microsoft.
- AzurePipelinePS is great if you’re already using PowerShell for automation and need more granular control over pipeline configurations.
- REST API works but requires careful attention to the payload structure (remember to use
type:2for YAML pipelines instead of defining steps manually).
内容的提问来源于stack exchange,提问作者Matrix

