5

I'm trying to update a web.config app setting called 'AppVersion' with the build number when building my application in VSTS.

Here are my build steps:

build steps

The 'Replace tokens' step converts any variables you set for your build and replaces the tokens you've set in your config files. This part works but what it won't do is get an environment variable like the build number and do a replace. It will just replace whatever text has been specified. Here's my build variables:

build variables

So after the build step is completed, my app setting is...

<add key="AppVersion" value="$(BuildNumber)" />

when it should be something like...

<add key="AppVersion" value="20160520.1" />

Can anyone point me in the right direction? Many thanks.

Johnny Rambo
  • 213
  • 3
  • 9
  • Have you seen the top result on Google? Seems this is a deployment task. http://stackoverflow.com/questions/35639687/vsts-release-define-custom-variable-in-web-config-and-set-at-release-time – alan May 20 '16 at 12:14
  • Yes I've seen that question already but this is related to the build and not the release or deployment. My build is being packaged into a drop folder in order to be used by the release manager and so the build number should probably be set before it's sent to the drop folder; the build number won't change after the drop is completed. – Johnny Rambo May 20 '16 at 14:51

4 Answers4

12

I did something similar using the "Replace Tokens in **/*config" task.

To update the value for the key "AppVersion" with the current build number, your line should look like the following,

<add key="AppVersion" value="#{Build.BuildNumber}#" />
Robblis
  • 121
  • 3
1

You can add a PowerShell script task before "Replace Token" task to pass the "BuildNumber" to "AppVersion" variable as following. enter image description here

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
0

In VSTS, use $(Build.BuildNumber) as specified in this doc.

Note that you cannot use $(Build.BuildNumber) to set a variable's value, because it is taken literally; it should be an argument to the task. If your task does not accept it, you can replace with a little Powershell script and the BUILD_BUILDNUMBER environment variable.

param (
    [Parameter(Mandatory = $true)]
    [String]$fileWithTokens,

    [Parameter(Mandatory = $false)]
    [String]$tokenRegex = "__(\w+)__"
)

$vars = Get-ChildItem -path env:*
$contents = Get-Content -Path $fileWithTokens
$newContents = "";
$contents | % {
    $line = $_
    if ($_ -match $tokenRegex) {
        $setting = Get-ChildItem -path env:* | ? { $_.Name -eq $Matches[1]  }
        if ($setting) {
            Write-Host ("Replacing key {0} with value from environment" -f $setting.Name)
            $line = $_ -replace $tokenRegex, $setting.Value
        }
    }
    $newContents += $line + [Environment]::NewLine
}
Set-Content $fileWithTokens -Value $newContents

```

Source https://github.com/colindembovsky/cols-agent-tasks/tree/master/Tasks/ReplaceTokens

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
  • That doesn't work. All is does is replace my token with '$(Build.BuildNumber)' instead of something like '20160520.1'. – Johnny Rambo May 20 '16 at 14:43
0

After a day of research, finally found/created a better option than using any random app (Replace Token) from Marketplace.

The option I am talking is already available in VSTS, Azure CLI task.

Here are the stpes:

  1. Add setting BUILD_NUMBER with initial value of 1.0 in appsettings.json
  2. Read appsettings.json in your app and display it. I am sure you all are smart enough to figure out how to use appsettings to display Build Number on your WebApplication.
  3. In Azure Portal, similarly create an App Setting named BUILD_NUMBER with initial value of 1.0 in Azure Application settings section under App Services for your App.
  4. In VSTS, In your Release definition, add a task Azure CLI.
  5. Populate required fields such as Azure Subscription, Script Location with Inline script and last but most important Inline Script with following CLI command

az webapp config appsettings set -n iCoreTestApi -g ArchitectsSandbox -s Dev --settings BUILD_NUMBER=$(Build.BuildNumber)

Command explanation:

  • iCoreTestApi should be replaced by your real WebApp or Api name in Azure
  • ArchitectsSandbox should be replaced by your resource group in Azure
  • Dev is the slot name, you may or may not have it.
  • Rest of the command remains same.

Once you will queue new build, after successful completion of the deployment, you can see app settings section on Azure is updated with new BUILD_NUMBER.

Let me know if you still have any question.

Dhaval
  • 113
  • 7