Jack Rogers
Jack Rogers

Reputation: 695

CI pipeline fails even though it is doing what its supposed to

Let me start by saying I just started Azure and by no means a pro and been following guides here and there. So, I'm following this article by deploying this repo in my own Azure DevOps subscription, after I changed the resource group, subscription, and function name, I build and run the CI pipeline, then at the "Deploy Azure Resources" I'd get this error even though after the pipeline exits, the resources are created the way its supposed to, I also notice if I re run the same pipeline the 2nd time, it works perfectly which is confusing. Here is the modified the YAML template that I have

name: DeployAzureFunction
variables:
  FunctionAppName: 'test'
  AzureConnection: 'DevOps-Test'
  ResourcegGroupName: 'test'

trigger:
  branches:
    include:
    - '*'  # must quote since "*" is a YAML reserved character; we want a string



stages:

- stage: Build
  jobs:
  - job: Test_FA
    pool:
      vmImage: windows-2019
    steps:
    - task: AzureResourceGroupDeployment@2
      displayName: 'Test ARM Deployment'
      inputs:
        azureSubscription: $(AzureConnection)
        resourceGroupName: $(ResourcegGroupName)
        location: 'East US 2'
        csmFile: Deployment/azuredeploy.json
        deploymentMode: Validation
    - powershell: |
       Install-PackageProvider -Name NuGet -Force -Scope CurrentUser
       Install-Module -Name Pester -Force -Scope CurrentUser -SkipPublisherCheck
       Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser
      displayName: 'Install Pester and import module'
   
  - job: Build_FA
    pool:
      vmImage: windows-2019
    steps:
    - task: ArchiveFiles@2
      displayName: 'Archive FunctionApp'
      inputs:
        rootFolderOrFile: FunctionApp
        includeRootFolder: false
        archiveFile: '$(System.DefaultWorkingDirectory)/zip/FunctionApp.zip'
    - task: PublishPipelineArtifact@0
      inputs:
        artifactName: 'zip'
        targetPath: '$(System.DefaultWorkingDirectory)/zip'

- stage: Deploy
  jobs:
  - job: Deploy_ARM
    pool:
      vmImage: windows-2019
    steps:
    - task: AzureResourceGroupDeployment@2
      displayName: 'Deploy Azure Resources'
      continueOnError: true
      inputs:
        azureSubscription: $(AzureConnection)
        resourceGroupName: $(ResourcegGroupName)
        location: 'East US 2'
        csmFile: Deployment/azuredeploy.json
        csmParametersFile: Deployment/azuredeploy.parameters.json
        overrideParameters: '-functionAppName $(FunctionAppName)'
        deploymentMode: Incremental
        deploymentOutputs: DeploymentOutput

And this is the unmodified azuredeploy.json that I think is causing the issue but not sure why

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "functionAppName": {
            "type": "string",
            "defaultValue": "[uniqueString(resourceGroup().id)]",
            "metadata": {
                "description": "Specify the name of the function application"
            }
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]",
            "metadata": {
                "description": "Specify the location for the function application resources"
            }
        }
    },
    "variables": {
        "hostingPlanName": "[parameters('functionAppName')]",
        "storageacccount": "[toLower(parameters('functionAppName'))]",
        "storageAccountName": "[concat('storage', variables('storageacccount'))]"
    },
    "resources": [
        {
            "apiVersion": "2017-06-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[variables('storageAccountName')]",
            "location": "[parameters('location')]",
            "kind": "Storage",
            "sku": {
                "name": "Standard_LRS"
            }
        },
        {
            "type": "Microsoft.Web/serverfarms",
            "apiVersion": "2018-11-01",
            "name": "[variables('hostingPlanName')]",
            "location": "[parameters('location')]",
            "sku": {
                "name": "Y1",
                "tier": "Dynamic",
                "size": "Y1",
                "family": "Y",
                "capacity": 0
            },
            "properties": {
                "name": "[variables('hostingPlanName')]"
            }
        },
        {
            "name": "[parameters('functionAppName')]",
            "type": "Microsoft.Web/sites",
            "apiVersion": "2018-02-01",
            "location": "[parameters('location')]",
            "kind": "functionapp",
            "dependsOn": [
                "[resourceId('Microsoft.Web/serverfarms/', variables('hostingPlanName'))]",
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]",
                "[resourceId('microsoft.insights/components/', parameters('functionAppName'))]"
            ],
            "identity": {
                "type": "SystemAssigned"
            },
            "properties": {
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "FUNCTIONS_WORKER_RUNTIME",
                            "value": "powershell"
                        },
                        {
                            "name": "AzureWebJobsStorage",
                            "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2017-06-01').keys[0].value)]"
                        },
                        {
                            "name": "FUNCTIONS_EXTENSION_VERSION",
                            "value": "~2"
                        },
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[reference(resourceId('microsoft.insights/components/', parameters('functionAppName')), '2018-05-01-preview').InstrumentationKey]"
                        },
                        {
                            "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
                            "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')),'2017-06-01').keys[0].value)]"
                        },
                        {
                            "name": "WEBSITE_CONTENTSHARE",
                            "value": "[toLower(parameters('functionAppName'))]"
                        }
                    ]
                },
                "name": "[parameters('functionAppName')]",
                "clientAffinityEnabled": false,
                "serverFarmId": "[resourceId('Microsoft.Web/serverfarms/', variables('hostingPlanName'))]"
            }
        },
        {
            "apiVersion": "2015-05-01",
            "name": "[parameters('functionAppName')]",
            "type": "Microsoft.Insights/components",
            "kind": "Web",
            "location": "[parameters('location')]",
            "tags": {
                "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', parameters('functionAppName')))]": "Resource"
            },
            "properties": {
                "ApplicationId": "[parameters('functionAppName')]",
                "Application_Type": "web"
            }
        }
    ],
    "outputs": {
        "principalId": {
            "type": "string",
            "value": "[reference(concat(resourceId('Microsoft.Web/sites/', parameters('functionAppName')), '/providers/Microsoft.ManagedIdentity/Identities/default'), '2015-08-31-PREVIEW').principalId]"
        }
    }
}

If anyone could help me figure out why I get that error in the first run, I'd appreciate it, thanks!

Upvotes: 1

Views: 78

Answers (1)

Tudor Muresan
Tudor Muresan

Reputation: 61

The Resources are created yet the Deployment operation fails because there is a problem in caclulating the output variable.

Most likely it is because of the way you are trying to access the Service Principal Id of the App Service. The '/providers/Microsoft.ManagedIdentity/Identities/default' part.

You probably want something like this:

[reference(resourceId('Microsoft.Web/sites', parameters('functionAppName')), '2016-08-01', 'Full').identity.principalId]

Upvotes: 1

Related Questions