与 CI/CD 管道集成Integrate with a CI/CD pipeline

本文介绍如何在持续集成和持续部署系统中使用来自 Azure 应用程序配置的数据。This article explains how to use data from Azure App Configuration in a continuous integration and continuous deployment system.

在 Azure DevOps 管道中使用应用程序配置Use App Configuration in your Azure DevOps Pipeline

如果你有一个 Azure DevOps 管道,则可以从应用程序配置提取键值,并将其设置为任务变量。If you have an Azure DevOps Pipeline, you can fetch key-values from App Configuration and set them as task variables. Azure 应用程序配置 DevOps 扩展是可以提供此功能的加载项模块。The Azure App Configuration DevOps extension is an add-on module that provides this functionality. 请按照其说明在生成或发布任务序列中使用该扩展。Follow its instructions to use the extension in a build or release task sequence.

使用应用程序部署应用程序配置数据Deploy App Configuration data with your application

如果应用程序依赖于 Azure 应用程序配置且无法访问它,则应用程序可能无法运行。Your application may fail to run if it depends on Azure App Configuration and cannot reach it. 通过将配置数据打包到与应用程序一起部署并在应用程序启动期间本地加载的文件中来提高应用程序的复原能力。Enhance the resiliency of your application by packaging configuration data into a file that's deployed with the application and loaded locally during application startup. 此方法可保证应用程序在启动时具有默认设置值。This approach guarantees that your application has default setting values on startup. 当应用程序配置存储区可用时,这些值将被应用程序配置存储区中的任何较新的更改覆盖。These values are overwritten by any newer changes in an App Configuration store when it's available.

使用 Azure 应用程序配置的导出功能,可以将当前配置数据的检索过程自动化为单个文件。Using the Export function of Azure App Configuration, you can automate the process of retrieving current configuration data as a single file. 然后,可以将此文件嵌入到持续集成和持续部署 (CI/CD) 管道的生成或部署步骤中。You can then embed this file in a build or deployment step in your continuous integration and continuous deployment (CI/CD) pipeline.

下面的示例演示如何将应用程序配置数据包含在快速入门中引入的 Web 应用程序的生成步骤中。The following example shows how to include App Configuration data as a build step for the web app introduced in the quickstarts. 在继续操作之前,请先完成使用应用程序配置创建 ASP.NET Core 应用Before you continue, finish Create an ASP.NET Core app with App Configuration first.

你可以使用任何代码编辑器执行本教程中的步骤。You can use any code editor to do the steps in this tutorial. Visual Studio Code 是 Windows、macOS 和 Linux 平台上提供的一个卓越选项。Visual Studio Code is an excellent option available on the Windows, macOS, and Linux platforms.

先决条件Prerequisites

如果在本地生成,请下载并安装 Azure CLI(如果尚未安装)。If you build locally, download and install the Azure CLI if you haven’t already.

要进行云生成,例如使用 Azure DevOps,请确保在生成系统中安装了 Azure CLITo do a cloud build, with Azure DevOps for example, make sure the Azure CLI is installed in your build system.

导出应用程序配置存储区Export an App Configuration store

  1. 打开 .csproj 文件,然后添加以下脚本:Open your .csproj file, and add the following script:

    <Target Name="Export file" AfterTargets="Build">
        <Message Text="Export the configurations to a temp file. " />
        <Exec WorkingDirectory="$(MSBuildProjectDirectory)" Condition="$(ConnectionString) != ''" Command="az appconfig kv export -d file --path $(OutDir)\azureappconfig.json --format json --separator : --connection-string $(ConnectionString)" />
    </Target>
    
  2. 打开 Program.cs 并更新 CreateWebHostBuilder 方法以通过调用 config.AddJsonFile() 方法使用导出的 JSON 文件。Open Program.cs, and update the CreateWebHostBuilder method to use the exported JSON file by calling the config.AddJsonFile() method. 此外,添加 System.Reflection 命名空间。Add the System.Reflection namespace as well.

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var directory = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var settings = config.Build();
    
                config.AddJsonFile(Path.Combine(directory, "azureappconfig.json"));
                config.AddAzureAppConfiguration(settings["ConnectionStrings:AppConfig"]);
            })
            .UseStartup<Startup>();
    

在本地生成并运行应用Build and run the app locally

  1. 设置名为“ConnectionString”的环境变量,并将其设置为应用程序配置存储区的访问密钥。Set an environment variable named ConnectionString, and set it to the access key to your App Configuration store. 如果使用 Windows 命令提示符,则请运行以下命令并重启命令提示符,这样更改才会生效:If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:

     setx ConnectionString "connection-string-of-your-app-configuration-store"
    

    如果使用 Windows PowerShell,请运行以下命令:If you use Windows PowerShell, run the following command:

     $Env:ConnectionString = "connection-string-of-your-app-configuration-store"
    

    如果使用 macOS 或 Linux,则请运行以下命令:If you use macOS or Linux, run the following command:

     export ConnectionString='connection-string-of-your-app-configuration-store'
    
  2. 要通过使用 .NET Core CLI 生成应用,请在命令行界面中执行以下命令:To build the app by using the .NET Core CLI, run the following command in the command shell:

     dotnet build
    
  3. 生成成功完成后,请运行以下命令以在本地运行 Web 应用:After the build successfully completes, run the following command to run the web app locally:

     dotnet run
    
  4. 启动浏览器窗口并转到 http://localhost:5000,即本地托管的 Web 应用的默认 URL。Open a browser window and go to http://localhost:5000, which is the default URL for the web app hosted locally.

    本地启动应用快速入门

后续步骤Next steps

在本教程中,导出了要在部署管道中使用的 Azure 应用程序配置数据。In this tutorial, you exported Azure App Configuration data to be used in a deployment pipeline. 若要了解有关如何使用应用程序配置的更多信息,请继续阅读 Azure CLI 示例。To learn more about how to use App Configuration, continue to the Azure CLI samples.