如何使用ARM'输出'值另一个发布任务?

时间:2016-04-29 22:40:17

标签: azure azure-devops azure-resource-manager

我有一个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模板的参数。

6 个答案:

答案 0 :(得分:9)

用于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"

答案 1 :(得分:9)

VSTS Azure资源组部署任务现在已outputs section(自January 2018起)。因此,您可以在 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)的形式提供。

答案 2 :(得分:8)

捕获这个答案是因为我总是在搜索解决方案时最终会遇到这个问题。

There is a marketplace task使得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" 
    }
}

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

答案 3 :(得分:1)

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

语法是

moment([2016]).isoWeek(1).startOf('isoWeek').format('DD.MM.YYYY') // "02.01.2015"

您可以拥有一个内联的Powershell脚本,该脚本可以将所需的变量设置为尚未执行的任务。您可以像Write-Host "##vso[task.setvariable variable=myvariable;]myvalue"一样访问它。

您可能需要$(myvariable)变量到system.debug才能使用此功能。

阅读更多详情here

答案 4 :(得分:1)

首先,您定义Azure资源部署任务,并在这种情况下定义std::vector<int> seq; int pair; while(pair!=-1) { seq.push_back(letter[pair--]); } . . . int* row = new int[N]; // N is the input std::vector<std::vector<int>> vect; vect.resize(row[0]); for(int e=0;e<row[0];e++){ for(int elm=0;elm<seq.size();elm++) vect[e].push_back(outputs[seq[elm]][row[e+1]]); } sort(vect,row); // sort both?

enter image description here

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

enter image description here

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

Deployment outputs

在后续任务中,您可以通过以下方式访问环境变量:通过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在PowerShell脚本中运行(不适用于$env:varName

enter image description here

答案 5 :(得分:0)

您只需为“Azure资源组部署”任务添加输出变量名称,如下所示: enter image description here

然后在“PowerShell on Target Machines”任务中使用该变量: enter image description here

“目标计算机上的PowerShell”任务将使用“Azure资源组部署”任务中配置的资源: enter image description here

  

输出变量:

     

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

     

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

有关详细信息,请参阅此链接:Azure Resource Group Deployment Task

相关问题