0

I have a web project which deployed over 50+ servers as per client and regions, each webconfig have 600+ values which are specific for each client and environment. means WebConfig have 600+ appsetting tags and and each client and environment have their own values. right now its manual if any value change than release team will go manually on server and do add/update value in webconfig. I want to handle it in

tfs release pipeline by tokenize webconfig's values but again it not an easy task to create each client's variables groups., which will create 50 group variables and each group variables will have 600 values. 50x600 = 30,000

is there an easy way to update webconfig values at time of per client release in TFS pipeline

Thanks in advance

Saad Awan
  • 566
  • 2
  • 9
  • 23

1 Answers1

1

You can try using FileTransform task in the release pipeline to update the webconfig values.

Variables defined in the build or release pipelines 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.

FileTransform task will match the pipeline variables against the key or name entries in the appsettings section. And update the appsettings value of webconfig with the matched pipeline variables.

So you can define a pipeline variable with the same name of the key or name entries of the appsetting which needs to be changed. When new release is created, the FileTransform task will change the appsetting with the value of the pipeline variable.

You can also use Magic Chuncks task to update the webconfig appsettings.

Check here to learn how to transform xml file with magic chucks

Please check this similar thread for more information.

Update:

Since it is a lot of task to manually create all the variables, you can have a try using Variablegroups - Add rest api to do this.

POST https://{instance}/{collection}/{project}/_apis/distributedtask/variablegroups?api-version=4.1-preview.1

You need to write scripts to read all the appsetttings first, and then add all the appsettings to the rest api request body. See below example:

For example, I have below appsettings in web.config.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings file="custom.config">
    <add key="ApplicationName" value="MyApplication" />
    <add key="ApplicationName2" value="MyApplication2" />
    <add key="ApplicationName3" value="MyApplication3" />
  </appSettings>
</configuration>

First I need to write script to add all the keys and values to the request body. Then call rest api. See below example:

$body=@{
  "variables"= @{};
  "type"= "Vsts";
  "name"= "VariableGroup1";  #variable Group Name
  "description"= "A test variable group"
}

$appConfig = New-Object XML 

$appConfig.Load("path\to\web.config") 

$settigs = $appConfig.configuration.appsettings

foreach($setting in $settigs.add) {

  $body.variables[$setting.key]=@{value=$setting.value}

}
# Call rest api to create variable group

$url = "https://{instance}/{collection}/{project}/_apis/distributedtask/variablegroups?api-version=4.1-preview.1"
$PAT="Personal access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))

$bodyjson = $body | convertto-json

$pipeline1 = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method post -ContentType "application/json" -Body $bodyjson

By using scripts you donot need to create the variable group manually.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • I think there is some confusion to understand my situation, as mentioned in the question that I am already using the extension 'Replace Tokens' for this I first tokenized the webconfig which is around 600 values after that I create 50 different pipeline variable groups (for each environment), as said the its not an easy tasks to create the 50*600=30000 values, is some other stuff which will do some magic – Saad Awan Jan 12 '21 at 05:54
  • @SaadAwan I cannot think of an easy way to achieve this. Actually you donot have to pre-create all the variables in the pipeline. You just need to create new variables that need be to changed in appsettings. If you need to create all the variables at one time. You can make use the `Variable group` Clone option. You can create a variable group with 600 variables first and then clone this variable group to 50 variable groups. – Levi Lu-MSFT Jan 12 '21 at 06:37
  • As I am using TFS 2018, CLONE option is not available in variable group. – Saad Awan Jan 12 '21 at 06:40
  • @SaadAwan I think you can try using rest api to create the variable group with all the appsettings. See above update. – Levi Lu-MSFT Jan 13 '21 at 02:33