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

时间:2019-12-07 03:19:14

标签: azure-devops azure-powershell

我们使用相同的代码部署了许多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”块内正确设置上下文?

编辑1 我可能应该确切地显示了我对SaveContext / ImportContext所做的...

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

$contextPath = "$tmpDir/context.json"
Save-AzContext -Path $contextPath" -Force

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

    foreach -parallel($app in $funcapps) 
    { 
      # Output context - initially not set
      Get-AzContext;

      # Fetch and display context - now set
      Import-AzContext -Path $contextPath;
      Get-AzContext; 

      Write-Output "Deploying function app: $app";
      $webapp = Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app -ArchivePath $filePath -Force;
...

这仍然给我一个错误,即在上下文中找不到该帐户。

根据建议,我改为使用工作:

foreach ($app in $funcapps) {
  $jobname = "$app-Job";
  Start-Job -Name $jobname -ScriptBlock {
    Param (
      [string]   $resourceGroupName,
      [string[]] $funcapps,
      [string]   $filePath,
      [string]   $contextPath
    )

    # Output context - initially not set
    Get-AzContext;

    # Fetch and display context - now set
    Import-AzContext -Path $contextPath;
    Get-AzContext; 

    Write-Output "Deploying function app: $app";
    $webapp = Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app -ArchivePath $filePath -Force;
...

这也表示上下文不正确。

1 个答案:

答案 0 :(得分:1)

保存\导入应该很好,并且只允许上下文自动保存Enable-AzContextAutosave

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

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

,然后等待作业完成并启动Web应用程序。