Azure - 未找到与请求 URI 匹配的 HTTP 资源

问题描述

我正在使用 YAML 管道从 Azure Devops 创建一个资源组。下面是我在管道中使用的 Azure 任务的片段。

- task: AzureResourceGroupDeployment@2
  displayName: 'Deploy RG'
  inputs:
    azureSubscription: 'my-servicePrinciple (SPN)'
    resourceGroupName: 'test-rg-001'
    location: 'westeurope'
    templateLocation: 'Linked artifact'
    csmFile: $(Pipeline.Workspace)/arm_templates/ResourceGroup/template.json
    csmParametersFile: $(Pipeline.Workspace)/arm_templates/ResourceGroup/parameter.json
    deploymentMode: Incremental

这是我的 template.json 文件内容

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
        "rgName": {
            "type": "string"
        },"rgLocation": {
            "type": "string"
        },"tags": {
            "type": "object","defaultValue": {}
        }
    },"variables": {},"resources": [
        {
            "type": "Microsoft.Resources/resourceGroups","apiVersion": "2020-06-01","location": "[parameters('rgLocation')]","name": "[parameters('rgName')]","properties": {},"tags": "[parameters('tags')]"
        }
    ],"outputs": {}
}

管道失败并出现以下错误

No HTTP resource was found that matches the request URI 'https://management.azure.com/subscriptions/xx4873gx-4yy1-4092-9a70-34812353vv77/resourcegroups/test-rg-001/providers/Microsoft.Resources/resourceGroups/test-rg-001?api-version=2020-06-01'

我在互联网上寻找导致它的原因,但没有希望。我也尝试过在我的 template.json 中使用以下架构,但也不起作用。

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#"

奇怪的是,管道创建了我想要的资源组,但我无法理解部署失败的原因。为了获得更详细的信息,我查看了门户中的部署菜单,这是扩展错误

{
  "code": "DeploymentFailed","message": "At least one resource deployment operation Failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details": [
    {
      "message": "No HTTP resource was found that matches the request URI 'https://management.azure.com/subscriptions/xx4873gx-4yy1-4092-9a70-34812353vv77/resourcegroups/test-rg-001/providers/Microsoft.Resources/resourceGroups/test-rg-001?api-version=2020-06-01'."
    }
  ]
}

对于 ARM 模板,我只想创建资源组,不想包括任何其他资源的部署。 有人可以闻到,这里的烹饪有什么问题吗? :D

解决方法

您看到上述错误是因为您试图在管道中的资源组内创建资源组。

在您的管道中使用上述任务 AzureResourceGroupDeployment 配置。您正在资源组 test-rg-001 中创建资源组(即 test-rg-001)。如果它没有找到您在 resourceGroupName 参数中指定的资源组(即 test-rg-001),它将自动创建一个资源组。这就是您看到资源组 test-rg-001 被奇怪地创建的原因。请参阅此similar issue

您可以使用任务 AzureResourceManagerTemplateDeploymentDeployment scope 设置为 Subscription 而不是 Resource Group。见下文:

steps:
- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'ARM Template deployment: Subscription scope'
  inputs:
    deploymentScope: Subscription
    azureResourceManagerConnection: 'my-servicePrinciple (SPN)'
    location: 'West Europe'
    csmFile: $(Pipeline.Workspace)/arm_templates/ResourceGroup/template.json
    csmParametersFile: $(Pipeline.Workspace)/arm_templates/ResourceGroup/parameter.json
    deploymentMode: Incremental