【问题标题】:Use Powershell to Publish to a WebApp Virtual Directory使用 Powershell 发布到 WebApp 虚拟目录
【发布时间】:2020-05-02 11:05:55
【问题描述】:

我有一个 Azure WebApp,即 split 到两个虚拟目录 - 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 指向虚拟目录。

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

请问我该怎么做?

【问题讨论】:

    标签: azure powershell azure-web-app-service azure-powershell


    【解决方案1】:

    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就可以了。

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2015-02-04
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多