使用 Azure Pipelines 从应用程序配置中拉取设置Pull settings to App Configuration with Azure Pipelines

Azure 应用程序配置任务将从应用程序配置存储中拉取键值,并将其设置为 Azure 管道变量,这些变量可由后续任务使用。The Azure App Configuration task pulls key-values from your App Configuration store and sets them as Azure pipeline variables, which can be consumed by subsequent tasks. 此任务是对 Azure 应用程序配置推送任务的补充,后者可将配置文件中的键值推送到应用程序配置存储中。This task complements the Azure App Configuration Push task that pushes key-values from a configuration file into your App Configuration store. 有关详细信息,请参阅使用 Azure Pipelines 将设置推送到应用程序配置For more information, see Push settings to App Configuration with Azure Pipelines.

先决条件Prerequisites

创建服务连接Create a service connection

通过服务连接,可以从 Azure DevOps 项目访问 Azure 订阅中的资源。A service connection allows you to access resources in your Azure subscription from your Azure DevOps project.

  1. 在 Azure DevOps 中,访问包含目标管道的项目,并打开左下方的“项目设置”。In Azure DevOps, go to the project containing your target pipeline and open the Project settings at the bottom left.
  2. 在“管道”下,选择“服务连接” 。Under Pipelines select Service connections.
  3. 如果你没有任何现有的服务连接,请单击屏幕中间的“创建服务连接”按钮。If you don't have any existing service connections, click the Create service connection button in the middle of the screen. 否则,请单击页面右上方的“新建服务连接”。Otherwise, click New service connection in the top right of the page.
  4. 选择“Azure 资源管理器”。Select Azure Resource Manager.
  5. 选择“服务主体(自动)”。Select Service principal (automatic).
  6. 填写你的订阅和资源。Fill in your subscription and resource. 为服务连接命名。Give your service connection a name.

创建服务连接后,请查找分配给它的服务主体的名称。Now that your service connection is created, find the name of the service principal assigned to it. 在下一步中,你将向此服务主体添加新的角色分配。You'll add a new role assignment to this service principal in the next step.

  1. 访问“项目设置” > “服务连接”。Go to Project Settings > Service connections.
  2. 选择在上一部分创建的服务连接。Select the service connection that you created in the previous section.
  3. 选择“管理服务主体”。Select Manage Service Principal.
  4. 请记下列出的显示名称。Note the Display name listed.

添加角色分配Add role assignment

向任务中使用的服务连接分配正确的应用程序配置角色,以便任务可以访问应用程序配置存储。Assign the proper App Configuration role to the service connection being used within the task so that the task can access the App Configuration store.

  1. 导航到目标应用配置存储。Navigate to your target App Configuration store. 有关设置应用程序配置存储的演练,请参阅 Azure 应用程序配置快速入门之一中的创建应用程序配置存储For a walkthrough of setting up an App Configuration store, see Create an App Configuration store in one of the Azure App Configuration quickstarts.
  2. 在左侧,选择“访问控制(IAM)”。On the left, select Access control (IAM).
  3. 在顶部,选择“+ 添加”,然后选取“添加角色分配” 。At the top, select + Add and pick Add role assignment.
  4. 在“角色”下,选择“应用程序配置数据读取者” 。Under Role, select App Configuration Data Reader. 此角色允许任务在应用程序配置存储中进行读取。This role allows the task to read from the App Configuration store.
  5. 选择与在上一部分创建的服务连接关联的服务主体。Select the service principal associated with the service connection that you created in the previous section.

备注

若要解析应用程序配置中的 Azure Key Vault 引用,还必须授予服务连接读取引用的 Azure Key Vault 中机密的权限。To resolve Azure Key Vault references within App Configuration, the service connection must also be granted permission to read secrets in the referenced Azure Key Vaults.

在生成中使用Use in builds

本部分介绍如何在 Azure DevOps 生成管道中使用“Azure 应用程序配置”任务。This section will cover how to use the Azure App Configuration task in an Azure DevOps build pipeline.

  1. 通过单击“管道” > “管道”,导航到“生成管道”页。Navigate to the build pipeline page by clicking Pipelines > Pipelines. 有关生成管道的文档,请参阅创建你的第一个管道For build pipeline documentation, see Create your first pipeline.
    • 如果要创建新的生成管道,请单击“新建管道”,然后为管道选择存储库。If you're creating a new build pipeline, click New pipeline, select the repository for your pipeline. 选择管道右侧的“显示助手”,然后搜索“Azure 应用程序配置”任务。Select Show assistant on the right side of the pipeline, and search for the Azure App Configuration task.
    • 如果使用现有生成管道,请选择“编辑”以编辑管道。If you're using an existing build pipeline, select Edit to edit the pipeline. 在“任务”选项卡中,搜索“Azure 应用程序配置”任务。In the Tasks tab, search for the Azure App Configuration Task.
  2. 配置任务的必要参数,以便从应用程序配置存储中拉取键值。Configure the necessary parameters for the task to pull the key-values from the App Configuration store. 参数说明在下面的“参数”部分以及每个参数旁边的工具提示中提供。Descriptions of the parameters are available in the Parameters section below and in tooltips next to each parameter.
    • 将“Azure 订阅”参数设置为你在前面步骤中创建的服务连接的名称。Set the Azure subscription parameter to the name of the service connection you created in a previous step.
    • 将“应用程序配置名称”设置为应用程序配置存储的资源名称。Set the App Configuration name to the resource name of your App Configuration store.
    • 保留其余参数的默认值。Leave the default values for the remaining parameters.
  3. 保存并将一个生成排入队列。Save and queue a build. 生成日志将显示在执行任务期间发生的任何故障。The build log will display any failures that occurred during the execution of the task.

