lundgrenjohan
lundgrenjohan

Reputation: 11

Change all Web.*.config files in first stage in release pipeline

I need to change the items inside appsettings on all the Web.*.config files in first stage step. That is I can't do transformation in every step in release pipeline. The reason is that I use Episerver DXC/DXP.

I have 4 stages; "Upload Package", "Integration", "Preproduction", and "Production".

The values is stored i Azure Key Vault.

Is there any smart way to do this?

Upvotes: 1

Views: 1281

Answers (3)

lundgrenjohan
lundgrenjohan

Reputation: 11

What I was trying to do was replace variables in config files from Azure Key Vault before transformation on config files because it can't be done (at this point) during the release pipeline when using Episerver DXC. What I did was replacing them during the build pipeline instead.

Made the variable substitution in Powershell during the build pipeline. Import the Key Vault secrets as separate task before the Powershell task, list all the one I would use as environment variables in the Powershell task.

The environment variables I named the same as the one it should replace in the config files (ex SomeApiKey_Integration). Go through the config files, look for two anything between two double underscores and replace them with value from the environment variable ((Get-ChildItem $variable).Value).

In the config files and environment variable they are named as previous stated, SomeApiKey_Integration. Key Vault name and environment variable value as SomeApiKey-Integration.

Upvotes: 0

Mengdi Liang
Mengdi Liang

Reputation: 19026

If File transformation is not suitable for your project, what about using powershell script to do the item change?

Sample:

Here is my example web.product.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="service.tasks" type="HRNetTaskService.TaskConfigurationSection, TaskService" />
  </configSections>
  <connectionStrings>
    <add name="Production" connectionString="xxxx" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="RestServiceUrl" value="https://sample.net" />   
  </appSettings> 
</configuration>

Now I want to update the connectionString of .config file. Add replace.ps1 into repos with below scripts, then call this replace.ps1 file in Powershell task via passing corresponding dynamic value:

Param(
[string]$source,
[string]$connectionstring
)
$webConfig = $source
$doc = (Get-Content $webConfig) -as [Xml]
$root = $doc.get_DocumentElement();
$activeConnection = $root.connectionStrings.SelectNodes("add");
$activeConnection.SetAttribute("connectionString", $connectionstring);
$doc.Save($webConfig)

enter image description here


Here $(ProductValue) is the variable that you configured in Azure key vault. Its call way is same with the pipeline variable. Just you need link the Azure key vault into azure devops, then combine it with Variable group.

Upvotes: 0

Related Questions