Home > database >  Checking Functions exist on FunctionApp in Azure via DevOps Pipeline
Checking Functions exist on FunctionApp in Azure via DevOps Pipeline

Time:07-08

On Azure DevOps I have a build/deploy YAML pipeline which builds my Function App and deploys this to a Linux Function App in Azure (hosted on an app-service plan).

However, recently I noticed an issue where the pipeline was showing the function app was successfully deployed, but when going into the FunctionApp in Azure and clicking on the Functions tab on the left, it shows "No results", but there should be 4 functions in there.

I want to have a step at the end of the deploy pipeline which checks that 4 functions exist in this FunctionApp, and to fail the pipeline run if this is not the case.

I know this will most likely be a task in the pipeline using Azure CLI or Powershell, but not sure how I would go around writing the script. Any help would be immensely appreciated.

Thanks all,

CodePudding user response:

You could use an Azure CLI task and call the Rest API to list the functions withint the FunctionApp. I don't think the native Az CLI and Azure PowerShell functions expose the functions within the FunctionApp/

URI = "https://management.azure.com/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Web/sites/<functionAppName>/functions?api-version=2015-08-01"

az rest -u $URI --method get | jq '.value[].name'

CodePudding user response:

steps:
- powershell: |
   #get token
   $TENANTID="xxx"
   $APPID="xxx"
   $PASSWORD="xxx"
   $result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
   $token=$result.access_token
   
   ##set Header
   $Headers=@{
       'authorization'="Bearer $token"
       'host'="management.azure.com"
   }
   
   $functions = Invoke-RestMethod  -Uri "https://management.azure.com/subscriptions/<subcription id>/resourceGroups/<resource group name>/providers/Microsoft.Web/sites/<function app name>/functions?api-version=2015-08-01"  -Headers $Headers -ContentType "application/json" -Method GET
   
   
   if($functions.value.Count -eq 4) {
       # make pipeline to succeed
       Write-Host 'Function deployment success.'
       exit 0
   }
   else {
       Write-Host 'Function deployment failed.'
       exit 1
   }
   
   
  displayName: 'Check whether the function app deployment completed.'

If you have concerns, let me know.

  • Related