我可以使用 XML 变量替换在 DevOps 中更改 XML 文件中的特定属性值吗?
Can I change a specific attribute value in a XML file in DevOps using XML Variable Substitution?
我有两个环境,Dev 和 Prod,我正在使用 XML variable Substitution 根据环境更改值。
我想做的是更改特定属性的值。例如,我有以下内容:
<client>
<endpoint address="url_1" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
<endpoint address="url_2" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>
我想将 address
值替换为 url_3
,将第二个端点替换为 url_4
。所以,它看起来像这样:
<client>
<endpoint address="url_3" binding="binding_3" bindingConfiguration="configuration_3" contract="contract_3" name="thirdLink" />
<endpoint address="url_4" binding="binding_4" bindingConfiguration="configuration_4" contract="contract_4" name="fourthLink" />
</client>
在 DevOps 方面,有一个 key, value, environment
对,其中键是名称。
有没有办法使用相同的约定专门更改 address
?地址是我要更改的,我不允许安装任何外部扩展。
如您在 docs 中所见:
Variable substitution takes effect only on the applicationSettings, appSettings, connectionStrings, and configSections elements of configuration files. If you are looking to substitute values outside of these elements you can use a (parameters.xml) file, however you will need to use a 3rd party pipeline task to handle the variable substitution.
所以在你的情况下你可以使用 token replace
您只需要像这样在您的文件中放入标记:
<client>
<endpoint address="#{URL_1}#" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
<endpoint address="#{URL_1}#" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>
其中 URL_1
和 URL_2
是管道中定义的变量。
如文档中所述,变量替换仅适用于配置文件的某些特定元素。如果你要替换的配置文件不属于applicationSettings
、appSettings
、connectionStrings
、configSections
元素,并且不允许使用任何外部扩展,那么我认为您需要使用 File Transform.
您可以通过创建转换文件来转换配置文件中的值。相关语法请参考Web.config Transformation Syntax.
我们也可以使用内置的File Transform任务进行改造
要将 XML 转换应用于配置文件 (*.config),您必须使用以下语法指定以换行符分隔的转换文件规则列表:
-transform <path to the transform file> -xml <path to the source file> -result <path to the result file>
例如:
详情可以参考这篇。
默认情况下,如果 *.csproj
文件中的转换文件中已存在 <DependentUpon>
元素,MSBuild 会在生成 Web 包时应用转换。在这种情况下,Azure 应用服务部署任务将失败,因为没有对 Web.config
文件应用进一步的转换。因此,建议从所有转换文件中删除 <DependentUpon>
元素,以在使用 XML 转换时禁用任何构建时配置。
我有两个环境,Dev 和 Prod,我正在使用 XML variable Substitution 根据环境更改值。
我想做的是更改特定属性的值。例如,我有以下内容:
<client>
<endpoint address="url_1" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
<endpoint address="url_2" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>
我想将 address
值替换为 url_3
,将第二个端点替换为 url_4
。所以,它看起来像这样:
<client>
<endpoint address="url_3" binding="binding_3" bindingConfiguration="configuration_3" contract="contract_3" name="thirdLink" />
<endpoint address="url_4" binding="binding_4" bindingConfiguration="configuration_4" contract="contract_4" name="fourthLink" />
</client>
在 DevOps 方面,有一个 key, value, environment
对,其中键是名称。
有没有办法使用相同的约定专门更改 address
?地址是我要更改的,我不允许安装任何外部扩展。
如您在 docs 中所见:
Variable substitution takes effect only on the applicationSettings, appSettings, connectionStrings, and configSections elements of configuration files. If you are looking to substitute values outside of these elements you can use a (parameters.xml) file, however you will need to use a 3rd party pipeline task to handle the variable substitution.
所以在你的情况下你可以使用 token replace
您只需要像这样在您的文件中放入标记:
<client>
<endpoint address="#{URL_1}#" binding="binding_1" bindingConfiguration="configuration_1" contract="contract_1" name="firstLink" />
<endpoint address="#{URL_1}#" binding="binding_2" bindingConfiguration="configuration_2" contract="contract_2" name="secondLink" />
</client>
其中 URL_1
和 URL_2
是管道中定义的变量。
如文档中所述,变量替换仅适用于配置文件的某些特定元素。如果你要替换的配置文件不属于applicationSettings
、appSettings
、connectionStrings
、configSections
元素,并且不允许使用任何外部扩展,那么我认为您需要使用 File Transform.
您可以通过创建转换文件来转换配置文件中的值。相关语法请参考Web.config Transformation Syntax.
我们也可以使用内置的File Transform任务进行改造
要将 XML 转换应用于配置文件 (*.config),您必须使用以下语法指定以换行符分隔的转换文件规则列表:
-transform <path to the transform file> -xml <path to the source file> -result <path to the result file>
例如:
详情可以参考这篇
默认情况下,如果 *.csproj
文件中的转换文件中已存在 <DependentUpon>
元素,MSBuild 会在生成 Web 包时应用转换。在这种情况下,Azure 应用服务部署任务将失败,因为没有对 Web.config
文件应用进一步的转换。因此,建议从所有转换文件中删除 <DependentUpon>
元素,以在使用 XML 转换时禁用任何构建时配置。