Azure DevOps管道,Azure PowerShell 4. *脚本在“ ForEach -Parallel”中丢失了AzContext?

问题描述 投票:0回答:1

我们使用相同的代码部署了许多Azure功能应用程序。在我们的Azure DevOps管道中,我们有一个Azure PowerShell(4. *)脚本来部署代码并启动功能应用程序:

Param (
  [string]   $resourceGroupName,
  [string[]] $funcapps,
  [string]   $filePath
)

Write-Output "Deploying the following function apps: $funcapps";

foreach ($app in $funcapps) 
{ 
  Write-Output "Deploying function app: $app";
  $webapp = Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app -ArchivePath $filePath -Force;

  Write-Output "Starting function app: $app";
  $webapp = Start-AzWebApp -ResourceGroupName $resourceGroupName -Name $app;

  Write-Output "Started function app: $app = $($webapp.State)";
}

这很好(无论是从本地PowerShell还是从Azure DevOps都可以运行,但是随着我们正在部署的应用程序数量的增加,可能需要一段时间。为了尝试使其性能更好,我们尝试并行运行publish / start语句:

Param (
  [string]   $resourceGroupName,
  [string[]] $funcapps,
  [string]   $filePath
)

Workflow Parallel-Deploy {  
    Param (
      [string]   $resourceGroupName,
      [string[]] $funcapps,
      [string]   $filePath
    )

    Write-Output "Deploying the following function apps: $funcapps";

    foreach -parallel($app in $funcapps) 
    { 
      Write-Output "Deploying function app: $app";
      $webapp = Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app -ArchivePath $filePath -Force;

      Write-Output "Starting function app: $app";
      $webapp = Start-AzWebApp -ResourceGroupName $resourceGroupName -Name $app;

      Write-Output "Started function app: $app = $($webapp.State)";
    }
}

Parallel-Deploy -resourceGroupName $resourceGroupName -funcapps $funcapps -filePath $filePath

代码是相同的,只是移到工作流中以使用“ foreach -parallel”。如果我从本地PowerShell运行脚本,则一切正常-但是从Azure DevOps管道中,出现错误No account found in the context. Please login using Connect-AzAccount.

我发现对changes made in the Azure PowerShell DevOps task的引用,需要将上下文显式传递给后台任务。我尝试遵循列出的示例(Save-AzContext和Import-AzContext-从示例中的Save-AzureRmContext / Import-AzureRmContext更新),但它仍然给我相同的错误。

关于我在做什么的任何建议,以及如何在“ foreach -parallel”块内正确设置上下文?

azure-devops azure-powershell
1个回答
0
投票

保存\导入应该可以正常工作,并且只允许上下文自动保存Enable-AzContextAutosave

或者,您也可以仅具有将cmdlet作为作业启动的本机功能:

Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app `
   -ArchivePath $filePath -Force -AsJob

然后等待作业完成并启动Webapp。

© www.soinside.com 2019 - 2024. All rights reserved.