向 Azure IoT Edge 进行持续集成和持续部署Continuous integration and continuous deployment to Azure IoT Edge

借助 Azure Pipelines 中的内置 Azure IoT Edge 任务,可以轻松地在 Azure IoT Edge 应用程序中采用 DevOps。You can easily adopt DevOps with your Azure IoT Edge applications with the built-in Azure IoT Edge tasks in Azure Pipelines. 本文演示了如何使用 Azure Pipelines 的持续集成和持续部署功能,快速高效地生成和测试应用程序并将其部署到 Azure IoT Edge。This article demonstrates how you can use the continuous integration and continuous deployment features of Azure Pipelines to build, test, and deploy applications quickly and efficiently to your Azure IoT Edge.

示意图 - 用于开发和生产的 CI 和 CD 分支

在本文中,你将学习如何使用 Azure Pipelines 的内置 Azure IoT Edge 任务为 IoT Edge 解决方案创建两个管道。In this article, you learn how to use the built-in Azure IoT Edge tasks for Azure Pipelines to create two pipelines for your IoT Edge solution. 可以在 Azure IoT Edge 任务中使用四项操作。There are four actions can be used in the Azure IoT Edge tasks.

  • Azure IoT Edge - 生成模块映像使用 IoT Edge 解决方案代码并生成容器映像。Azure IoT Edge - Build Module images takes your IoT Edge solution code and builds the container images.
  • Azure IoT Edge - 推送模块映像将模块映像推送到指定的容器注册表。Azure IoT Edge - Push Module images pushes module images to the container registry you specified.
  • Azure IoT Edge - 生成部署清单使用 deployment.template.json 文件和变量来生成最终的 IoT Edge 部署清单文件。Azure IoT Edge - Generate Deployment Manifest takes a deployment.template.json file and the variables, then generates the final IoT Edge deployment manifest file.
  • Azure IoT Edge - 部署到 IoT Edge 设备用于创建 IoT Edge 部署,此类部署可部署到单个/多个 IoT Edge 设备。Azure IoT Edge - Deploy to IoT Edge devices helps create IoT Edge deployments to single/multiple IoT Edge devices.

先决条件Prerequisites

  • Azure Repos 存储库。An Azure Repos repository. 如果没有存储库,可在项目中创建一个新的 Git 存储库If you don't have one, you can Create a new Git repo in your project.

  • 提交 IoT Edge 解决方案并将其推送到存储库。An IoT Edge solution committed and pushed to your repository. 如果要为测试本文创建新的示例解决方案,请按照在 Visual Studio Code 中开发和调试模块在 Visual Studio 中开发和调试 C# 模块中的步骤进行操作。If you want to create a new sample solution for testing this article, follow the steps in Develop and debug modules in Visual Studio Code or Develop and debug C# modules in Visual Studio.

    对于本文,你只需要 Visual Studio Code 或 Visual Studio 中的 IoT Edge 模板创建的解决方案文件夹。For this article, all you need is the solution folder created by the IoT Edge templates in either Visual Studio Code or Visual Studio. 在继续操作之前,无需生成、推送、部署或调试此代码。You don't need to build, push, deploy, or debug this code before proceeding. 你将在 Azure Pipelines 中设置这些进程。You'll set those processes up in Azure Pipelines.

    如果要创建新解决方案,请首先在本地克隆存储库。If you're creating a new solution, clone your repository locally first. 然后,在创建解决方案时,可以选择直接在存储库文件夹中创建它。Then, when you create the solution you can choose to create it directly in the repository folder. 可以轻松从中提交和推送新文件。You can easily commit and push the new files from there.

  • 容器注册表,你可以在其中推送模块图像。A container registry where you can push module images. 可使用 Azure 容器注册表或第三方注册表。You can use Azure Container Registry or a third-party registry.

  • 具有至少 IoT Edge 设备的活动 IoT 集线器,用于测试单独的测试和生产部署阶段。An active IoT hub with at least IoT Edge devices for testing the separate test and production deployment stages. 可按照快速入门文章在 LinuxWindows 上创建 IoT Edge 设备You can follow the quickstart articles to create an IoT Edge device on Linux or Windows

