繁体   English   中英

如何通过 CI/CD 管道将 Azure 函数部署到函数应用中?

[英]How to deploy an Azure function into a function app through a CI/CD pipeline?

我有一个 CI/CD 管道,用于创建基础结构以创建函数应用程序、存储容器和应用程序洞察到 azure 门户。 我在 Linux 中有一个 Azure 函数,我想知道如何在部署过程中将这个 azure 函数链接到函数应用程序。

这是我用来部署当前基础架构的 template.json:

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "subscriptionId": {
        "type": "string"
    },
    "name": {
        "type": "string"
    },
    "location": {
        "type": "string"
    },
    "hostingPlanName": {
        "type": "string"
    },
    "serverFarmResourceGroup": {
        "type": "string"
    },
    "alwaysOn": {
        "type": "bool"
    },
    "use32BitWorkerProcess": {
        "type": "bool"
    },
    "storageAccountName": {
        "type": "string"
    },
    "netFrameworkVersion": {
        "type": "string"
    },
    "sku": {
        "type": "string"
    },
    "skuCode": {
        "type": "string"
    },
    "workerSize": {
        "type": "string"
    },
    "workerSizeId": {
        "type": "string"
    },
    "numberOfWorkers": {
        "type": "string"
    }
},
"variables": {},
"resources": [
    {
        "apiVersion": "2018-11-01",
        "name": "[parameters('name')]",
        "type": "Microsoft.Web/sites",
        "kind": "functionapp",
        "location": "[parameters('location')]",
        "tags": {},
        "dependsOn": [
            "microsoft.insights/components/templateforarm",
            "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
            "[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
        ],
        "properties": {
            "name": "[parameters('name')]",
            "siteConfig": {
                "appSettings": [
                    {
                        "name": "FUNCTIONS_EXTENSION_VERSION",
                        "value": "~4"
                    },
                    {
                        "name": "FUNCTIONS_WORKER_RUNTIME",
                        "value": "node"
                    },
                    {
                        "name": "WEBSITE_NODE_DEFAULT_VERSION",
                        "value": "~14"
                    },
                    {
                        "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                        "value": "[reference('microsoft.insights/components/templateforarm', '2015-05-01').InstrumentationKey]"
                    },
                    {
                        "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                        "value": "[reference('microsoft.insights/components/templateforarm', '2015-05-01').ConnectionString]"
                    },
                    {
                        "name": "AzureWebJobsStorage",
                        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
                        
                    },
                    {
                        "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
                        "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
                    },
                    {
                        "name": "WEBSITE_CONTENTSHARE",
                        "value": "[concat(toLower(parameters('name')), 'a8cc')]"
                    }
                ],
                "use32BitWorkerProcess": "[parameters('use32BitWorkerProcess')]",
                "netFrameworkVersion": "[parameters('netFrameworkVersion')]"
            },
            "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
            "clientAffinityEnabled": false
        }
    },
    {
        "apiVersion": "2018-11-01",
        "name": "[parameters('hostingPlanName')]",
        "type": "Microsoft.Web/serverfarms",
        "location": "[parameters('location')]",
        "kind": "",
        "tags": {},
        "dependsOn": [],
        "properties": {
            "name": "[parameters('hostingPlanName')]",
            "workerSize": "[parameters('workerSize')]",
            "workerSizeId": "[parameters('workerSizeId')]",
            "numberOfWorkers": "[parameters('numberOfWorkers')]"
        },
        "sku": {
            "Tier": "[parameters('sku')]",
            "Name": "[parameters('skuCode')]"
        }
    },
    {
        "apiVersion": "2020-02-02-preview",
        "name": "templateforarm",
        "type": "microsoft.insights/components",
        "location": "westus2",
        "tags": {},
        "dependsOn": [],
        "properties": {
            "ApplicationId": "[parameters('name')]",
            "Request_Source": "IbizaWebAppExtensionCreate",
            "Flow_Type": "Redfield",
            "Application_Type": "web",
            "WorkspaceResourceId": "[concat('/subscriptions/', parameters('subscriptionId'), '/resourcegroups/defaultresourcegroup-wus2/providers/microsoft.operationalinsights/workspaces/defaultworkspace-', parameters('subscriptionId'),'-wus2')]"
        }
    },
    {
        "apiVersion": "2019-06-01",
        "type": "Microsoft.Storage/storageAccounts",
        "name": "[parameters('storageAccountName')]",
        "location": "[parameters('location')]",
        "tags": {},
        "sku": {
            "name": "Standard_LRS"
        },
        "properties": {
            "supportsHttpsTrafficOnly": true,
            "minimumTlsVersion": "TLS1_2"
        }
    }
]
}

这是我的 azure 函数、应用洞察、存储容器和函数应用基础架构的文件夹结构。

-azure
 -azure_functions
   -my_function
     -arm
       -dev.json
       -prod.json
       -template.json
     -my_function_code
       -index.json
       -function.json
     -host.json
     -local.settings.json
     -package-lock.json
     -package.json
     -proxies.json
     -release.yml

这是我用来部署应用洞察、存储容器和函数应用的 yml 文件。

trigger:
  none

pr:
  branches:
    include:
      - main
  paths:
    include:
        - azure/azure_functions/my_function/

