Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

Web.config for multiple environment in TFS pipeline

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("pathoweb.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...