Azure App Service 部署任务选项,用于在部署时删除容器中的文件

Azure App Service deploy task option to remove files in container upon deployment

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

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

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

任何人都可以建议在部署时删除容器内容的替代方法吗?此外,关于为什么在使用 Zip Deploy 和 Linux?

时管道任务无法使用此选项的任何见解

在此先感谢您提供的任何帮助。

您可以使用 kudu command 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.

有关更多示例,您可以查看 here

这是对 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