Manu Padmanabhan
Manu Padmanabhan

Reputation: 555

Update Build number in App config xml file on build pipeline

I have a build pipeline in Azure DevOps, I need to update the build number in my apconfig exe file that will be $(Build.BuildNumber).

I just tried this way:

Adding a variable name = BuildNumber value = $(Build.BuildNumber).

And in my apconfig.exe file have a key same like <add key="BuildNumber" value="1812201901" />.

Why I have tried like this way: thinking like it will update in the config file if variable name match with the key.

But it is not working. can anyone please help? I have just started in CI/CD.

Upvotes: 1

Views: 1780

Answers (2)

Leo Liu
Leo Liu

Reputation: 76760

Update Build number in App config xml file on build pipeline

Just like the Shayki said, using the Replace Tokens extension should be the directly way to resolve this issue.

But since you need to request to get this extension, as workaround, you could also use power shell scripts to resolve this issue, you can check below my test powershell scripts:

$currentDirectory = [IO.Path]::GetDirectoryName($MyInvocation.MyCommand.Path)

$appConfigFile = [IO.Path]::Combine($currentDirectory, 'App.config')

$appConfig = New-Object XML

$appConfig.Load($appConfigFile)

foreach($BuildNumber in $appConfig.configuration.add)
{
    'name: ' + $BuildNumber.name
    'BuildNumber: ' + $BuildNumber.value
    $BuildNumber.value = '123456789'
}
$appConfig.Save($appConfigFile)

As result, the app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <add key="BuildNumber" value="123456789" />
</configuration>

Note: Set the powershell scripts at the same folder of the app.config file.

Hope this helps.

Upvotes: 2

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41555

You can use the Replace Tokens extension and in the apconfig.exe file put this:

<add key="BuildNumber" value="__BuildNumber__" />

Configure the task to search variables with __ prefix and suffix:

enter image description here

Now the value will be replaced with the value of the BuildNumber variable you configured (equal to Build.BuildNumber).

Upvotes: 1

Related Questions