Remove files and foldes on Azure before a new deploy from VSTS

前端 未结 3 705
野趣味
野趣味 2020-11-29 09:32

As part of my build process in VSTS I want to delete all files and folders (except af few) from my azure site before a new deploy. My guess is, that using a Azure Powershell

3条回答
  •  执念已碎
    2020-11-29 10:22

    First, it’s better to include the files to the project that the web app needs, then just check Remove additional files at destination option (Check Publish using Web Deploy option first) to remove additional files.

    Secondly, you can remove the files through Kudu API.

    DELETE /api/vfs/{path}    (Delete the file at path)
    

    More information, you can refer to: Interacting with Azure Web Apps Virtual File System using PowerShell and the Kudu API

    Update (Add Kudu sample):

    1. Add Azure PowerShell step/task
    2. Sepcify arguments, for example: -resourceGroupName XXX -webAppName XXX -kuduPath Global.asax

    Script:

    param(
        [string]$resourceGroupName,
        [string]$webAppName,
        [string]$slotName="", 
        [string]$kuduPath
    )
    function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
        if ([string]::IsNullOrWhiteSpace($slotName)){
            $resourceType = "Microsoft.Web/sites/config"
            $resourceName = "$webAppName/publishingcredentials"
        }
        else{
            $resourceType = "Microsoft.Web/sites/slots/config"
            $resourceName = "$webAppName/$slotName/publishingcredentials"
        }
        $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
        Write-Host $publishingCredentials   
        return $publishingCredentials
    }
    function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){
        $publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
        Write-Host $publishingCredentials.Properties.PublishingUserName
        Write-Host $publishingCredentials.Properties.PublishingPassword
        return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
    }
    function Delete-FileToWebApp($resourceGroupName, $webAppName, $slotName = "", $kuduPath){
    
        $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
        if ($slotName -eq ""){
            $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$kuduPath"
        }
        else{
            $kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/vfs/site/wwwroot/$kuduPath"
        }
    
        Write-Output $kuduApiUrl
        Write-Output $kuduApiAuthorisationToken
        Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method DELETE
    }
    
    Delete-FileToWebApp $resourceGroupName $webAppName $slotName $kuduPath
    

提交回复
热议问题