Azure Devops Release Pipeline XML Variable Substitution not working
Asked Answered
T

2

7

I have a very simple variable substitution in my release pipeline, but it's not working. I have the variable in the connectionStrings.config file as such:

<?xml version="1.0"?>
<connectionStrings>
  <add name="ExpenseDBConnectionString" connectionString="__ProdConnString__" providerName="System.Data.SqlClient" />
</connectionStrings>

I then have the variable defined in the release pipeline:

enter image description here

I also have the XML Variable Substitution enabled in the Deploy Task:

enter image description here

But I run the release, and the variable doesn't get substituted. I get this message in the logs:

2020-02-28T19:57:26.1262198Z Initiated variable substitution in config file : D:\a\_temp\temp_web_package_875508750741006\Content\D_C\a\1\s\Expenses.Web\obj\Release\Package\PackageTmp\App_Config\ConnectionStrings.config
2020-02-28T19:57:26.1312311Z Processing substitution for xml node : connectionStrings
2020-02-28T19:57:26.1321602Z Skipped Updating file: D:\a\_temp\temp_web_package_875508750741006\Content\D_C\a\1\s\Expenses.Web\obj\Release\Package\PackageTmp\App_Config\ConnectionStrings.config

This should be pretty simple, so not sure what setting I am missing. Help!

Trauma answered 28/2, 2020 at 20:10 Comment(0)
S
12

You should define your variable name as ExpenseDBConnectionString in the release pipeline. Below is the description of the setting XML variable substitution. The variables are matched against the key or name entries

Variables defined in the build or release pipeline will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of any config file and parameters.xml. Variable Substitution is run after config transforms.

So the variable defined should be like below:

enter image description here

Below screenshot is the result from my test release, you can see the connectionString was replaced.

enter image description here

For more information about XML variable substitution, please check it out here.

There are also some third party substitution tools(ie. Magic Chunks) that you can use to replace your config settings. Please check out the example for this thread.

Semaphore answered 2/3, 2020 at 3:44 Comment(1)
Thanks - It wasn't clear to me, as I had seen other variable substitutions that match any names. Didn't realize it was from a third party.Trauma
S
-1
 XML transformation will be run on the *.config file for transformation configuration files named *.Release.config or *.<stage>.config

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/transforms-variable-substitution?view=azure-devops&tabs=Classic#xml-transformation

That should be your answer? Your file name is not like that.

Shrier answered 28/2, 2020 at 20:32 Comment(3)
This is not a transformation - this is a 'substitution'.Trauma
ah sorry, my bad. Maybe it only works when the <connectionStrings> in inside a <configuration> section? learn.microsoft.com/en-us/azure/devops/pipelines/tasks/…Shrier
alternatively, use something like this: marketplace.visualstudio.com/items?itemName=qetza.replacetokens I use it in various pipelines and am pretty happy with itShrier

© 2022 - 2024 — McMap. All rights reserved.