使用 Github Actions 进行持续交付Continuous delivery by using GitHub Action

使用 GitHub Actions 定义一个工作流,以便自动生成代码并将其部署到 Azure 函数应用。Use GitHub Actions to define a workflow to automatically build and deploy code to your Azure function app.

在 GitHub Actions 中,工作流是在 GitHub 存储库中定义的自动化过程。In GitHub Actions, a workflow is an automated process that you define in your GitHub repository. 此过程告知 GitHub 如何在 GitHub 中生成和部署函数应用项目。This process tells GitHub how to build and deploy your functions app project on GitHub.

工作流通过存储库的 /.github/workflows/ 路径中的 YAML (.yml) 文件定义。A workflow is defined by a YAML (.yml) file in the /.github/workflows/ path in your repository. 此定义包含组成工作流的各种步骤和参数。This definition contains the various steps and parameters that make up the workflow.

对于 Azure Functions 工作流,此文件有三个部分:For an Azure Functions workflow, the file has three sections:

部分Section 任务Tasks
身份验证Authentication 下载发布配置文件。Download a publish profile.
创建 GitHub 机密。Create a GitHub secret.
生成Build 设置环境。Set up the environment.
生成函数应用。Build the function app.
部署Deploy 部署函数应用。Deploy the function app.

先决条件Prerequisites

生成部署凭据Generate deployment credentials

对于 GitHub Actions,建议使用发布配置文件向 Azure Functions 进行身份验证。The recommended way to authenticate with Azure Functions for GitHub Actions is by using a publish profile. 还可以使用服务主体进行验证。You can also authenticate with a service principal. 若要了解详细信息,请参阅此 GitHub Actions 存储库To learn more, see this GitHub Actions repository.

将你的发布配置文件凭据保存为 GitHub 机密后,将在工作流中使用此机密向 Azure 进行身份验证。After saving your publish profile credential as a GitHub secret, you'll use this secret within your workflow to authenticate with Azure.

下载你的发布配置文件Download your publish profile

若要下载函数应用的发布配置文件:To download the publishing profile of your function app:

  1. 选择函数应用的“概述”页,然后选择“获取发布配置文件” 。Select the function app's Overview page, and then select Get publish profile.

    下载发布配置文件

  2. 保存并复制该文件的内容。Save and copy the contents of the file.

添加 GitHub 机密Add the GitHub secret

  1. GitHub 中浏览到存储库,选择“设置” > “机密” > “添加新机密”。In GitHub, browse to your repository, select Settings > Secrets > Add a new secret.

    添加机密

  2. 添加新机密的方法如下:使用 AZURE_FUNCTIONAPP_PUBLISH_PROFILE 作为“名称”,使用发布配置文件的内容作为“值”,然后选择“添加机密” 。Add a new secret using AZURE_FUNCTIONAPP_PUBLISH_PROFILE for Name, the content of the publishing profile file for Value, and then select Add secret.

GitHub 现在可以针对 Azure 中的函数应用进行身份验证了。GitHub can now authenticate to your function app in Azure.

创建环境Create the environment

设置环境是使用特定于语言的发布设置操作完成的。Setting up the environment is done using a language-specific publish setup action.

.NET(包括 ASP.NET)使用 actions/setup-dotnet 操作。.NET (including ASP.NET) uses the actions/setup-dotnet action.
以下示例显示了设置环境的工作流部分:The following example shows the part of the workflow that sets up the environment:

    - name: Setup DotNet 2.2.402 Environment
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 2.2.402

生成函数应用Build the function app

这取决于语言。对于 Azure Functions 支持的语言,应该可以将此部分视为每种语言的标准生成步骤。This depends on the language and for languages supported by Azure Functions, this section should be the standard build steps of each language.

以下示例显示了生成函数应用的工作流部分,该部分是特定于语言的:The following example shows the part of the workflow that builds the function app, which is language-specific:

    env:
      AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root

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

部署函数应用Deploy the function app

使用 Azure/functions-action 操作将代码部署到函数应用。Use the Azure/functions-action action to deploy your code to a function app. 此操作有三个参数:This action has three parameters:

参数Parameter 说明Explanation
app-nameapp-name (必需)函数应用的名称。(Mandatory) The name of your function app.
slot-nameslot-name (可选)要部署到其中的部署槽的名称。(Optional) The name of the deployment slot you want to deploy to. 该槽必须已经在函数应用中定义。The slot must already be defined in your function app.
publish-profilepublish-profile (可选)发布配置文件的 GitHub 机密的名称。(Optional) The name of the GitHub secret for your publish profile.

以下示例使用 functions-action 版本 1 和 publish profile 进行身份验证The following example uses version 1 of the functions-action and a publish profile for authentication

设置使用发布配置文件的 .NET Linux 工作流。Set up a .NET Linux workflow that uses a publish profile.

name: Deploy DotNet project to Azure function app with a Linux environment

on:
  [push]

env:
  AZURE_FUNCTIONAPP_NAME: your-app-name  # set this to your application's name
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'    # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '2.2.402'              # set this to the dotnet version to use

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

    - 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: bash
      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: ${{ env.AZURE_FUNCTIONAPP_NAME }}
        package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
        publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}

设置使用发布配置文件的 .NET Windows 工作流。Set up a .NET Windows workflow that uses a publish profile.

name: Deploy DotNet project to Azure function app with a Windows environment

on:
  [push]

env:
  AZURE_FUNCTIONAPP_NAME: your-app-name  # set this to your application's name
  AZURE_FUNCTIONAPP_PACKAGE_PATH: '.'    # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '2.2.402'              # set this to the dotnet version to use

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

    - 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: ${{ env.AZURE_FUNCTIONAPP_NAME }}
        package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
        publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }}

后续步骤Next steps