45

Ideally, I would want to configure our Azure Web App application settings using build variables (on VSTS), for example:

VSTS Build

We perform our publish tasks using Powershell scripts. In order to set app settings, the following script could be used:

param($websiteName, $appSettings)
Set-AzureWebsite -Name $websiteName -AppSettings $appSettings

I could pass these build variables manually into a Powershell script build task, like so:

PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings @{"MyConnectionString" = $(MyConnectionString);"MyRandomService" = $(MyRandomService);"MyRandomServiceClient"=$(MyRandomServiceClient);"MyRandomServicePassword"=$(MyRandomServicePassword)}

Is there a way to pass all build variables into a script without having to explicitly specifying each one in a hash table?

Harshil Lodhi
  • 7,274
  • 1
  • 33
  • 42
Dave New
  • 38,496
  • 59
  • 215
  • 394

4 Answers4

88

Build Variables are automatically passed to all the PowerShell scripts as environment variables.

So if you have defined a variable myVar in the Variables section. You can access it as $env:myVar in your script. One thing to note here is that . is converted to a _. For eg. if your variable is myVar.config, you will access it in your script as $env:myVar_config.

The available variables also include variables such as branch name, build number etc. To see all the available variables, run a dummy build/release definition and add a PowerShell task with inline type and run Get-ChildItem Env:. This will show you all the available environment variables and you can see all your custom defined variables.

More details are available here

Harshil Lodhi
  • 7,274
  • 1
  • 33
  • 42
  • 2
    Thanks - so I ended up setting all build variables, which I intend to become App Settings, with the "App:" prefix. It certainly isn't the cleanest of approaches. `Get-ChildItem Env: | Where-Object { $_.Name -like "App:*" } | ForEach-Object { $appSettings.Add($_.Name.Substring(4), $_.Value) }` – Dave New May 04 '16 at 04:12
  • @davenewza Yeah, it seems a good idea to put it in a dictionary if you have a lot of settings and then access them cleanly and easily. – Harshil Lodhi May 04 '16 at 05:15
  • another thing to not here is if you use hashes in your variable names (like `my-var`), you need to retrieve them by doing `(Get-Variable -Name "env:my-var").Value` – JED Feb 22 '23 at 17:35
9

It is worth mentioning here, that secret variables are not passed into scripts as environment variables (env: in PS). So accessible only if passed as parameters for a script, eg. -MyPassword $(Password). See https://learn.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch#secret-variables

Artyom
  • 3,507
  • 2
  • 34
  • 67
3

The variables have already been passed to PowerShell script when the build start. If I understand your question correctly, you want to use these variables together instead of specifying them one by one like following:

PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings $(AllVariables)

Then there isn't any way to do this.

If you want to reduce the strings passed to the PowerShell script, you can set the variable as following:

VariableName: MyRandomService | Value:"MyRandomService" = xxxxxxxx

Then you just need to call the PowerShell script with variable name passed.

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

You could have also used variable groups to achieve this, thus maintaining variables that are closely related, isolated from the build variables and from other groups of related variables. I have shown how to easily set this up in this answer.

ccoutinho
  • 3,308
  • 5
  • 39
  • 47