繁体   English   中英

如何在另一个发布任务中使用ARM“输出”值?

[英]How do I use ARM 'outputs' values another release task?

我有一个包含和输出部分的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')]"
    }
}

我有一个使用模板的Azure资源组部署任务。 然后,我想在下一个任务中使用变量$(sqlServerFqdn)进行配置。 该变量似乎不仅仅在填充,而且我找不到任何地方告诉我如何在发布时使用“输出”值。

在此ARM模板运行之后,我需要怎么做才能填充变量以用于配置任务? 一个示例就是Powershell脚本任务或另一个ARM模板的参数。

VSTS Azure资源组部署任务现在具有“ 输出”部分 (自20181月开始 )。 因此,您可以在“ Azure资源组部署”任务的“ 部署输出 ”中将变量名设置为例如ResourceGroupDeploymentOutputs并使用以下内联脚本添加PowerShell脚本任务:

# 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)"
}

在随后的任务中,您可以使用模板变量。 因此,例如,如果模板中包含sqlServerFqdn变量,则在完成PowerShell脚本任务后,该变量将以$(RGDO_sqlServerFqdn)形式提供。

捕获此答案是因为在寻找解决方案时,我总是会遇到这个问题。

有一个市场任务使ARM模板输出参数在管道中进一步可用。 但是在某些情况下,您无权购买用于订阅的市场项目,因此以下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" 
    }
}

请注意,环境变量在此脚本的上下文中将不可用,但在后续任务中将可用。

UI资源上显示的用于Azure资源组部署的Visual Studio Team Services任务的输出值似乎仅适用于Eddie的答案中所述的方案,该方案适用于VM。 实际上,如果您的部署中不包括虚拟机,则会出现类似以下错误:

在资源组“ MY-RESOURCE-GROUP-NAME”中找不到虚拟机。 无法在输出变量“ myVariableName”中注册环境。

对于非VM示例,我创建了一个Powershell脚本,该脚本在RG部署后运行。 以该脚本为例,它使用资源组$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"

VSTS允许在Powershell脚本中设置变量,您可以在其他任务中使用该变量。

语法是

Write-Host "##vso[task.setvariable variable=myvariable;]myvalue"

您可以拥有一个内联Powershell脚本,该脚本可以设置所需的变量以使用尚未执行的任务。您可以像$(myvariable)这样访问它。

您可能需要将system.debug变量设置为true才能使用此功能。

在此处阅读更多详细信息。

首先,您定义Azure资源部署任务,在这种情况下, Deployment outputs

在此处输入图片说明

在下一步中,您将创建一个PowerShell任务,该任务将上面定义的Deployment outputs作为输入参数

在此处输入图片说明

PowerShell脚本如下所示,并为ARM模板中定义的每个输出分配一个单独的VSTS环境变量,其名称与ARM模板输出部分中定义的名称相同。 这些变量然后可以在后续任务中使用。

param (
    [Parameter(Mandatory=$true)]
    [string]
    $armOutputString
)

Write-Host $armOutputString
$armOutputObj = $armOutputString | convertfrom-json
Write-Host $armOutputObj

$armOutputObj.PSObject.Properties | ForEach-Object {
    $type = ($_.value.type).ToLower()
    $key = $_.name
    $value = $_.value.value

    if ($type -eq "securestring") {
        Write-Host "##vso[task.setvariable variable=$key;issecret=true]$value"
        Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
    } elseif ($type -eq "string") {
        Write-Host "##vso[task.setvariable variable=$key]$value"
        Write-Host "Create VSTS variable with key '$key' and value '$value' of type '$type'!"
    } else {
        Throw "Type '$type' not supported!"
    }
}

在后续任务中,您可以通过以下方式访问环境变量:通过'$(varName)'将它们作为参数传递(这也适用于SecureString ),或者例如通过$env:varName在PowerShell脚本中使用(对于SecureString无效)

在此处输入图片说明

您只需要为“ Azure资源组部署”任务添加输出变量名称,如下所示: 在此处输入图片说明

然后在“目标计算机上的PowerShell”任务中使用变量: 在此处输入图片说明

“目标计算机上的PowerShell”任务将使用在“ Azure资源组部署”任务中配置的资源: 在此处输入图片说明

输出变量:

现在,Azure资源组任务的创建/更新操作将在执行期间生成一个输出变量。 输出变量可用于在后续任务中引用资源组对象。 例如,“目标计算机上的PowerShell”任务现在可以将资源组输出变量称为“ $(variableName)”,以便它可以在资源组VM目标上执行powershell脚本。

限制:执行期间产生的输出变量将包含有关VM主机名和(公共)端口(如果有)的详细信息。 在后续任务中将明确提供连接到VM主机的凭据。

有关更多详细信息,请参考此链接: Azure资源组部署任务

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM