Skip to content

Instantly share code, notes, and snippets.

@bryanknox
Last active February 9, 2024 10:25
Show Gist options
  • Save bryanknox/836307a3ac4300930aeb5c982881270a to your computer and use it in GitHub Desktop.
Save bryanknox/836307a3ac4300930aeb5c982881270a to your computer and use it in GitHub Desktop.
Azure DevOps YAML pipeline template to disable an Azure Function if it exists.

Disable Azure Function if Exists using YAML Pipeline

The disable-function-if-exists.template.yml below is a sample Azure DevOps YAML pipeline template that can be used to disable an Azure Function if it exists.

In some deployment scenarios you may need to disable an Azure Function, such as a queue triggered function, before deploying down stream resources. The function to be disabled may not exist the first time the deployment pipeline is run in a particular environment, so the template checks for its existance before attempting to disalbe it.

Links and References

Pass variables between job steps in Azure DevOps YAML Pipelines

https://gist.github.com/bryanknox/6e571e4c5fde9a9eb531a81d51b0ed3e#pass-variables-between-job-steps-in-azure-devops-yaml-pipelines

How to disable functions in Azure Functions

https://docs.microsoft.com/en-us/azure/azure-functions/disable-function Describes the recommended way to disable a function by setting the AzureWebJobs.<FUNCTION_NAME>.Disabled app setting to true in the containing function app.

parameters:
- name: connectedServiceNameARM
type: string
- name: resourceGroupName
type: string
- name: functionAppName
type: string
- name: functionName
type: string
steps:
- task: AzurePowerShell@4
displayName: 'Check if Function App Exists'
inputs:
TargetAzurePs: latestVersion
ConnectedServiceNameARM: '${{ parameters.connectedServiceNameARM }}'
scriptType: inlineScript
# Sets the 'DoesAppExist' variable for use in subsequent task.
inline: |
$funcApp = Get-AzFunctionApp -ResourceGroupName ${{ parameters.resourceGroupName }} -Name ${{ parameters.functionAppName }}
$doesAppExist = ($funcApp -ne $null)
Write-Host "##vso[task.setvariable variable=DoesAppExist]$doesAppExist"
- task: AzureAppServiceSettings@1
displayName: 'Disable Function App'
condition: eq(variables.DoesAppExist, true)
inputs:
azureSubscription: '$(subscriptionPrefix)-${{ parameters.envName }}'
appName: ${{ parameters.functionAppName }}
appSettings: |
[
{
"name": "AzureWebJobs.${{ parameters.functionName }}.Disabled",
"value": true,
"slotSetting": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment