gpt4 book ai didi

AzureRmWebAppDeployment 任务中的 Azure : appsettings. json 变量替换

转载 作者:行者123 更新时间:2023-12-02 06:40:56 31 4
gpt4 key购买 nike

在我的 azure 管道中,我曾经通过以下方式替换 appsettings.json 文件中的变量:

  1. 我的 appsettings 文件定义带有标记值的变量

     {
    "AzureAd": {
    "Instance": "__AAD_Instance__",
    "Domain": "__AAD_Domain__",
    "TenantId": "__AAD_TenantId__",
    "ClientId": "__AAD_AuthorPortalApi_ClientId__"
    }
  2. 在该版本中,定义了管道变量

enter image description here

  • 标记生成器任务在发布管道的开头运行,并用正确的值替换标记。
  • 请注意,不同应用程序中的某些变量是相似的。例如,上面我有两个应用程序(AuthorPortalApi 和 FileApi),它们都有一个 AzureAD ClientId,因此我必须在 token 中包含应用程序的名称,以便将其替换为正确的值。

    这一切都运行得很好,但现在我正在更改为使用 YAML 发布管道。唯一的区别是现在变量是在专用的 YAML 文件中定义的:

      variables:
    - name: AAD_FilesApi_ClientId
    value: some value

    我可以保持这种方式。但我注意到部署 azure webapp AzureRmWebAppDeployment 的任务提供了一种在 appsettings.json 中进行变量替换的方法:

      - task: AzureRmWebAppDeployment@4
    displayName: 'Deploy my api'
    inputs:
    azureSubscription: 'my sub'
    WebAppName: 'my-api'
    packageForLinux: '$(workFolder)/my-api.zip'
    JSONFiles: '**/appsettings.json'

    在变量 yaml 文件中,我定义了如下变量:

    variables:
    - name: AzureAd.ClientId
    value: some value

    它有效,但就像我上面说的,如果两个应用程序在 appsettings.json 中具有相同的 json 结构,那么如何区分它们以替换为正确的值?

    我看到了documentation ,但它没有展示如何在 YAML 模板中定义变量。

    最佳答案

    if two applications have the same json structure in appsettings.json, then how do I differentiate them to replace by the correct values ?

    我们可以为具有不同变量的管道创建两个作业模板yaml。

    模板AuthorPortalApi.yml:

    jobs:
    - job: AuthorPortalApi
    variables:
    AAD_AuthorPortalApi_ClientId: xxx

    模板FilesApi.yml:

    jobs:
    - job: FilesApi
    variables:
    AAD_FilesApi_ClientId: xxx

    您可以查看文档Define variables:Variable scopes了解更多详细信息。

    另一方面,我们也可以只使用 Runtime parameters 的一个任务模板。 :

    parameters:
    - name: ClientId
    displayName: AzureAd.ClientId
    type: string
    default: Vlaue_AAD_AuthorPortalApi_ClientId
    values:
    - Value_AAD_FilesApi_ClientId
    - Value_OhterThings_ClientId

    jobs:
    - job: build
    displayName: build
    pool:
    vmImage: ubuntu-latest
    steps:
    - script: echo The value is ${{ parameters.ClientId }}

    选择管道排队时的值或使用默认值Vlaue_AAD_AuthorPortalApi_ClientId:

    enter image description here

    测试结果:

    enter image description here

    更新:

    如果以上方法都不适合你,那么我能想到的方法就是使用模板参数,但是这种方法需要我们找到一个条件来将不同的参数传递给模板:

    jobs:
    - template: ClientId.yml
    parameters:
    ClientId: AAD_AuthorPortalApi_ClientId
    condition: and(succeeded(), eq(variables['xx.xx'], 'xx'))

    - template: ClientId.yml
    parameters:
    ClientId: AAD_FilesApi_ClientId
    condition: and(succeeded(), eq(variables['yy.yy'], 'yy'))

    查看此文档 Job, stage, and step templates with parameters了解更多详细信息。

    关于AzureRmWebAppDeployment 任务中的 Azure : appsettings. json 变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62854070/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com