在从 VSTS 进行新部署之前删除 Azure 上的文件和折叠 [英] Remove files and foldes on Azure before a new deploy from VSTS

查看:15
本文介绍了在从 VSTS 进行新部署之前删除 Azure 上的文件和折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 script would be a good idea and I would prefer making an inline script.

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

I am using Azure Resource Manager as connection type, I have selected my subscription and script type (Inline Script) but then I am lost, how do i select my app service and, for a start, list my files?

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

Trying just, this for a test, only gives my files in my VSTS environment

Get-ChildItem -Path $(build.sourcesDirectory)

推荐答案

首先,最好将 Web 应用程序需要的文件包含到项目中,然后只需选中 Remove additional files at destination 选项(Check Publish using Web Deploy 选项首先)删除其他文件.

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.

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

Secondly, you can remove the files through Kudu API.

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

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

更新(添加 Kudu 示例):

Update (Add Kudu sample):

  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

这篇关于在从 VSTS 进行新部署之前删除 Azure 上的文件和折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