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

时间:2017-05-05 09:41:24

标签: powershell azure azure-devops azure-powershell azure-pipelines-build-task

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

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

尝试,仅用于测试,仅在我的VSTS环境中提供我的文件

Get-ChildItem -Path $(build.sourcesDirectory)

2 个答案:

答案 0 :(得分:4)

这是脚本的经过调整的版本,应包含在您的项目中,并作为人工制品作为构建的一部分导出,我称之为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

然后您可以如上所述添加一个Powershell任务,该任务看起来应该像这样…… enter image description here

答案 1 :(得分:0)

这是使用Postman测试上面的PowerShell脚本中使用的设置的方法。

我发现使用Postman来使用REST API很有用,以了解上面的PowerShell脚本在做什么,并了解我可以期望从API返回的状态代码。我还注意到,即使确实删除了文件,用于删除目录的递归逻辑也会返回409冲突错误。

在以下示例中,我的应用程序服务称为“ YatesDeleteMe”

在授权标头中创建以下使用64位编码的用户名和密码字符串,或在上方运行PowerShell脚本,它将为您输出一个

  1. 下载应用程序服务的发布文件,该文件可以从Azure门户的“概述”选项卡中下载。
  2. 使用文本编辑器打开文件
  3. 找到用户名(用户名示例:$ YatesDeleteMe)和密码(密码示例:ch222cDlpCjx4Glq333qo4QywGPMs1cK2Rjrn6phqZ9HswtgEEE12CrhDmcn)
  4. 在其中创建一个字符串,并用冒号分隔它们(看起来应像这样:$ YatesDeleteMe:ch222cDlpCjx4Glq333qo4QywGPMs1cK2Rjrn6phqZ9HswtgEEE12CrhDmcn)
  5. Base 64使用您自己的程序site对它们进行编码。结果应如下所示:JFlhdGVzRGVsZXRlTWU6Y2gyMjJjRGxwQ2p4NEdscTMzM3FvNFF5d0dQTXMxY0syUmpybjZwaHFaOUhzd3RnRUVFMTJDcmhEbWNu

检索单个文件

  1. 打开邮递员
  2. 将动词更改为GET
  3. 输入您要检索的文件的URL(例如https://YatesDeleteMe.scm.azurewebsites.net/api/vfs/site/wwwroot/web.config)。有关路径see this
  4. 的更多信息
  5. 添加了标题->
    • 密钥:授权
    • 值:基本的YourBase64EncodedStringFromAbove
  6. 左键单击“发送”按钮,文件将下载

删除单个文件

  1. 打开邮递员
  2. 将动词更改为DELETE
  3. 输入您要删除的文件的URL(例如https://YatesDeleteMe.scm.azurewebsites.net/api/vfs/site/wwwroot/web.config
  4. 添加了标题->
    • 密钥:授权
    • 值:基本的YourBase64EncodedStringFromAbove
  5. 添加了标题->
    • 键:如果匹配
    • 值:*
  6. 左键单击“发送”按钮,文件将被删除

删除目录中的所有文件

  1. 打开邮递员
  2. 将动词更改为DELETE
  3. 输入您要删除PLUST加斜杠的文件夹的URL,然后输入查询字符串?recursive = true(例如https://YatesDeleteMe.scm.azurewebsites.net/api/vfs/site/wwwroot/?recursive=true
  4. 添加了标题->
    • 密钥:授权
    • 值:基本的YourBase64EncodedStringFromAbove
  5. 添加了标题->
    • 键:如果匹配
    • 值:*
  6. 左键单击“发送”按钮,文件夹将被删除。我总是会收到409错误,但是文件夹已删除。

参考

  • 带有图片here的博客帖子。