在从 VSTS 进行新部署之前删除 Azure 上的文件和折叠

key*_*oze 5 powershell azure azure-powershell azure-devops azure-pipelines-build-task

作为我在 VSTS 中构建过程的一部分,我想在新部署之前从我的 azure 站点中删除所有文件和文件夹(除了少数)。我的猜测是,使用 Azure Powershell 脚本是个好主意,我更喜欢制作内联脚本。

我使用 Azure 资源管理器作为连接类型,我已经选择了我的订阅和脚本类型(内联脚本),但后来我迷路了,我该如何选择我的应用服务并首先列出我的文件?

只是尝试,这是为了测试,仅在我的 VSTS 环境中提供我的文件

Get-ChildItem -Path $(build.sourcesDirectory)
Run Code Online (Sandbox Code Playgroud)

sta*_*SFT 6

首先,最好将文件包含到 Web 应用程序需要的项目中,然后只需选中 Remove additional files at destination 选项(首先检查使用 Web 部署选项发布)以删除其他文件。

其次,您可以通过 Kudu API 删除文件。

DELETE /api/vfs/{path}    (Delete the file at path)
Run Code Online (Sandbox Code Playgroud)

更多信息,可以参考:使用PowerShell和Kudu API与Azure Web Apps虚拟文件系统交互

更新(添加 Kudu 示例):

  1. 添加 Azure PowerShell 步骤/任务
  2. Sepcify 参数,例如: -resourceGroupName XXX -webAppName XXX -kuduPath Global.asax

脚本:

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
Run Code Online (Sandbox Code Playgroud)


Pau*_*her 6

这是脚本的调整版本,它应该包含在您的项目中并作为构建的一部分导出为人工制品,我称之为 Delete-WebAppFiles.ps1

它通过还处理虚拟应用程序并在文件不存在的情况下进行错误处理,例如在第一次部署到新环境时扩展了先前的答案

param(
    [string]$resourceGroupName,
    [string]$webAppName,
    [string]$appPath="wwwroot",
    [string]$slotName="", 
    [string]$kuduPath,
    [bool]$recursive=$false
)
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-KuduFile($resourceGroupName, $webAppName, $appPath, $slotName, $kuduPath, $recursive){

    $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName
    if ($recursive -eq $true) {
        if (-not ($kuduPath.endswith("recursive=true"))) {
           if (-not ($kuduPath.endswith("/"))) {
              $kuduPath += "/"
           }
           $kuduPath += "?recursive=true"
        }
    }
    if ($slotName -eq ""){
        $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/$appPath/$kuduPath"
    }
    else{
        $kuduApiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/vfs/site/$appPath/$kuduPath"
    }

    Write-Output $kuduApiUrl
    Write-Output $kuduApiAuthorisationToken

    try
    {
        Invoke-RestMethod -Uri $kuduApiUrl `
                        -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                        -Method DELETE
    } catch {
        Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
        if (-not ($_.Exception.Response.StatusCode.value__ -eq 404)) {
            throw $PSItem
        }
    }    
}

Delete-KuduFile $resourceGroupName $webAppName $appPath $slotName $kuduPath $recursive
Run Code Online (Sandbox Code Playgroud)

然后,您可以添加上面提到的 Powershell 任务,它应该看起来有点像这样...... 在此处输入图片说明