使用Azure Devops Pipeline将标签带有空格传递到ARM模板

如何解决使用Azure Devops Pipeline将标签带有空格传递到ARM模板

我正在使用Azure DevOps Pipelines部署ARM模板。我的模板有一个我使用AzureResourceManagerTemplateDeployment@3传递到管道的标签参数。

我的ARM模板在parameters部分中有一个值作为对象。 tags是一个对象,这是许多示例模板显示的内容:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
    "resourceName": {
      "type": "string","metadata": {
        "description": "Specifies the name of the resource,including its prefix."
      }
    },"tags": {
      "type": "object","defaultValue": {
        "Cost Center": "Admin"
      }
    }
  },"resources": [
    {
      "apiVersion": "2019-06-01","kind": "StorageV2","location": "[resourceGroup().location]","name": "[parameters('resourceName')]","properties": {
        "supportsHttpsTrafficOnly": true
      },"sku": {
        "name": "Standard_LRS"
      },"type": "Microsoft.Storage/storageAccounts","tags": "[parameters('tags')]"
    }
  ]
}

[已编辑,以匹配后面的线程]

我正在将ubuntu-latest用于我的游泳池。标签可能有空格。

在为简单起见的管道中,我将标签设置为变量。

pool:
  vmImage: 'ubuntu-latest'
variables:
  - name: tags
    value: ("Location Region=West US 2" "Environment=${{ parameters.environment }}")

调用模板部署时,我将标签作为overrideParameters

传递
  - task: AzureResourceManagerTemplateDeployment@3
    displayName: "Deploy my templateaccount"
    inputs:
      deploymentScope: 'Resource Group'
      azureResourceManagerConnection: 'ResourceManager-connection'
      subscriptionId: ${{ parameters.subscriptionid }}
      action: 'Create Or Update Resource Group'
      resourceGroupName: '$(resourceGroupName)'
      location: '${{ parameters.location }}'
      templateLocation: 'Linked artifact'
      csmFile: 'mytemplatelocation/azuredeploy.json'
      overrideParameters: -resourceName abcdefg76534 -tags "$(tags)"
      deploymentMode: 'Incremental'
      deploymentOutputs: resourceOutput
  - pwsh: Write-Output '$(resourceOutput)'

到目前为止,我还不了解Ubuntu上的AzureResourceManagerTemplateDeployment@3如何期望标签被发送。

在每种情况下,模板都无法部署。

Azure DevOps Pipeline是否支持此方案?

有人提出建议吗?

解决方法

在Azure DevOps管道AzureResourceManagerTemplateDeployment@3中工作的标签的格式是将JSON用于ARM模板对象,例如标签。

  • 左花括号,用冒号分隔键值对,并用逗号分隔标签。
  • 在我的案例中,每个键和值都用引号引起来。
  • 右花括号。

但是,以下模板通过传入JSON对象来工作:{"Cost Center":"DevTest","Location":"West US"}作为模板参数。在上下文中,这看起来像:

- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'ResourceManager-connection'
    subscriptionId: 'XXXXX'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'rg-wus2-exampletest'
    location: 'West US 2'
    templateLocation: 'Linked artifact'
    csmFile: 'storageaccount/example.azuredeploy.json'
    csmParametersFile: 'storageaccount/azuredeploy.parameters.json'
    overrideParameters: '-resourceName oweruhsow -resourceTags {"Cost Center":"DevTest","Location":"West US"}'
    deploymentMode: 'Complete'

此管道模块期望使用JSON对象,而不是通过命令行部署PowerShell(https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resource)所使用的其他格式。

另外,作为其他说明,其他帖子也建议您使用tags以外的名称作为标签参数。对我有用的是resourceTags。这是我的ARM模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#","contentVersion": "1.0.0.0","parameters": {
    "resourceName": {
      "type": "string","metadata": {
        "description": "Specifies the name of the resource"
      }
    },"location": {
      "type": "string","defaultValue": "[resourceGroup().location]","metadata": {
        "description": "Location for the resources."
      }
    },"resourceTags": {
      "type": "object","defaultValue": {
        "Cost Center": "Admin"
      }
    }
  },"resources": [
    {
      "apiVersion": "2019-06-01","kind": "StorageV2","location": "[parameters('location')]","name": "[parameters('resourceName')]","properties": {
        "supportsHttpsTrafficOnly": true
      },"sku": {
        "name": "Standard_LRS"
      },"type": "Microsoft.Storage/storageAccounts","tags": "[parameters('resourceTags')]"
    }
  ]
}

如果要将模板对象设置为变量,则可以使用DevOps变量(例如$(tags))将其传入:

variables:
  tags: '{"Cost Center":"DevTest","Location":"West US"}'
steps:
- task: AzureResourceManagerTemplateDeployment@3
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: 'ResourceManager-connection'
    subscriptionId: '9f241d6e-16e2-4b2b-a485-cc546f04799b'
    action: 'Create Or Update Resource Group'
    resourceGroupName: 'rg-wus2-exampletest'
    location: 'West US 2'
    templateLocation: 'Linked artifact'
    csmFile: 'storageaccount/example.azuredeploy.json'
    csmParametersFile: 'storageaccount/azuredeploy.parameters.json'
    overrideParameters: '-resourceName oweruhso11w -resourceTags $(tags)'
    deploymentMode: 'Complete'

(作为旁注)由于某种原因,该模块需要具有csmParametersFile,否则它将因所有大写字母RESOURCEGROUP失败而失败。从命令行部署不需要param文件,但是Pipelines模块似乎确实需要它。 一个csmParamters文件,几乎没有任何内容,但似乎是必需的。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#","parameters": { }
  }

这也可以使用pool image: windows-latest

非常感谢https://github.com/MicrosoftDocs/azure-devops-docs/issues/9051

中的ToMakesSense ,

如果您使用的参数值包含多个单词,即使您使用变量传递它们,也请用引号引起来。

例如,-name“参数值” -name2“ $(变量)”。

尝试使用以下格式:

overrideParameters: -location "${{ parameters.location }}" -tags "$(tags)"

更多详细信息,请参阅此处的官方文档-Azure Resource Group Deployment task

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-