VSTS:将 build/release 变量传递给 Powershell 脚本任务
VSTS: Pass build/release variables into Powershell script task
理想情况下,我想使用构建变量(在 VSTS 上)配置我们的 Azure Web App 应用程序设置,例如:
我们使用 Powershell 脚本执行发布任务。为了设置应用程序设置,可以使用以下脚本:
param($websiteName, $appSettings)
Set-AzureWebsite -Name $websiteName -AppSettings $appSettings
我可以将这些构建变量手动传递到 Powershell 脚本构建任务中,如下所示:
PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings @{"MyConnectionString" = $(MyConnectionString);"MyRandomService" = $(MyRandomService);"MyRandomServiceClient"=$(MyRandomServiceClient);"MyRandomServicePassword"=$(MyRandomServicePassword)}
有没有一种方法可以将所有构建变量传递到脚本中,而不必在散列中显式指定每个变量table?
构建变量作为环境变量自动传递给所有 PowerShell 脚本。
因此,如果您在 Variables
部分定义了一个变量 myVar
。您可以在脚本中以 $env:myVar
的形式访问它。这里要注意的一件事是 .
被转换为 _
。例如。如果您的变量是 myVar.config
,您将在脚本中以 $env:myVar_config
.
的形式访问它
可用变量还包括分支名称、内部版本号等变量。要查看所有可用变量,运行 虚拟 build/release 定义并添加内联类型和 运行 Get-ChildItem Env:
。这将向您显示所有可用的环境变量,您可以看到所有自定义变量。
有更多详细信息here
构建开始时,变量已传递给 PowerShell 脚本。如果我正确理解你的问题,你想一起使用这些变量,而不是像下面这样一个一个地指定它们:
PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings $(AllVariables)
那就没办法了
如果要减少传递给 PowerShell 脚本的字符串,可以按如下方式设置变量:
VariableName: MyRandomService | Value:"MyRandomService" = xxxxxxxx
那么你只需要调用传递变量名的PowerShell脚本即可。
这里值得一提的是,秘密变量不会作为环境变量传递到脚本中(env:
in PS)。因此仅当作为脚本参数传递时才可访问,例如。 -MyPassword $(Password)
。参见 https://docs.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch#secret-variables
您也可以使用变量组来实现这一点,从而维护密切相关的变量,与构建变量和其他相关变量组隔离。我已经在 中展示了如何轻松设置它。
理想情况下,我想使用构建变量(在 VSTS 上)配置我们的 Azure Web App 应用程序设置,例如:
我们使用 Powershell 脚本执行发布任务。为了设置应用程序设置,可以使用以下脚本:
param($websiteName, $appSettings)
Set-AzureWebsite -Name $websiteName -AppSettings $appSettings
我可以将这些构建变量手动传递到 Powershell 脚本构建任务中,如下所示:
PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings @{"MyConnectionString" = $(MyConnectionString);"MyRandomService" = $(MyRandomService);"MyRandomServiceClient"=$(MyRandomServiceClient);"MyRandomServicePassword"=$(MyRandomServicePassword)}
有没有一种方法可以将所有构建变量传递到脚本中,而不必在散列中显式指定每个变量table?
构建变量作为环境变量自动传递给所有 PowerShell 脚本。
因此,如果您在 Variables
部分定义了一个变量 myVar
。您可以在脚本中以 $env:myVar
的形式访问它。这里要注意的一件事是 .
被转换为 _
。例如。如果您的变量是 myVar.config
,您将在脚本中以 $env:myVar_config
.
可用变量还包括分支名称、内部版本号等变量。要查看所有可用变量,运行 虚拟 build/release 定义并添加内联类型和 运行 Get-ChildItem Env:
。这将向您显示所有可用的环境变量,您可以看到所有自定义变量。
有更多详细信息here
构建开始时,变量已传递给 PowerShell 脚本。如果我正确理解你的问题,你想一起使用这些变量,而不是像下面这样一个一个地指定它们:
PrepareAppSettings.ps1 -websiteName "MyWebApp" -appsettings $(AllVariables)
那就没办法了
如果要减少传递给 PowerShell 脚本的字符串,可以按如下方式设置变量:
VariableName: MyRandomService | Value:"MyRandomService" = xxxxxxxx
那么你只需要调用传递变量名的PowerShell脚本即可。
这里值得一提的是,秘密变量不会作为环境变量传递到脚本中(env:
in PS)。因此仅当作为脚本参数传递时才可访问,例如。 -MyPassword $(Password)
。参见 https://docs.microsoft.com/en-us/vsts/pipelines/build/variables?view=vsts&tabs=batch#secret-variables
您也可以使用变量组来实现这一点,从而维护密切相关的变量,与构建变量和其他相关变量组隔离。我已经在