【问题标题】:Is there a workaround to keep app settings which not defined in Bicep template?是否有解决方法来保留 Bicep 模板中未定义的应用程序设置?
【发布时间】:2022-10-13 15:08:04
【问题描述】:

主二头肌


resource appService 'Microsoft.Web/sites@2020-06-01' = {
  name: webSiteName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
    siteConfig: {
      linuxFxVersion: linuxFxVersion

      appSettings: [
        {
          name: 'ContainerName'
          value: 'FancyContainer'
        }
        {
          name: 'FancyUrl'
          value: 'fancy.api.com'
        }
      ]
    }
  }
}

基础结构发布过程成功运行,并且应用程序设置设置正确,之后我运行节点应用程序构建和发布,其中 Azure DevOps 发布管道将一些与应用程序相关的配置添加到应用程序设置。 (例如 API 密钥、API URL),一切都很好。

但是如果我必须重新发布基础架构,例如,我使用存储帐户扩展我的环境,应用程序发布设置的应用程序设置就会丢失。

是否有解决方法来保留未在 Bicep 模板中定义的应用程序设置?

【问题讨论】:

  • 并不真地。如果在主文件中不存在 BiCep 文件之外的任何更改,则它们将被重置。过去我使用 AzDo 的方法是在 IaC 部署之前查询 appSettings,然后在部署后将它们传递回应用程序。

标签: azure azure-web-app-service azure-resource-manager azure-bicep


【解决方案1】:

来自这篇文章:Merge App Settings With Bicep

  1. 部署时不要在siteConfig 中包含appSettings
  2. 创建一个模块来创建/更新应用程序设置,它将现有设置与新设置合并。

    appsettings.二头肌文件:

    param webAppName string
    param appSettings object
    param currentAppSettings object
    
    resource siteconfig 'Microsoft.Web/sites/config@2022-03-01' = {
      name: '${webAppName}/appsettings'
      properties: union(currentAppSettings, appSettings)
    }
    

    主二头肌:

    param webAppName string
    ...
    
    // Create the webapp without appsettings
    resource webApp 'Microsoft.Web/sites@2022-03-01' = {
      name: webAppName
      ...
      properties:{    
        ... 
        siteConfig: {
          // Dont include the appSettings
        }
      }
    }
    
    // Create-Update the webapp app settings.
    module appSettings 'appsettings.bicep' = {
      name: '${webAppName}-appsettings'
      params: {
        webAppName: webApp.name
        // Get the current appsettings
        currentAppSettings: list(resourceId('Microsoft.Web/sites/config', webApp.name, 'appsettings'), '2022-03-01').properties
        appSettings: {
          Foo: 'Bar'
        }
      }
    }
    

【讨论】:

  • 如果我在它自己的模块中有 webApp 资源并从 main.bicep 调用 webApp 和 appSettings 模块,那么我在列表函数上得到一个错误:“这个表达式正在函数“列表”的参数中使用,这需要可以在部署开始时计算的值。”
  • @OliverNilsen 您将 webapp 名称 / id 作为 webapp 模块的输出?
  • 对,就是这样。所以 main.bicep 部署了定义 webApp 资源的模块。然后,我立即部署 appSettings 模块并提供 webAppName: webApp.outputs.name 作为输入参数。似乎函数 list() 只能在 webApp 资源模块中使用,而不能在 main.bicep 中使用。
  • 您总是可以让 main 调用 webapp 模块和 webapp 模块调用 appsettings 模块吗?根据我的回答,我的主要成为您的 webapp 模块。
  • 这是个好主意。我没有想到那个。
猜你喜欢
  • 2015-07-05
  • 2019-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2015-09-13
相关资源
最近更新 更多