VSTS:将构建/发布变量传递到 Powershell 脚本任务 [英] VSTS: Pass build/release variables into Powershell script task
问题描述
理想情况下,我希望使用构建变量(在 VSTS 上)配置我们的 Azure Web App 应用程序设置,例如:
Ideally, I would want to configure our Azure Web App application settings using build variables (on VSTS), for example:
我们使用 Powershell 脚本执行发布任务.为了设置应用程序设置,可以使用以下脚本:
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
我可以将这些构建变量手动传递到 Powershell 脚本构建任务中,如下所示:
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)}
有没有一种方法可以将所有构建变量传递到脚本中,而无需在哈希表中明确指定每个变量?
推荐答案
构建变量会自动作为环境变量传递给所有 PowerShell 脚本.
Build Variables are automatically passed to all the PowerShell scripts as environment variables.
因此,如果您在 Variables
部分定义了变量 myVar
.您可以在脚本中以 $env:myVar
的形式访问它.这里需要注意的一点是 .
被转换为 _
.例如.如果您的变量是 myVar.config
,您将在脚本中以 $env:myVar_config
的形式访问它.
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
.
可用变量还包括分支名称、内部版本号等变量.要查看所有可用变量,请运行虚拟构建/发布定义并添加内联类型的 PowerShell 任务并运行 Get-ChildItem Env:代码>.这将显示所有可用的环境变量,并且您可以看到所有自定义变量.
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.
更多详细信息可在这里
这篇关于VSTS:将构建/发布变量传递到 Powershell 脚本任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!