Azure 模板导出与部署的模板不匹配

Azure template export does not match deployed template

我正在从 Azure DevOps 构建管道部署资源组,以便发布管道可以根据需要部署其余资源。是的,我在订阅级别部署,资源组创建没有问题。我的问题是,当我查看 Azure 资源管理器中的 "Export Template" 选项时,我的更改没有显示出来,最明显的是我的 contentVersion 没有更新,但是,我也注意到 $schema 也不一样.这是我的资源组模板:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "[parameters('semVer')]",
    "parameters": {
        "semVer": {
            "type": "string"
        },
        "resourceGroupName": {
            "type": "string"
        }
    },
    "resources": [
        {
            "name": "[parameters('resourceGroupName')]",
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2019-05-01",
            "location": "eastus",
            "tags": {
            },
            "properties": {
            }
        }
    ],
    "outputs": {
    },
    "functions": [
    ]
}

这是我正在使用的参数文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "semVer":{
            "value": "0.0.0.1"
        },
        "resourceGroupName":{
            "value": "rgName"
        }
    }
}

如我所说,我想更新内容版本,所以我在我的构建管道中使用了一个转换,它应该将 contentVersion 更新为程序集语义版本。我似乎正在转换,这是我的 azure-pipelines.yml:

name: $(date:yyyyMMdd)$(rev:.r)-$(Build.SourceBranchName)-$(GitVersion.SemVer)

trigger:
  - master
  - develop

stages:
- stage: DEV
  displayName: 'DEV'
  condition: and(always(), contains(variables['Build.SourceBranch'], 'develop'))
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    contentVersion: $(GitVersion.AssemblySemVer)
    parameters.semVer.value: $(GitVersion.AssemblySemVer)
    parameters.resourceGroupName.value: 'rgName-DEV'
  jobs:
    - job: DevResourceGroup
      steps:
      - task: GitVersion@5
        inputs:
          preferBundledVersion: false
          updateAssemblyInfo: true
          configFilePath: './GitVersion.yml'
      - script: echo %Action%%BuildVersion%
        displayName: 'Set Build Number to Semantic Version'
        env:
          Action: '##vso[build.updatebuildnumber]'
          BuildVersion: '$(GitVersion.SemVer)'
      - task: FileTransform@2
        inputs:
          folderPath: '$(Build.SourcesDirectory)'
          xmlTransformationRules: 
          jsonTargetFiles: './ResourceGroup/resourceGroup.parameters.json'
      - task: AzureResourceManagerTemplateDeployment@3
        inputs:
          deploymentScope: 'Subscription'
          azureResourceManagerConnection: 'ConnectionName'
          subscriptionId: 'GUID'
          location: 'East US'
          templateLocation: 'Linked artifact'
          csmFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.json'
          csmParametersFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.parameters.json'
          deploymentMode: 'Incremental'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.SourcesDirectory)'
          ArtifactName: 'develop'
          publishLocation: 'Container'

- stage: PROD
  displayName: 'PROD'
  condition: and(always(), contains(variables['Build.SourceBranch'],'master'))
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    contentVersion: $(GitVersion.AssemblySemVer)
    parameters.semVer.value: $(GitVersion.AssemblySemVer)
  jobs:
    - job: ProdResourceGroup
      steps:
      - task: GitVersion@5
        inputs:
          preferBundledVersion: false
          updateAssemblyInfo: true
          configFilePath: './GitVersion.yml'
      - script: echo %Action%%BuildVersion%
        displayName: 'Set Build Number to Semantic Version'
        env:
          Action: '##vso[build.updatebuildnumber]'
          BuildVersion: '$(GitVersion.SemVer)'
      - task: FileTransform@2
        inputs:
          folderPath: '$(Build.SourcesDirectory)'
          xmlTransformationRules: 
          jsonTargetFiles: './ResourceGroup/resourceGroup.parameters.json'
      - task: AzureResourceManagerTemplateDeployment@3
        inputs:
          deploymentScope: 'Subscription'
          azureResourceManagerConnection: 'ConnectionName'
          subscriptionId: 'GUID'
          location: 'East US'
          templateLocation: 'Linked artifact'
          csmFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.json'
          csmParametersFile: '$(Build.SourcesDirectory)/ResourceGroup/resourceGroup.parameters.json'
          deploymentMode: 'Incremental'
      - task: PublishBuildArtifacts@1
        inputs:
          PathtoPublish: '$(Build.SourcesDirectory)'
          ArtifactName: 'master'
          publishLocation: 'Container'

看来转换工作正常,因为 DEV 中的资源组确实附加了 DEV 文本。管道中的步骤还输出以下内容:

Applying JSON variable substitution for ./ResourceGroup/resourceGroup.parameters.json
Applying JSON variable substitution for /home/vsts/work/1/s/ResourceGroup/resourceGroup.parameters.json
Substituting value on key contentVersion with (string) value: 0.1.0.0
Substituting value on key value with (string) value: 0.1.0.0
Substituting value on key value with (string) value: rgName-DEV
JSON variable substitution applied successfully.

我担心的是,当我从 Azure 资源管理器导出模板时,模板看起来是这样的:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {},
    "variables": {},
    "resources": []
}

为什么我的contentVersion此时回到了默认值?我将如何验证将哪个版本的模板部署到环境中?如果只是要覆盖,contentVersion 变量有什么意义?

我想说的是,当您部署模板时,内容版本和架构都无关紧要。此外,导出模板与验证它是否有效无关。

导出模板是获取当前资源配置的一种方式(有点),如果您只是部署了一个模板,则导出模板没有任何意义,因为如果模板有效 - 您的配置已成功应用。