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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…