在发布中使用Use in releases

本部分介绍如何在 Azure DevOps 发布管道中使用“Azure 应用程序配置”任务。This section will cover how to use the Azure App Configuration task in an Azure DevOps release pipeline.

  1. 通过选择“管道” > “发布”,导航到“发布管道”页。Navigate to release pipeline page by selecting Pipelines > Releases. 有关发布管道的文档,请参阅发布管道For release pipeline documentation, see Release pipelines.
  2. 选择现有的发布管道。Choose an existing release pipeline. 如果没有,请单击“新建管道”创建一个新的发布管道。If you don’t have one, click New pipeline to create a new one.
  3. 选择右上角的“编辑”按钮以编辑发布管道。Select the Edit button in the top-right corner to edit the release pipeline.
  4. 选择“阶段”以添加任务。Choose the Stage to add the task. 有关阶段的详细信息,请参阅添加阶段、依赖项和条件For more information about stages, see Add stages, dependencies, & conditions.
  5. 为“在代理上运行”单击 + ,然后在“添加任务”选项卡下添加“Azure 应用程序配置”任务。Click + for on "Run on agent", then add the Azure App Configuration task under the Add tasks tab.
  6. 配置任务内的必要参数,以便从应用程序配置存储中拉取键值。Configure the necessary parameters within the task to pull your key-values from your App Configuration store. 参数说明在下面的“参数”部分以及每个参数旁边的工具提示中提供。Descriptions of the parameters are available in the Parameters section below and in tooltips next to each parameter.
    • 将“Azure 订阅”参数设置为你在前面步骤中创建的服务连接的名称。Set the Azure subscription parameter to the name of the service connection you created in a previous step.
    • 将“应用程序配置名称”设置为应用程序配置存储的资源名称。Set the App Configuration name to the resource name of your App Configuration store.
    • 保留其余参数的默认值。Leave the default values for the remaining parameters.
  7. 保存并将一个发布排入队列。Save and queue a release. 发布日志将显示任务执行过程中遇到的任何故障。The release log will display any failures encountered during the execution of the task.

参数Parameters

“Azure 应用程序配置”任务使用以下参数:The following parameters are used by the Azure App Configuration task:

  • Azure 订阅:包含可用 Azure 服务连接的下拉列表。Azure subscription: A drop-down containing your available Azure service connections. 若要更新和刷新可用 Azure 服务连接的列表,请按文本框右侧的“刷新 Azure 订阅”按钮。To update and refresh your list of available Azure service connections, press the Refresh Azure subscription button to the right of the textbox.
  • 应用配置名称:用于在所选订阅下加载可用配置存储的下拉列表。App Configuration Name: A drop-down that loads your available configuration stores under the selected subscription. 若要更新和刷新可用配置存储的列表,请按文本框右侧的“刷新应用配置名称”按钮。To update and refresh your list of available configuration stores, press the Refresh App Configuration Name button to the right of the textbox.
  • 键筛选器:该筛选器可用于选择从 Azure 应用程序配置中请求哪些键值。Key Filter: The filter can be used to select what key-values are requested from Azure App Configuration. 值 * 将选择所有键值。A value of * will select all key-values. 有关详细信息,请参阅查询键值For more information on, see Query key values.
  • 标签:指定从应用程序配置存储中选择键值时应使用的标签。Label: Specifies which label should be used when selecting key-values from the App Configuration store. 如果未提供标签,则将检索带有 no 标签的键值。If no label is provided, then key-values with the no label will be retrieved. 不允许使用以下字符:, *。The following characters are not allowed: , *.
  • 剪裁键前缀:指定在将应用程序配置键设置为变量之前,应从中剪裁的一个或多个前缀。Trim Key Prefix: Specifies one or more prefixes that should be trimmed from App Configuration keys before setting them as variables. 多个前缀可以用换行符分隔。Multiple prefixes can be separated by a new-line character.

在后续任务中使用键值Use key-values in subsequent tasks

从应用程序配置中提取的键值将设置为管道变量,可以作为环境变量进行访问。The key-values that are fetched from App Configuration are set as pipeline variables, which are accessible as environment variables. 环境变量的键是剪裁前缀后从应用程序配置中检索的键值的键(如果指定)。The key of the environment variable is the key of the key-value that is retrieved from App Configuration after trimming the prefix, if specified.

例如,如果后续任务运行 PowerShell 脚本,则它可能会使用包含“myBuildSetting”键的键值,如下所示:For example, if a subsequent task runs a PowerShell script, it could consume a key-value with the key 'myBuildSetting' like this:

echo "$env:myBuildSetting"

值将输出到控制台。And the value will be printed to the console.

疑难解答Troubleshooting

如果发生意外错误,可以通过将管道变量 system.debug 设置为 true 来启用调试日志。If an unexpected error occurs, debug logs can be enabled by setting the pipeline variable system.debug to true.

FAQFAQ

如何根据多个键和标签撰写配置?How do I compose my configuration from multiple keys and labels?

有时可能需要根据多个标签撰写配置,例如 default 和 dev。There are times when configuration may need to be composed from multiple labels, for example, default and dev. 可以在一个管道中使用多个应用程序配置任务来实现此方案。Multiple App Configuration tasks may be used in one pipeline to implement this scenario. 任务在后面步骤中提取的键值将取代前面步骤中的所有值。The key-values fetched by a task in a later step will supersede any values from previous steps. 在上述示例中,可以使用一个任务来选择具有 default 标签的键值,而第二个任务可以选择具有 dev 标签的键值。In the aforementioned example, a task can be used to select key-values with the default label while a second task can select key-values with the dev label. 具有 dev 标签的键将替代具有 default 标签的相同键。The keys with the dev label will override the same keys with the default label.