Azure devops Azure应用程序服务部署任务选项,用于在部署时删除容器中的文件

Azure devops Azure应用程序服务部署任务选项,用于在部署时删除容器中的文件,azure-devops,azure-pipelines,azure-web-app-service,kudu,azure-pipelines-tasks,Azure Devops,Azure Pipelines,Azure Web App Service,Kudu,Azure Pipelines Tasks,我正在使用Linux上的web app服务和DevOps发布管道部署一个python web app。我正在使用的管道任务称为AzureRmWebAppDeployment@4 在部署之间,容器中的文件不会被删除,这会导致问题 我注意到,如果我正在使用不同类型的应用程序服务(即Windows上的Web应用程序),并且如果部署方法设置为Web Deploy,则存在在目标位置删除其他文件的选项(请参见屏幕截图)。但是,我们使用的是Zip部署方法,更喜欢使用linux服务。如果没有应用程序服务和部署方

我正在使用Linux上的web app服务和DevOps发布管道部署一个python web app。我正在使用的管道任务称为
AzureRmWebAppDeployment@4

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

我注意到,如果我正在使用不同类型的应用程序服务(即Windows上的Web应用程序),并且如果部署方法设置为
Web Deploy
,则存在
在目标位置删除其他文件的选项(请参见屏幕截图)。但是,我们使用的是
Zip部署
方法,更喜欢使用linux服务。如果没有应用程序服务和部署方法的组合,我将无法使用此选项

有人能建议一种在部署时删除容器内容的替代方法吗?另外,在使用Zip Deploy和Linux时,您是否了解为什么管道任务中不提供此选项

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

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

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

在Azure应用程序服务部署任务之前添加任务,并在内联脚本下面运行

$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

如果需要删除app server上的特定文件,可以使用kudu delete rest api:

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

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

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

谢谢,这似乎是一个非常有希望的解决方案。我一直在尝试运行Azure Powershell任务,但一直遇到以下错误:
找不到版本为“1.0.0”的模块:“Az.Accounts”。如果模块是最近安装的,请在重新启动Azure Pipelines任务代理后重试
,我找到并尝试了建议,但仍然没有成功。您熟悉此错误消息吗?您的管道代理是什么?azure powershell任务版本和azure powershell版本是什么?我使用azure powershell任务
version 4
和最新安装的
azure powershell版本在代理ubuntu 18.04上成功运行了上述脚本,这就是问题所在。我使用了错误的代理。我接受你的回答。非常感谢你知道一种方法可以让你的答案针对某个时段吗?我现在遇到了问题,因为az powershell任务正在清除生产槽。理想情况下,它将清除我们部署到的暂存槽。您可以尝试将
$param
中的Uri更改为
Uri=“https://$WebAppName-$slotName.scm.azurewebsites.net/api/command”
欢迎使用堆栈溢出,并感谢您的贡献。考虑到脚本的长度,您能否进一步澄清您对Levi的解决方案做了哪些更改,以及为什么您认为这是一种更好的方法?您好@JeremyCaney,Levi的解决方案仅适用于Azure App Services实例的默认生产时段。通常,最好先在非prod部署插槽上进行测试。通过提供deplotment槽的名称$SlotName,它将只影响槽上的更改,这是我更改的增值。如果$SlotName为null或空,它将影响默认生产槽的更改,就像Levi的解决方案一样。