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

查看:80
本文介绍了从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应用程序所需的项目中,然后选中在目标位置删除其他文件"选项(选中使用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)

更多信息,您可以参考:

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

更新(添加Kudu示例):

Update (Add Kudu sample):

  1. 添加Azure PowerShell步骤/任务
  2. 区分参数,例如:-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天全站免登陆