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

时间:2020-06-01 19:02:13

标签: azure-devops azure-pipelines azure-web-app-service kudu azure-pipelines-tasks

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

在部署之间,没有删除容器中的文件,这会引起问题。

我注意到我是否在使用其他类型的应用程序服务(例如Windows上的Web App),并且如果部署方法设置为Web Deploy,则Remove additional files at destination的选项存在(请参见屏幕截图)。但是,我们使用Zip Deploy方法,并且更喜欢使用linux服务。如果没有将应用程序服务和部署方法结合在一起,则该选项对我不可用。

enter image description here

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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   
相关问题