配置持续集成Configure continuous integration

在本部分中,将创建新的生成管道。In this section, you create a new build pipeline. 管道配置,使其在用户向 IoT Edge 解决方案签入任何更改时自动运行,并发布生成日志。Configure the pipeline to run automatically when you check in any changes to the sample IoT Edge solution, and publish build logs.

备注

本文使用 Azure DevOps 可视化设计器。This article uses the Azure DevOps visual designer. 在按照本部分中的步骤操作之前,请关闭新 YAML 管道创建体验的预览功能。Before you follow the steps in this section, turn off the preview feature for the new YAML pipeline creation experience.

  1. 在 Azure DevOps 中,选择个人资料图标,然后选择“预览功能”。In Azure DevOps, select your profile icon then select Preview features.
  2. 关闭“新的 YAML 管道创建体验”。Turn New YAML pipeline creation experience off.

有关详细信息,请参阅创建生成管道For more information, see Create a build pipeline.

  1. 登录 Azure DevOps 组织 (https://dev.azure.com/{your organization}/) 并打开包含 IoT Edge 解决方案存储库的项目。Sign in to your Azure DevOps organization (https://dev.azure.com/{your organization}/) and open the project that contains your IoT Edge solution repository.

    在本文中,我们创建了名为“IoTEdgeRepo”的存储库。For this article, we created a repository called IoTEdgeRepo. 该存储库包含“IoTEdgeSolution”,其中包含名为“filtermodule”的模块代码 。That repository contains IoTEdgeSolution which has the code for a module named filtermodule.

    打开 DevOps 项目

  2. 导航到项目中的 Azure Pipelines。Navigate to Azure Pipelines in your project. 打开“生成”选项卡,然后选择“新建管道” 。Open the Builds tab and select New pipeline. 或者,如果已有生成管道,则选择“新建”按钮。Or, if you already have build pipelines, select the New button. 然后选择“新建生成管道”。Then choose New build pipeline.

    创建新的生成管道

  3. 按照提示创建管道。Follow the prompts to create your pipeline.

    1. 提供新生成管道的源信息。Provide the source information for your new build pipeline. 选择“Azure Repos Git”作为源,然后选择 IoT Edge 解决方案代码所在的项目、存储库和分支。Select Azure Repos Git as the source, then select the project, repository, and branch where your IoT Edge solution code is located. 然后选择“继续”。Then, select Continue.

      选择管道源

    2. 选择“空作业”而不是模板。Select Empty job instead of a template.

      从空进程开始

  4. 创建管道后,将转到管道编辑器。Once your pipeline is created, you are taken to the pipeline editor. 在管道描述中,根据目标平台选择正确的代理池:In your pipeline description, choose the correct agent pool based on your target platform:

    • 若要在用于 Linux 容器的 amd64 平台中生成模块,请选择“托管 Ubuntu 1604”If you would like to build your modules in platform amd64 for Linux containers, choose Hosted Ubuntu 1604

    • 如果想在平台 amd64 中为 Windows 1809 容器生成模块,则需要在 Windows 上设置自托管代理If you would like to build your modules in platform amd64 for Windows 1809 containers, you need to set up self-hosted agent on Windows.

    • 如果想在平台 arm32v7 或 arm64 中为 Linux 容器生成模块,则需要在 Linux 上设置自托管代理If you would like to build your modules in platform arm32v7 or arm64 for Linux containers, you need to set up self-hosted agent on Linux.

      配置生成代理池

  5. 管道预先配置了名为“代理作业 1”的作业。Your pipeline comes preconfigured with a job called Agent job 1. 选择加号 (+),向作业添加三个任务:“Azure IoT Edge”两次,“复制文件”一次,“发布生成项目”一次 。Select the plus sign (+) to add three tasks to the job: Azure IoT Edge twice, Copy Files once and Publish Build Artifacts once. (将鼠标悬停在每个任务的名称上,以查看“添加”按钮。)(Hover over the name of each task to see the Add button.)

    添加 Azure IoT Edge 任务

    添加所有四个任务后,代理作业如下所示:When all four tasks are added, your Agent job looks like the following example:

    生成管道中的三个任务

  6. 选择第一个“Azure IoT Edge”任务进行编辑。Select the first Azure IoT Edge task to edit it. 此任务通过指定的目标平台生成解决方案中的所有模块。This task builds all modules in the solution with the target platform that you specify.

    • 显示名称:接受默认的“Azure IoT Edge - 生成模块图像”。Display name: Accept the default Azure IoT Edge - Build module images.
    • 操作:接受默认的“生成模块图像”。Action: Accept the default Build module images.
    • .template.json 文件:选择省略号 (...) 并导航到包含 IoT Edge 解决方案的存储库中的“deployment.template.json”文件 。.template.json file: Select the ellipsis (...) and navigate to the deployment.template.json file in the repository that contains your IoT Edge solution.
    • 默认平台:根据目标 IoT Edge 设备为模块选择相应的平台。Default platform: Select the appropriate platform for your modules based on your target IoT Edge device.
    • 输出变量:输出变量包含引用名称,可使用该名称配置生成 deployment.json 文件的文件路径。Output variables: The output variables include a reference name that you can use to configure the file path where your deployment.json file will be generated. 将引用名称设置为令人难忘的内容,如“边缘”。Set the reference name to something memorable like edge.

    这些配置使用 module.json 文件中定义的映像存储库和标记来命名和标记模块映像。These configurations use the image repository and tag that are defined in the module.json file to name and tag the module image. “生成模块映像”也有助于将变量替换为在 module.json 文件中定义的具体值。Build module images also helps replace the variables with the exact value you define in the module.json file. 在 Visual Studio 或 Visual Studio Code 中,你是在 .env 文件中指定实际值。In Visual Studio or Visual Studio Code, you are specifying the actual value in a .env file. 在 Azure Pipelines 中,请在“管道变量”选项卡中设置值。选择“变量”选项卡,并按如下所示配置名称和值:In Azure Pipelines, you set the value on the Pipeline Variables tab. Select the Variables tab and configure the name and value as following:

    • ACR_ADDRESS:你的 Azure 容器注册表地址。ACR_ADDRESS: Your Azure Container Registry address.

    如果你在项目中有其他变量,可以在此选项卡上指定名称和值。“生成模块映像”仅识别 ${VARIABLE} 格式的变量。If you have other variables in your project, you can specify the name and value on this tab. Build module images recognizes only variables that are in ${VARIABLE} format. 请确保在 **/module.json 文件中使用此格式。Make sure you use this format in your **/module.json files.

  7. 选择第二个“Azure IoT Edge”任务进行编辑。Select the second Azure IoT Edge task to edit it. 此任务会把所有模块图像推送到所选容器注册表。This task pushes all module images to the container registry that you select.

    • 显示名称:操作字段更改时,显示名称会自动更新。Display name: The display name is automatically updated when the action field changes.
    • 操作:使用下拉列表选择“推送模块图像”。Action: Use the dropdown list to select Push module images.
    • 容器注册表类型:选择用于存储模块图像的容器注册表类型。Container registry type: Select the type of container registry that you use to store your module images. 窗体会根据选择的注册表类型进行更改。Depending on which registry type you choose, the form changes. 如果选择“Azure 容器注册表”,请使用下拉列表选择 Azure 订阅和容器注册表的名称。If you choose Azure Container Registry, use the dropdown lists to select the Azure subscription and the name of your container registry. 如果选择“通用容器注册表”,请选择“新建”以创建注册表服务连接 。If you choose Generic Container Registry, select New to create a registry service connection.
    • .template.json 文件:选择省略号 (...) 并导航到包含 IoT Edge 解决方案的存储库中的“deployment.template.json”文件 。.template.json file: Select the ellipsis (...) and navigate to the deployment.template.json file in the repository that contains your IoT Edge solution.
    • 默认平台:选择与生成的模块图像相同的平台。Default platform: Select the same platform as your built module images.

    如果有多个用于托管模块映像的容器注册表,则需重复此任务,选择其他容器注册表,并使用高级设置中的“绕过模块”来绕过不适用于此特定注册表的映像。If you have multiple container registries to host your module images, you need to duplicate this task, select different container registry, and use Bypass module(s) in the advanced settings to bypass the images which are not for this specific registry.

  8. 选择“复制文件”任务进行编辑。Select the Copy Files task to edit it. 使用此任务将文件复制到项目暂存目录。Use this task to copy files to the artifact staging directory.

    • 显示名称:将文件复制到:放置文件夹。Display name: Copy Files to: Drop folder.
    • 内容:在此节中放置两行:deployment.template.json**/module.jsonContents: Put two lines in this section, deployment.template.json and **/module.json. 这两类文件是生成 IoT Edge 部署清单所需的输入。These two types of files are the inputs to generate IoT Edge deployment manifest. 需复制到项目暂存文件夹并进行发布,用于发布管道。Need to be copied to the artifact staging folder and published for release pipeline.
    • 目标文件夹:放置变量 $(Build.ArtifactStagingDirectory)Target Folder: Put the variable $(Build.ArtifactStagingDirectory). 请参阅生成变量,了解相关说明。See Build variables to learn about the description.
  9. 选择“发布生成项目”任务进行编辑。Select the Publish Build Artifacts task to edit it. 提供到任务的项目暂存目录路径,使该路径可以发布到发布管道。Provide artifact staging directory path to the task so that the path can be published to release pipeline.

    • 显示名称:发布项目:放置。Display name: Publish Artifact: drop.
    • 要发布的路径:放置变量 $(Build.ArtifactStagingDirectory)Path to publish: Put the variable $(Build.ArtifactStagingDirectory). 请参阅生成变量,了解相关说明。See Build variables to learn about the description.
    • 项目名称:放置。Artifact name: drop.
    • 项目发布位置:Azure Pipelines。Artifact publish location: Azure Pipelines.
  10. 打开“触发器”标签,然后选中“启用持续集成”框 。Open the Triggers tab and check the box to Enable continuous integration. 确保包含代码的分支已包括在内。Make sure the branch containing your code is included.

    开启“持续集成”触发器

  11. 使用“保存”按钮保存新的生成管道。Save the new build pipeline with Save button.

此管道现配置为在将新代码推送到存储库时自动运行。This pipeline is now configured to run automatically when you push new code to your repo. 发布管道项目的最后一项任务是触发发布管道。The last task, publishing the pipeline artifacts, triggers a release pipeline. 继续下一部分以生成发布管道。Continue to the next section to build the release pipeline.

配置持续部署Configure continuous deployment

在本部分中,会创建一个发布管道,此管道配置为在生成管道放置项目时自动运行,并且它将在 Azure Pipelines 中显示部署日志。In this section, you create a release pipeline that is configured to run automatically when your build pipeline drops artifacts, and it will show deployment logs in Azure Pipelines.

创建新管道,然后添加新阶段Create a new pipeline, and add a new stage

  1. 在“发布”选项卡上,选择“+ 新键管道” 。In the Releases tab, choose + New pipeline. 或者,如果已有发布管道,则选择“+ 新建”按钮并选择“+ 新建发布管道” 。Or, if you already have release pipelines, choose the + New button and select + New release pipeline.

    添加发布管道

  2. 提示选择模板时,选择以“空作业”开头。When prompted to select a template, choose to start with an Empty job.

    从空作业开始

  3. 新版本管道初始化为一个阶段,称为“阶段 1”。Your new release pipeline initializes with one stage, called Stage 1. 将阶段 1 重命名为“dev”,并将其视为测试环境。Rename Stage 1 to dev and treat it as a test environment. 通常情况下,持续部署管道具有多个阶段,包括 devstagingprod。可以根据 DevOps 实践创建更多内容。Usually, continuous deployment pipelines have multiple stages including dev, staging and prod. You can create more based on your DevOps practice. 重命名后关闭阶段详细信息窗口。Close the stage details window once it's renamed.

  4. 将发布链接到生成管道发布的生成项目。Link the release to the build artifacts that are published by the build pipeline. 在项目区域中,单击“添加”。Click Add in artifacts area.

    添加项目

  5. 在“添加项目”页中,选择源类型“生成” 。In Add an artifact page, select source type Build. 然后选择项目和创建的生成管道。Then, select the project and the build pipeline you created. 然后选择“添加”。Then, select Add.

    添加生成项目

  6. 打开项目触发器并选择切换以启用持续部署触发器。Open the artifact triggers and select the toggle to enable the continuous deployment trigger. 现在,每次有新的生成时,都会创建新版本。Now, a new release will be created each time a new build is available.

    配置“持续部署”触发器

  7. “dev”阶段预先配置了一个作业和零个任务。The dev stage is preconfigured with one job and zero tasks. 从管道菜单中,选择“任务”,然后选择“dev”阶段 。From the pipeline menu, select Tasks then choose the dev stage. 选择作业和任务计数以在此阶段配置任务。Select the job and task count to configure the tasks in this stage.

    配置 dev 任务

  8. dev 阶段,会看到默认的“代理作业”。In the dev stage, you should see a default Agent job. 可配置有关代理作业的详细信息,但部署任务对平台不敏感,因此可在“代理池”(或你自己管理的任何其他代理)中使用“托管 VS2017”或“托管 Ubuntu 1604” 。You can configure details about the agent job, but the deployment task is platform insensitive so you can use either Hosted VS2017 or Hosted Ubuntu 1604 in the Agent pool (or any other agent managed by yourself).

  9. 选择加号 (+),添加两个任务。Select the plus sign (+) to add two task. 搜索并添加“Azure IoT Edge”两次。Search for and add Azure IoT Edge twice.

    添加 dev 任务

  10. 选择第一个 Azure IoT Edge 任务,并使用以下值对其进行配置:Select the first Azure IoT Edge task and configure it with the following values:

    • 显示名称:操作字段更改时,显示名称会自动更新。Display name: The display name is automatically updated when the action field changes.
    • 操作:使用下拉列表选择“生成部署清单”。Action: Use the dropdown list to select Generate deployment manifest. 更改操作值还会更新要匹配的任务显示名称。Changing the action value also updates the task display name to match.
    • .template.json 文件:放置路径 $(System.DefaultWorkingDirectory)/Drop/drop/deployment.template.json.template.json file: Put the path $(System.DefaultWorkingDirectory)/Drop/drop/deployment.template.json. 路径从生成管道发布。The path is published from build pipeline.
    • 默认平台:生成模块映像时,选择同一值。Default platform: Choose the same value when building the module images.
    • 输出路径:放置路径 $(System.DefaultWorkingDirectory)/Drop/drop/configs/deployment.jsonOutput path: Put the path $(System.DefaultWorkingDirectory)/Drop/drop/configs/deployment.json. 此路径是最终的 IoT Edge 部署清单文件。This path is the final IoT Edge deployment manifest file.

    这些配置有助于替换 deployment.template.json 文件中的模块映像 URL。These configurations helps replace the module image URLs in the deployment.template.json file. “生成部署清单”也有助于将变量替换为在 deployment.template.json 文件中定义的具体值。The Generate deployment manifest also helps replace the variables with the exact value you defined in the deployment.template.json file. 在 VS/VS Code 中,你是在 .env 文件中指定实际值。In VS/VS Code, you are specifying the actual value in a .env file. 在 Azure Pipelines 中,请在“发布管道变量”选项卡中设置值。转到“变量”选项卡,配置“名称”和“值”如下。In Azure Pipelines, you set the value in Release Pipeline Variables tab. Move to Variables tab and configure the Name and Value as following.

    • ACR_ADDRESS:你的 Azure 容器注册表地址。ACR_ADDRESS: Your Azure Container Registry address.
    • ACR_PASSWORD:你的 Azure 容器注册表密码。ACR_PASSWORD: Your Azure Container Registry password.
    • ACR_USER:你的 Azure 容器注册表用户名。ACR_USER: Your Azure Container Registry username.

    如果你在项目中有其他变量,可以在此选项卡中指定名称和值。“生成部署清单”只能识别 ${VARIABLE} 样式的变量,确保你在 *.template.json 文件中使用此样式。If you have other variables in your project, you can specify the name and value in this tab. The Generate deployment manifest can only recognize the variables are in ${VARIABLE} flavor, make sure you are using this in your *.template.json files.

    配置发布管道的变量

  11. 选择第二个 Azure IoT Edge 任务,并使用以下值对其进行配置:Select the second Azure IoT Edge task and configure it with the following values:

    • 显示名称:操作字段更改时,显示名称会自动更新。Display name: The display name is automatically updated when the action field changes.
    • 操作:使用下拉列表选择“部署到 IoT Edge 设备”。Action: Use the dropdown list to select Deploy to IoT Edge devices. 更改操作值还会更新要匹配的任务显示名称。Changing the action value also updates the task display name to match.
    • Azure 订阅:选择包含 IoT 中心的订阅。Azure subscription: Select the subscription that contains your IoT Hub.
    • IoT 中心名称:选择 IoT 中心。IoT Hub name: Select your IoT hub.
    • 选择单个/多个设备:选择是否要将发布管道部署到一个设备或多个设备。Choose single/multiple device: Choose whether you want the release pipeline to deploy to one device or multiple devices.
      • 如果部署到单个设备,请输入“IoT Edge设备 ID”。If you deploy to a single device, enter the IoT Edge device ID.
      • 如果要部署到多个设备,请指定设备“目标条件”。If you are deploying to multiple devices, specify the device target condition. 目标条件是用于在 IoT 中心匹配一组 IoT Edge 设备的筛选器。The target condition is a filter to match a set of IoT Edge devices in IoT Hub. 若想将设备标记用作条件,则需要使用 IoT 中心设备孪生更新对应的设备标记。If you want to use Device Tags as the condition, you need to update your corresponding devices Tags with IoT Hub device twin. 在高级设置中更新“IoT Edge 部署 ID”和“IoT Edge 部署优先级” 。Update the IoT Edge deployment ID and IoT Edge deployment priority in the advanced settings. 有关为多个设备创建部署的详细信息,请参阅了解 IoT Edge 自动部署For more information about creating a deployment for multiple devices, see Understand IoT Edge automatic deployments.
    • 展开“高级设置”,选择“IoT Edge 部署 ID”,然后放置变量 $(System.TeamProject)-$(Release.EnvironmentName)Expand Advanced Settings, select IoT Edge deployment ID, put the variable $(System.TeamProject)-$(Release.EnvironmentName). 这样会将项目和发布名称映射为你的 IoT Edge 部署 ID。This maps the project and release name with your IoT Edge deployment ID.
  12. 选择“保存”,将更改保存到新发布管道。Select Save to save your changes to the new release pipeline. 从菜单中选择“管道”,返回管道视图。Return to the pipeline view by selecting Pipeline from the menu.

使用生成和发布管道验证 IoT Edge CI/CDVerify IoT Edge CI/CD with the build and release pipelines

要触发生成作业,可以将提交推送到源代码存储库或手动触发该作业。To trigger a build job, you can either push a commit to source code repository or manually trigger it. 在本部分中,手动触发 CI/CD 管道以测试它是否正常工作。In this section, you manually trigger the CI/CD pipeline to test that it works. 然后验证部署是否成功。Then verify that the deployment succeeds.

  1. 导航到在本文开头创建的生成管道。Navigate to the build pipeline that you created at the beginning of this article.

  2. 通过选择“队列”按钮,可在生成管道中触发生成作业(如以下屏幕截图所示)。You can trigger a build job in your build pipeline by selecting the Queue button as in following screenshot.

    手动触发器

  3. 选择生成作业以查看其进度。Select the build job to watch its progress. 如果生成管道成功完成,则会触发到“dev”阶段的发布。If the build pipeline is completed successfully, it triggers a release to dev stage.

    生成日志

  4. dev 发布成功以后,会创建 IoT Edge 部署,部署到目标 IoT Edge 设备。The successful dev release creates IoT Edge deployment to target IoT Edge devices.

    发布到 dev

  5. 单击 dev 阶段即可查看发布日志。Click dev stage to see release logs.

    发布日志

后续步骤Next steps