3

我正在使用 Linux 服务上的 Web 应用程序并使用 DevOps 发布管道部署一个 python Web 应用程序。我正在使用的管道任务称为AzureRmWebAppDeployment@4.

在部署之间,容器中的文件不会被删除,这会导致问题。

我注意到我是否使用了不同类型的应用程序服务(即 Windows 上的 Web 应用程序)以及部署方法是否设置Web Deploy为存在选项Remove additional files at destination(见屏幕截图)。但是,我们正在使用一种Zip Deploy方法,并且更喜欢使用 linux 服务。如果没有应用服务和部署方法的组合,我无法使用此选项。

在此处输入图像描述

有人可以建议在部署时删除容器内容的替代方法吗?此外,关于为什么在使用 Zip Deploy 和 Linux 时无法通过管道任务使用此选项的任何见解?

提前感谢您提供的任何帮助。

4

2 回答 2

5

您可以使用kudu 命令 api来清理 webapp 服务器上的 wwwroot 文件夹。kudu 命令rest api 将command在服务器上的指定目录中执行。

{
    command = "find . -mindepth 1 -delete"  
    dir = "/home/site/wwwroot
} 

在 Azure 应用服务部署任务之前添加一个Azure powershell任务,并在下面的内联脚本中运行。

$ResGroupName = ""
$WebAppName = ""

# Get publishing profile for web application
$WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp

# Create Base64 authorization header
$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$bodyToPOST = @{  
                  command = "find . -mindepth 1 -delete"  
                  dir = "/home/site/wwwroot"  
}  
# Splat all parameters together in $param  
$param = @{  
            # command REST API url  
            Uri = "https://$WebAppName.scm.azurewebsites.net/api/command"  
            Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
            Method = "POST"  
            Body = (ConvertTo-Json $bodyToPOST)  
            ContentType = "application/json"  
}  
# Invoke REST call  
Invoke-RestMethod @param  

/home/site/wwwroot以上脚本将在每次部署之前清空文件夹。

如果需要删除应用服务器上的特定文件,可以使用kudu delete rest api:

DELETE /api/vfs/{path}
Delete the file at path.

有关更多示例,您可以在此处查看

于 2020-06-02T08:15:55.587 回答
0

这是对 Levi Lu-MSFT 的答案的轻微修改,适用于生产或插槽:

$ResGroupName = ""
$WebAppName = ""
$SlotName = ""

# Get publishing profile for web application
if([string]::IsNullOrEmpty($SlotName)) {
  $WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
  [xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp
  $Uri = "https://$WebAppName.scm.azurewebsites.net/api/command"
} else {
  $WebApp = Get-AzWebAppSlot -Name $WebAppName -ResourceGroupName $ResGroupName -Slot $SlotName    
  [xml]$publishingProfile = Get-AzWebAppSlotPublishingProfile -WebApp $WebApp  
  $Uri = "https://$WebAppName-$SlotName.scm.azurewebsites.net/api/command"
}

# Create Base64 authorization header
$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$bodyToPOST = @{  
                  command = "find . -mindepth 1 -delete"  
                  dir = "/home/site/wwwroot"  
}  
# Splat all parameters together in $param  
$param = @{  
            # command REST API url  
            Uri = $Uri  
            Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)}  
            Method = "POST"  
            Body = (ConvertTo-Json $bodyToPOST)  
            ContentType = "application/json"  
}  
# Invoke REST call  
Invoke-RestMethod @param   
于 2021-06-01T21:10:44.627 回答