首页 > 解决方案 > 使用 Powershell 发布到 WebApp 虚拟目录

问题描述

我有一个 Azure WebApp,它分为两个虚拟目录 - UI 和 API。

我设法在代码中创建了虚拟目录,但找不到向它们发布的方法。

到目前为止,这是我的代码:

# Set UI Virtaul Directory (call /ui )
$website = Get-AzWebApp -Name $appsvWebAppName -ResourceGroupName $resourceGroupName
 $VDApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication 
 $VDApp.VirtualPath = "/ui" 
 $VDApp.PhysicalPath = "site\wwwroot\ui" 
 $VDApp.PreloadEnabled ="YES" 
 $website.siteconfig.VirtualApplications.Add($VDApp) 
 $website | Set-AzWebApp -Verbose

# Set API Virtual Directory (call /api )
$website = Get-AzWebApp -Name $appsvWebAppName -ResourceGroupName $resourceGroupName
 $VDApp = New-Object Microsoft.Azure.Management.WebSites.Models.VirtualApplication 
 $VDApp.VirtualPath = "/api" 
 $VDApp.PhysicalPath = "site\wwwroot\api" 
 $VDApp.PreloadEnabled ="YES" 
 $website.siteconfig.VirtualApplications.Add($VDApp) 
 $website | Set-AzWebApp -Verbose

 $website.SiteConfig.VirtualApplications

# Dotnet publish & convert to zip here, removed for brevity ...

$uiZipPath = $zipFilesFolder + "\ui.zip"


 $publishprofile = Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroupName `
 -Name $appsvWebAppName `
 -OutputFile $publishProfileFileName


 Publish-AzWebApp -ArchivePath $uiZipPath `
 -ResourceGroupName $resourceGroupName `
 -Name $appsvWebAppName 

我看不到如何将Publish-AzWebApp指向虚拟目录。

发布可以手动完成,但我真的想自动化它(使用 Publish-AzWebApp 或其他方式)。

请问我该怎么做?

标签: azurepowershellazure-web-app-serviceazure-powershell

解决方案


Publish-AzWebApp不支持,您可以在 powershell 中使用它Kudu API来自动化它。

在我的示例中,它VFS首先用于创建目录,然后通过Zip.

$appsvWebAppName = "xxxxxxx"
$resourceGroupName = "xxxxxxx"

$resource = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName "$appsvWebAppName/publishingcredentials" -Action list -ApiVersion 2018-02-01 -Force

$username = $resource.Properties.publishingUserName
$password = $resource.Properties.publishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
$userAgent = "powershell/1.0"

# Create the folder, not lose `/` after `ui`
$apiUrl = "https://$appsvWebAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/ui/"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT

#Upload the zip file
$apiUrl = "https://$appsvWebAppName.scm.azurewebsites.net/api/zip/site/wwwroot/ui"
$filePath = "C:\Users\joyw\Desktop\testdep.zip"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $filePath -ContentType "multipart/form-data"

对于site\wwwroot\api, 也是一样的逻辑,只是在脚本里ui改成。api


推荐阅读