Xuntar
Xuntar

Reputation: 2280

How to delete a folder on an Azure App Service website using Octopus Deploy

I am setting up an automated deployment project for a Sitecore website using TeamCity and Octopus Deploy (v. 3.3.6).

Before deploying to the App Service using a "Deploy an Azure Web App" step, I would like to delete one folder on that site (/site/wwwroot/App_Config/Include or D:\home\site\wwwroot\App_Config\Include).

Is there a built-in mechanism in Octopus to do this? I have tried using a Powershell script, but that runs on the server, not on Azure. Is there a way to run a Powershell script on Azure while deploying? Or can I use the "Run an Azure Powershell Script" to do file manipulations on an App Service website without having to authenticate (since Octopus is doing the authentication)?

Or are there any other/better solutions to solve this without having to save credentials in a file on the Build Server?

I would prefer not to use FTP for this, if at all possible.

Upvotes: 1

Views: 2784

Answers (2)

Xuntar
Xuntar

Reputation: 2280

In the end I decided to use FTP anyway. Even though I quite like Kai Zhao's suggestion, I preferred to keep all things related to the automated deploy on the deploy server instead of having to put and maintain scripts in different locations.

I used the following Powershell FTP Client Module and installed it on our build server: https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb

And I used this script to do the actual delete and run it as a step in Octopus:

$AzureAppService is a variable in Octopus which changes depending on the environment.

Import-Module PSFTP 
$username = $AzureAppService
$password = ConvertTo-SecureString "************" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential `
         -argumentlist $username, $password
$ftpserver = "***url_to_your_ftp_server**"
$folderToDelete = "/site/wwwroot/App_Config/Include"
Set-FTPConnection -Credentials $cred -Server $ftpserver -Session MyFTPSession -UsePassive 
$Session = Get-FTPConnection -Session MyFTPSession
Try
{
    Remove-FTPItem -Session $Session -Path $folderToDelete -Recurse 
}
Catch
{
    Write-Warning "There was an error while trying to remove the folder:"
    Write-Warning $_.Exception.Message
    Write-Warning $_.Exception.ItemName
}

Upvotes: 0

Kai Zhao
Kai Zhao

Reputation: 1015

I would do this:

  1. Create a on-demand webjob in Azure and upload a powershell script that can clean up the folder to your Webapp. Try to keep this ps script simple, use base cmdlet commands, not all ps modules would run in azure. How: https://blogs.msdn.microsoft.com/nicktrog/2014/01/22/running-powershell-web-jobs-on-azure-websites/

  2. You still need your teamcity or octopus to run a powershell line to kick off the webjob. With that, the work load is not on octopus server anymore but you still need the same azure authentication process for the powershell line. How: http://www.nullfactory.net/2015/05/start-azure-webjobs-on-demand/

"Run an Azure Powershell Script" in octopus helps you load the Azure Powershell module and do the Azure-Subscription trick, so you don't need to include the library within the script or re-authenticate. It still runs locally but step #2 fits this well.

Hope this can help you.

Upvotes: 1

Related Questions