Azure devops Azure DevOps管道,Azure PowerShell 4.*脚本在“中丢失AzContext”;ForEach-平行“吗;?
我们有许多Azure function应用程序要部署,使用相同的代码。在我们的Azure DevOps管道中,我们有一个Azure PowerShell(4.*)脚本来部署代码并启动函数应用程序:Azure devops Azure DevOps管道,Azure PowerShell 4.*脚本在“中丢失AzContext”;ForEach-平行“吗;?,azure-devops,azure-powershell,Azure Devops,Azure Powershell,我们有许多Azure function应用程序要部署,使用相同的代码。在我们的Azure DevOps管道中,我们有一个Azure PowerShell(4.*)脚本来部署代码并启动函数应用程序: Param ( [string] $resourceGroupName, [string[]] $funcapps, [string] $filePath ) Write-Output "Deploying the following function apps: $funcap
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管道,我得到一个错误,在上下文中找不到帐户。请使用Connect AzaAccount登录。
我已经找到了对上下文的引用,上下文需要显式地传递给后台任务。我尝试按照列出的示例(示例中的Save-AzureRmContext和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;
...
这也说明上下文不正确。保存\导入应该可以正常工作,并且只允许上下文自动保存
启用AzContextAutosave
或者,您可以仅使用本机功能将cmdlet作为作业启动:
Publish-AzWebapp -ResourceGroupName $resourceGroupname -Name $app `
-ArchivePath $filePath -Force -AsJob
然后等待作业完成并启动webapps。哦,你说的是工作流,而不是powershell 7。你试过jobs或threadjobs吗?嗨,Bill Turner这个案子怎么样?您是否使用了
保存/导入?由于其他竞争优先级(当有火柴的家伙还在四处奔跑时,很难扑灭火灾…)我还没有机会尝试此功能。@Bill Turner如果您有机会尝试,请与我们分享如何进行?感谢您在前端更改了脚本,在ForEach循环中使用Start Job(非并行),但仍然得到“上下文中找不到帐户”。使用Save-AzContext和Import-AzContext看起来它得到了正确的信息,但实际上不起作用。给我一点时间,我会用我尝试过的方法来编辑我的问题。这似乎是答案-出于某种原因,使用Start Job我不知道如何正确设置AzContext,但使用-AsJob非常有效。应该说,保存/导入不起作用,但发布AzWebapp-AsJob起作用。奇怪,保存/传递上下文对我来说很好,除非(可能)我做错了我不知道的事情。。。同样,如果我在本地Azure PowerShell中运行,那么工作区和作业都可以正常工作,但是从Azure DevOps管道中运行就没有运气了。。。