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

如何通过脚本/模板自动化创建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.

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:

  1. First, install Azure CLI and authenticate:

    az login
    az devops login --organization "https://dev.azure.com/<your-org>" # Use your PAT when prompted
    
  2. Create 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:

  1. Install the module:

    Install-Module -Name AzurePipelinePS -Scope CurrentUser -Force
    
  2. Connect to your Azure DevOps organization:

    Connect-APAccount -Organization "https://dev.azure.com/<your-org>" -PersonalAccessToken "your-PAT"
    
  3. 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"
    
  4. 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:2 for YAML pipelines instead of defining steps manually).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:51:57