AC4*_*AC4 18 azure azure-resource-manager azure-devops
我有一个ARM模板,其中包含以下输出部分:
"outputs": {
"sqlServerFqdn": {
"type": "string",
"value": "[reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName]"
},
"primaryConnectionString": {
"type": "string",
"value": "[concat('Data Source=tcp:', reference(concat('Microsoft.Sql/servers/', variables('sqlserverName'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', variables('databaseName'), ';User Id=', parameters('administratorLogin'), '@', variables('sqlserverName'), ';Password=', parameters('administratorLoginPassword'), ';')]"
},
"envResourceGroup": {
"type": "string",
"value": "[parameters('hostingPlanName')]"
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个使用该模板的Azure资源组部署任务.然后我想在下一个任务中使用变量$(sqlServerFqdn)进行配置.该变量似乎不仅仅是填充,我无法找到告诉我如何在发布时使用"输出"值的任何地方.
在运行此ARM模板后,我需要做什么才能填充变量以用于配置任务?一个示例是powershell脚本任务或另一个ARM模板的参数.
ode*_*bas 13
VSTS Azure资源组部署任务现在具有输出部分(自2018年1月起).因此,您可以在Azure资源组部署任务的部署输出中设置变量名称,例如,并使用以下内联脚本添加PowerShell脚本任务:
ResourceGroupDeploymentOutputs
# Make outputs from resource group deployment available to subsequent tasks
$outputs = ConvertFrom-Json $($env:ResourceGroupDeploymentOutputs)
foreach ($output in $outputs.PSObject.Properties) {
Write-Host "##vso[task.setvariable variable=RGDO_$($output.Name)]$($output.Value.value)"
}
Run Code Online (Sandbox Code Playgroud)
在后续任务中,您可以使用模板变量.因此,例如,如果您sqlServerFqdn的模板中有变量,那么$(RGDO_sqlServerFqdn)在PowerShell脚本任务完成后它将可用.
Jos*_*osh 11
捕获这个答案,因为我总是在搜索解决方案时结束这个问题.
市场任务使得ARM模板输出参数在管道中进一步可用.但在某些情况下,您无权为订阅购买marketplace项目,因此以下PowerShell将执行相同的操作.要使用它,请在ARM模板资源组部署步骤之后立即将其添加为powershell脚本步骤.它将查看最后一次部署并将输出变量拉入管道变量.
param(
[string] $resourceGroupName
)
$lastDeployment = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName | Sort Timestamp -Descending | Select -First 1
if(!$lastDeployment) {
throw "Deployment could not be found for Resource Group '$resourceGroupName'."
}
if(!$lastDeployment.Outputs) {
throw "No output parameters could be found for the last deployment of Resource Group '$resourceGroupName'."
}
foreach ($key in $lastDeployment.Outputs.Keys){
$type = $lastDeployment.Outputs.Item($key).Type
$value = $lastDeployment.Outputs.Item($key).Value
if ($type -eq "SecureString") {
Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
}
else {
Write-Host "##vso[task.setvariable variable=$key;]$value"
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,环境变量在此脚本的上下文中不可用,但将在后续任务中使用.
用于Azure资源组部署的Visual Studio Team Services任务的UI上显示的输出值似乎仅适用于Eddie的答案中描述的场景,该场景适用于VM.实际上,如果您的部署不包含VM,您将收到如下错误:
资源组中找不到虚拟机:'MY-RESOURCE-GROUP-NAME'.无法在输出变量中注册环境:'myVariableName'.
对于非VM示例,我创建了一个在RG部署之后运行的powershell脚本.作为示例,此脚本获取资源组的输入变量$resourceGroupName以及所需的输出变量的名称$rgDeploymentOutputParameterName.您可以自定义和使用类似的东西:
#get the most recent deployment for the resource group
$lastRgDeployment = Get-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName |
Sort Timestamp -Descending |
Select -First 1
if(!$lastRgDeployment)
{
throw "Resource Group Deployment could not be found for '$resourceGroupName'."
}
$deploymentOutputParameters = $lastRgDeployment.Outputs
if(!$deploymentOutputParameters)
{
throw "No output parameters could be found for the last deployment of '$resourceGroupName'."
}
$outputParameter = $deploymentOutputParameters.Item($rgDeploymentOutputParameterName)
if(!$outputParameter)
{
throw "No output parameter could be found with the name of '$rgDeploymentOutputParameterName'."
}
$outputParameterValue = $outputParameter.Value
# From here, use $outputParameterValue, for example:
Write-Host "##vso[task.setvariable variable=$rgDeploymentOutputParameterName;]$outputParameterValue"
Run Code Online (Sandbox Code Playgroud)
小智 7
2020 年 11 月,在此提交之后 - https://github.com/microsoft/azure-pipelines-tasks/commit/1173324604c3f61ce52cdcc999f6d4d7ea9ab8f9,变量可以直接在管道中的后续任务中使用(不需要 powershell 脚本!!)
步骤是这样的 -
在 ARM 模板部署任务中,为“高级”下拉列表下的“部署输出”部分指定任何引用名称。就我而言,我给出了armOutputVariable。
现在要在后续任务中使用sqlServerFqdn的值,只需按以下方式使用$(armOutputVariable.sqlServerFqdn.value)
例如,假设我想使用它来覆盖部署后的测试任务中的一个参数,以便我可以按以下方式使用它 - 示例图像
总而言之,ARM 中的所有输出都可以通过这种方式直接在后续步骤中使用(确保在 ARM 模板部署步骤中分配引用名称)-
$(armOutputVariable.sqlServerFqdn.value)
$(armOutputVariable.sqlServerFqdn.type)
$(armOutputVariable.primaryConnectionString.value)
$(armOutputVariable.primaryConnectionString.type)
$(armOutputVariable.envResourceGroup.value)
$(armOutputVariable.envResourceGroup.type)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11151 次 |
| 最近记录: |