重命名其他文件或将其他文件部署到Azure Web App

时间:2019-04-08 10:21:11

标签: azure-devops azure-pipelines azure-web-app-service azure-pipelines-release-pipeline

作为我在Azure DevOps中发布管道的一部分,我希望在“部署Azure应用程序服务”任务完成后将一些.config文件重命名为.config.disabled

我尝试添加其他“部署Azure应用服务”任务,但这似乎覆盖了前一个任务,仅将.config.disabled文件保留在wwwroot中。

Azure Web应用程序中是否有其他方法(FTP上传任务除外)可用于重命名/部署文件的子集?

1 个答案:

答案 0 :(得分:1)

如果您不想使用FTP,则尝试使用支持Azure Web App的Kudu service可能会成功。

要查看Kudu服务,请导航至https:// << em>您的网络应用名称>。 scm .azurewebsites.net

通过身份验证后,您会发现有一种浏览文件,查看正在运行的进程以及交互式控制台的方法,该控制台允许您对文件系统运行基本的DOS或PowerShell命令。

交互式控制台很棒,但是要使其自动化,您可以使用REST API对文件系统发出命令。

issuing commands to the server有几个示例。在PowerShell中:

# authenticate with Azure
Login-AzureRmAccount
$resoureGroupName = "your-web-app-name"
$websiteName = "your-resource-group-name"

$env = @{
     command= 'Set COMPUTERNAME'
     dir= 'site'
}
$json = $env | ConvertTo-Json

$env2 = @{
     command= 'copy filename.config file.config.notused'
     dir= 'site\wwwroot'
}
$json2 = $env2 | ConvertTo-Json

$env3 = @{
     command= 'delete filename.config'
     dir= 'site\wwwroot'
}
$json3 = $env3 | ConvertTo-Json

# setup auth header
$website = Get-AzureWebsite -Name $websiteName
$username = $website.PublishingUsername
$password = $website.PublishingPassword
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"
[System.Uri]$Uri = $apiBaseUrl

# get all the vms in the web-app
$instances = Get-AzureRmResource -ResourceGroupName $resoureGroupName `
                                -ResourceType Microsoft.Web/sites/instances `
                                -ResourceName $websiteName `
                                -ApiVersion 2018-02-01

#loop through instances
foreach($instance in $instances)
{
    $instanceName = $instance.Name
    Write-Host "`tVM Instance ID `t`t: " $instanceName

    #Now execute 'SET COMPUTER' cmd
    $cookie= New-Object System.Net.Cookie
    $cookie.Name = "ARRAffinity"
    $cookie.Value = $instanceName
    $Cookie.Domain = $uri.DnsSafeHost
    $session=New-Object Microsoft.Powershell.Commands.WebRequestSession
    $session.Cookies.add($cookie)
     $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                                -Headers @{Authorization=("Basic {0}" `
                                -f $base64AuthInfo)} `
                                -Method Post -Body $json `
                                -ContentType 'application/json' `
                                -WebSession $session
    Write-Host "`tVM Instance Name `t: " $response

    # perform copy file
    $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                               -Headers @{Authorization=("Basic {0}" `
                               -f $base64AuthInfo)} `
                               -Method Post -Body $json2 `
                               -ContentType 'application/json' `
                               -WebSession $session
    Write-Host "`tCopy file result `t: " $response

    # perform delete file
    $response = Invoke-RestMethod -Uri "$apiBaseUrl/command" `
                               -Headers @{Authorization=("Basic {0}" `
                               -f $base64AuthInfo)} `
                               -Method Post -Body $json3 `
                               -ContentType 'application/json' `
                               -WebSession $session
    Write-Host "`tCopy file result `t: " $response
 }    

文件系统支持基本命令,例如 copy delete ,因此您可以轻松地重命名文件。