1
votes

As part of my release pipeline in Azure DevOps, I wish to rename some .config files to .config.disabled, after my "Deploy Azure App Service" task has finished.

I have tried to add an additional "Deploy Azure App Service" task, but that seems to overwrite the previous one, leaving only the .config.disabled files in the wwwroot.

Is there any other way (other than FTP upload task), which can be used to rename/deploy a subset of files, in an Azure web app?

1
Why modify after when you could modify before?bryanbcook
I need to run a script that requires the site to be online and when the script has run, I need to disable the opportunity for that script to run again. The script performs some database synchronization and to avoid the script being run by accident, I want to disable it as soon as I have run it (by disabling the .config files)Hos

1 Answers

1
votes

If you don't want to use FTP, you might have some success trying to use the Kudu service that backs the Azure Web App.

To view the Kudu service, navigate to https://<your-web-app-name>.scm.azurewebsites.net

After you've authenticated, you'll discover there's a way to browse files, view running processes and an interactive console that allows you to run basic DOS or PowerShell commands against the file system.

The interactive console is great, but to automate it you can issue commands against the file system using a REST API.

There are several examples of issuing commands to the server. In 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
 }    

The file system supports basic commands, like copy and delete so you could easily rename the file.