Press "Enter" to skip to content

Disabling Sitecore config files in Azure App Service using Azure DevOps

Since we are keen on keeping the Sitecore scwdp packages OOTB as untouched as can be (as of our deployment strategy) we have two type of tasks
in our release pipeline to disable/rename config files after deploying a vanilla package. Please keep in mind that, while using these tasks, we are always deploying to a staging slot with status ‘stopped’.


While checking the script(s), note that you are able to write your own powershell scripts within the $commandBody variable:
"$commandBody = @{
command = "powershell.exe -command `"Get-ChildItem -file `"$kuduFile`" | Rename-Item -newname {`$_.name + '.disabled'}`""
}"


Disabling a single config file (for disabling your CES device detection for example):
Within Azure DevOps you should add an Azure Powershell task and point it towards a ps1 file containing:

param(
        $Environment,
        $ResourceGroupPaas = "",
        $slotName = "",
        [string]$kuduFile = "",
        [int]$timeOutSec = 180
)

$ErrorActionPreference = "Stop"


	function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null)
	{
		if ([string]::IsNullOrWhiteSpace($slotName)){
			$resourceType = "Microsoft.Web/sites/config"
			$resourceName = [string]::Format("{0}/publishingcredentials",$webAppName)

		}
		else{
			$resourceType = "Microsoft.Web/sites/slots/config"
			$resourceName = [string]::Format("{0}/{1}/publishingcredentials",$webAppName,$slotName)
		}

		$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 executePowershellCommandWithinKudu($resourceGroupName = "", $webAppName, $slotName = "", $kuduFile)
	{
		if($resourceGroupName -eq "")
		{
			Write-Error 'resourceGroupName is invalid'
		}

		if($kuduFile -eq "")
		{
			Write-Error 'kuduFile is invalid'
		}

		$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName

		if ([string]::IsNullOrWhiteSpace($slotName)){
			$kuduApiUrl = [string]::Format("https://{0}.scm.azurewebsites.net/api/command",$webAppName)
		}
		else{
			$kuduApiUrl = [string]::Format("https://{0}-{1}.scm.azurewebsites.net/api/command",$webAppName,$slotName)
		}

        $commandBody = @{
            command = "powershell.exe -command `"Get-ChildItem -file `"$kuduFile`" | Rename-Item -newname {`$_.name + '.disabled'}`""
        }

		Try
		{        
		    Write-Host $kuduApiUrl
		    Write-Host $kuduApiAuthorisationToken
		    Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method POST `
                            -ContentType "application/json"`
                            -Body (ConvertTo-Json $commandBody)`
                            -TimeoutSec $timeOutSec

			Write-Host "disabled $kuduFile"
		}
		Catch
		{
			Write-Warning "Could not delete path: $kuduFile"
			Write-Warning  $_.Exception.Message   
		}
	}

# Execute command
executePowershellCommandWithinKudu $ResourceGroupPaas $Environment $slotName $kuduFile

Use the following example parameters while executing:

-Environment “NameOfYourAppService” -ResourceGroupPaas “ResourceGroupName” -slotName “staging” -kuduFile ‘D:/home/site/wwwroot/App_Config/Include/CES/Sitecore.CES.DeviceDetection.config’ -timeOutSec 360

Disabling all config files within a given folder (for disabling FXM for example):
Within Azure DevOps you should add an Azure Powershell task and point it towards a ps1 file containing:


param(
        $Environment,
        $ResourceGroupPaas = "",
        $slotName = "",
        [string]$kuduPath = "",
        [int]$timeOutSec = 180
)

$ErrorActionPreference = "Stop"


	function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null)
	{
		if ([string]::IsNullOrWhiteSpace($slotName)){
			$resourceType = "Microsoft.Web/sites/config"
			$resourceName = [string]::Format("{0}/publishingcredentials",$webAppName)

		}
		else{
			$resourceType = "Microsoft.Web/sites/slots/config"
			$resourceName = [string]::Format("{0}/{1}/publishingcredentials",$webAppName,$slotName)
		}

		$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 executePowershellCommandWithinKudu($resourceGroupName = "", $webAppName, $slotName = "", $kuduPath)
	{
		if($resourceGroupName -eq "")
		{
			Write-Error 'resourceGroupName is invalid'
		}

		if($kuduPath -eq "")
		{
			Write-Error 'kuduPath is invalid'
		}

		$kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $slotName

		if ([string]::IsNullOrWhiteSpace($slotName)){
			$kuduApiUrl = [string]::Format("https://{0}.scm.azurewebsites.net/api/command",$webAppName)
		}
		else{
			$kuduApiUrl = [string]::Format("https://{0}-{1}.scm.azurewebsites.net/api/command",$webAppName,$slotName)
		}

        $commandBody = @{
            command = "powershell.exe -command `"Get-ChildItem -path `"$kuduPath`" -exclude '*.disabled' | Where-Object {!`$_.PsIsContainer} | Rename-Item -newname {`$_.name + '.disabled'}`""
        }
        #Remove-Item –path c:\testfolder –recurse 

		Try
		{        
		    Write-Host $kuduApiUrl
		    Write-Host $kuduApiAuthorisationToken
		    Invoke-RestMethod -Uri $kuduApiUrl `
                            -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} `
                            -Method POST `
                            -ContentType "application/json"`
                            -Body (ConvertTo-Json $commandBody)`
                            -TimeoutSec $timeOutSec

			Write-Host "disabled all files in $kuduPath"
		}
		Catch
		{
			Write-Warning "Could not delete path: $kuduPath"
			Write-Warning  $_.Exception.Message   
		}
	}

# Execute command
executePowershellCommandWithinKudu $ResourceGroupPaas $Environment $slotName $kuduFile

Use the following example parameters while executing:

-Environment “NameOfYourAppService” -ResourceGroupPaas “ResourceGroupName” -slotName “staging” -kuduPath ‘D:/home/site/wwwroot/App_Config/Sitecore/FederatedExperienceManager’ -timeOutSec 360

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.