stages:
- stage: Validation
  displayName: Validate Templates
  jobs:
  - job: Validate
    steps:
       - task: AzureResourceGroupDeployment@2
         displayName: "Validate Test"
         inputs:
            azureSubscription: "test-group-SPN"
            resourceGroupName: "test-group"
            location: "West US 2"
            csmFile: azure/azure_functions/my_function/arm/template.json
            csmParametersFile: 
            azure/azure_functions/my_function/arm/test.json
            deploymentMode: Validation

       - task: PublishBuildArtifacts@1
         displayName: "Publish Artifact: ARM Template"
         inputs:
             PathtoPublish: ./azure/azure_functions/my_function/arm
             ArtifactName: arm

- stage: TEST
  displayName: Deploy stage Test
  dependsOn: Build
  condition: succeeded()
  jobs:
     - deployment: Deploy
       displayName: Deploy
       environment: eventconnector-test           
       strategy:
          runOnce:
            deploy:
              steps:
               - task: AzureResourceManagerTemplateDeployment@3
                 displayName: 'Deploy App Insights, Storage Container & Function App'
                 inputs:
                     deploymentScope: 'Resource Group'
                     azureResourceManagerConnection: 'eventconnector-test-group-SPN'
                     subscriptionId: 'XXXX'
                     action: 'Create Or Update Resource Group'
                     resourceGroupName: 'eventconnector-test-group'
                     location: 'West US 2'
                     templateLocation: 'Linked artifact'
                     csmFile: '$(Pipeline.Workspace)/arm/template.json'
                     csmParametersFile: '$(Pipeline.Workspace)/arm/test.json'
                     deploymentMode: 'Incremental'

我可以创建另一个 yml 文件来部署我的 azure 函数,但这没有链接到我之前定义的当前函数应用程序。

trigger:
  none

pr:
  branches:
    include:
      - main
paths:
  include:
  - azure/azure_functions/my_function/

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: Build
    displayName: Build
    pool:
       vmImage: ubuntu-latest
    steps:
    - task: NodeTool@0
      inputs:
         versionSpec: '14.x'
      displayName: 'Install Node.js'

    - script: |
        npm install --prefix azure/azure_functions/my_function/

      displayName: 'Prepare binaries'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
         rootFolderOrFile: 'azure/azure_functions/my_function/'
         includeRootFolder: false
         archiveType: zip
         archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
         replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

  - stage: TEST
    displayName: Deploy stage Test
    dependsOn: Build
    condition: succeeded()
    jobs:
    - deployment: Deploy
      displayName: Deploy
      environment: eventconnector-test
      pool:
        vmImage: ubuntu-latest
      strategy:
         runOnce:
            deploy:
              steps:
                - task: AzureFunctionApp@1
                  displayName: 'Azure Functions App Deploy: '
                  inputs:
                  azureSubscription: 'test-group-SPN'
                  appType: functionAppLinux
                  appName: 'my_function'
                  package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

如何将这个 azure 函数链接到我在第一个 yml 文件中声明的函数应用程序中?

经过一番研究,我发现我只需要在我的 yml 文件末尾添加以下代码即可将我的 azure 函数部署到函数应用程序中:

- task: AzureCLI@1
  displayName: 'Deploy Azure function'
  inputs:
    azureSubscription: 'XXX'
    scriptType: 'ps'
    scriptLocation: 'inlineScript'
    inlineScript: |
     az functionapp deployment source config-zip -g XXX -n azure_func_app_name --src $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

下面是部署函数应用、应用洞察、存储和删除 azure 函数的最终 yml 代码。

trigger:
  none

pr:
  branches:
    include:
      - main
  paths:
    include:
        - azure/azure_functions/my_function/

stages:
    - stage: Validation
      displayName: Validate Templates
      jobs:
      - job: Validate
        steps:
          - task: AzureResourceGroupDeployment@2
          displayName: "Validate Test"
     inputs:
        azureSubscription: "test-group-SPN"
        resourceGroupName: "test-group"
        location: "West US 2"
        csmFile: azure/azure_functions/my_function/arm/template.json
        csmParametersFile: 
        azure/azure_functions/my_function/arm/test.json
        deploymentMode: Validation

   - task: PublishBuildArtifacts@1
        displayName: "Publish Artifact: ARM Template"
        inputs:
         PathtoPublish: ./azure/azure_functions/my_function/arm
         ArtifactName: arm

    - stage: TEST
       displayName: Deploy stage Test
       dependsOn: Build
       condition: succeeded()
       jobs:
        - deployment: Deploy
          displayName: Deploy
          environment: eventconnector-test           
          strategy:
          runOnce:
          deploy:
          steps:
           - task: AzureResourceManagerTemplateDeployment@3
             displayName: 'Deploy App Insights, Storage Container & Function App'
             inputs:
                 deploymentScope: 'Resource Group'
                 azureResourceManagerConnection: 'eventconnector-test-group-SPN'
                 subscriptionId: 'XXXX'
                 action: 'Create Or Update Resource Group'
                 resourceGroupName: 'eventconnector-test-group'
                 location: 'West US 2'
                 templateLocation: 'Linked artifact'
                 csmFile: '$(Pipeline.Workspace)/arm/template.json'
                 csmParametersFile: '$(Pipeline.Workspace)/arm/test.json'
                 deploymentMode: 'Incremental'

          - task: AzureCLI@1
            displayName: 'Deploy Azure function'
            inputs:
            azureSubscription: 'XXX'
            scriptType: 'ps'
            scriptLocation: 'inlineScript'
            inlineScript: |
                az functionapp deployment source config-zip -g XXX -n azure_func_app_name --src $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM