【问题标题】:How to deploy Azure function from c# or github?如何从 c# 或 github 部署 Azure 功能?
【发布时间】:2022-12-15 10:59:28
【问题描述】:

我在寻找的道路上找到的最新答案已有 5 年历史,不再适用。

有没有办法通过 REST、Azure.ResourceManager 或其他“不是”Azure 或 Visual Studio 标准方式的方式将 c# 中的函数应用程序发布到其他环境?

我需要能够完全自动化部署和发布。至此,所有的方法都进入了死胡同。

如果 github 是答案,它如何在与其他环境零交互的情况下实现自动化?

【问题讨论】:

  • 希望这会帮助你。 link
  • 谢谢,我会看看是否可以将其集成到我当前的设计中。

标签: c# azure azure-functions azure-resource-manager


【解决方案1】:
  • 我使用以下步骤使用 ARM 模板将函数应用程序部署到 Azure 门户和 Git-Hub 以部署 HTTP 触发器

  • 打开 Azure 门户并搜索自定义部署。

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
        "_generator": {
            "name": "bicep",
      "version": "0.5.6.12127",
      "templateHash": "10848576439099634716"
        }
    },
  "parameters": {
        "appName": {
            "type": "string",
      "defaultValue": "[format('fnapp{0}', uniqueString(resourceGroup().id))]",
      "metadata": {
                "description": "function app that you want to create."
      }
        },
    "storageAccountType": {
            "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
                "description": "Storage Account type"
      }
        },
    "location": {
            "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
                "description": "Location for all resources."
      }
        },
    "appInsightsLocation": {
            "type": "string",
      "metadata": {
                "description": "Location for Application Insights"
      }
        },
    "runtime": {
            "type": "string",
      "defaultValue": "node",
      "allowedValues": [
        "node",
        "dotnet",
        "java"
      ],
      "metadata": {
                "description": "runtime to load in the function app."
      }
        }
    },
  "variables": {
        "functionAppName": "[parameters('appName')]",
    "hostingPlanName": "[parameters('appName')]",
    "applicationInsightsName": "[parameters('appName')]",
    "storageAccountName": "[format('{0}azfunctions', uniqueString(resourceGroup().id))]",
    "functionWorkerRuntime": "[parameters('runtime')]"
  },
  "resources": [
    {
        "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-08-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
            "name": "[parameters('storageAccountType')]"
      },
      "kind": "Storage"
    },
    {
        "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2021-03-01",
      "name": "[variables('hostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
            "name": "Y1",
        "tier": "Dynamic"
      },
      "properties": { }
    },
    {
        "type": "Microsoft.Web/sites",
      "apiVersion": "2021-03-01",
      "name": "[variables('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp",
      "identity": {
            "type": "SystemAssigned"
      },
      "properties": {
            "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "siteConfig": {
                "appSettings": [
                  {
                    "name": "AzureWebJobsStorage",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2021-08-01').keys[0].value)]"
                  },
            {
                    "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[format('DefaultEndpointsProtocol=https;AccountName={0};EndpointSuffix={1};AccountKey={2}', variables('storageAccountName'), environment().suffixes.storage, listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2021-08-01').keys[0].value)]"
            },
            {
                    "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(variables('functionAppName'))]"
            },
            {
                    "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~4"
            },
            {
                    "name": "WEBSITE_NODE_DEFAULT_VERSION",
              "value": "~10"
            },
            {
                    "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))).InstrumentationKey]"
            },
            {
                    "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[variables('functionWorkerRuntime')]"
            }
          ],
          "ftpsState": "FtpsOnly",
          "minTlsVersion": "1.2"
        },
        "httpsOnly": true
      },
      "dependsOn": [
        "[resourceId('Microsoft.Insights/components', variables('applicationInsightsName'))]",
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
        "type": "Microsoft.Insights/components",
      "apiVersion": "2020-02-02",
      "name": "[variables('applicationInsightsName')]",
      "location": "[parameters('appInsightsLocation')]",
      "kind": "web",
      "properties": {
            "Application_Type": "web",
        "Request_Source": "rest"
      }
    }
  ]
}
  • 部署到 Azure 门户后,您将看到如下所示

  • 转到Github-> 创建一个空存储库
  • 创建一个新建文件夹在本地系统中
  • 后藤视觉工作室代码->文件->打开文件夹-> 选择文件夹名称并单击打开并按照以下说明

  • 单击“浏览”并选择您创建的文件夹,如下所示

  • 选择C#

  • 选择.NET 6.0

  • 选择 HTTP 触发器

  • 输入HTTP 触发器名称点击进入

  • 输入你想要的名字然后点击回车

  • 选择匿名的

  • 将代码推送到您创建的 Git-Hub 存储库。

  • 将项目推送到存储库后转到Azure 门户-> 想要添加HTTP触发器的Function App -> 选择部署中心-> 连接您的 Git-Hub 帐户和您要添加的存储库的详细信息,如下所示

  • 连接存储库后,azure 将在 git 存储库中创建一个工作流文件夹,如下所示

  • 现在转到动作并检查部署是否成功,如下所示

  • 成功部署 Goto 后Azure 门户->函数应用->功能如下

  • 下面是我用来在 Azure 门户中将 Http 触发器部署到 Azure 函数的 yml 文件

  • 请检查下面yml文件中的DOTNET_VERSION格式

更新

# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy dotnet core app to Azure Function App - function app name

on:
  push:
    branches:
      - main
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@v2

      - name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: 'Resolve Project Dependencies Using Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet build --configuration Release --output ./output
          popd

      - name: 'Run Azure Functions Action'
        uses: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'Function app name'
          slot-name: 'Production'
          package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE }}

【讨论】:

  • github 凭据必须以编程方式导入到部署中。 Azure 中的零用户输入。
  • 当您自动将 git 操作添加到 azure functions 时,触发器将发送到该存储库并自动部署到 azure functions
  • 我添加了下面的图片以显示我们可以在 git hub 中看到触发器部署更新的位置
  • 那么来自另一个环境的具有不同名称的新功能应用程序将如何使用具有 0 人机交互的相同 .yml 文件?
  • 我们必须提供一些信息才能从 github 获取 yml 文件,我们必须执行一些步骤。请转到您的 azure 函数的部署中心,并提供 GitHub 帐户名称、存储库和节点名称,然后单击保存
猜你喜欢
  • 2017-08-17
  • 2021-01-19
  • 2020-02-22
  • 1970-01-01
  • 2020-07-30
  • 2022-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多