Azure DevOps CI/CD 部署 Web 或 Function App 更改 YAML 管道中 appsettings 中的值

Azure DevOps CI/CD Deploy Web or Function App changing the values in appsettings in YAML Pipelines

最近在我们的一些部署中,关于如何自动化功能应用程序和 Web 应用程序的应用程序设置在内部存在一些混乱,并且检查周围似乎有大量令人眼花缭乱的选项,看起来它们在做大致相同的事情事情,但在不同的步骤。

我们的开发人员通常有一个 appsettings.json 他们提交给 repo 的文件,可能看起来像这样用于他们的测试...

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
    }
  },
  "Values": {
    "ThingToPointTo": "http://localhost",
  }
}

当我们把它带到其他环境时,例如PROD,我们将 ThingToPointTo 更改为“https://productionservice”

我们一直在使用 Azure DevOps YAML 管道以这种方式部署和更改 AppSettings...

- task: AzureFunctionApp@1
  inputs:
    azureSubscription: 'OurAzureSubServiceConnection'
    appType: functionApp
    appName: $(azfuncappname)
    package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
    AppSettings: '-Values:ThingToPointTo "https://productionservice"'

我的问题是2倍

  1. Values:ThingToPointTo 是否正确枚举到正确的设置,或者它应该只是 ThingToPointTo(省略 值:) ?

  2. 是这样吗?我注意到有 JSON 转换步骤可以用来在部署之前更改实际文件,还有一个名为“Azure App Service Settings”的任务可以在部署后使用?

关于这个主题的文章很多,但是none似乎很合适。

提前致谢!

Is the Values:ThingToPointTo correct for enumerating to the correct setting, or should it just be ThingToPointTo (omitting the Values:) ?

ThingToPointTo :https://productionservice 可能是正确的格式。您不需要添加 values.

例如:

- task: AzureFunctionApp@1
  displayName: 'Azure Function App Deploy: kevin0806'
  inputs:
    azureSubscription: '7.28-8.28'
    appType: functionApp
    appName: kevin
    appSettings: '-ThingToPointTo http://localhost'

结果:

Is this the way to do it? I notice there are JSON transform steps you can use to change the actual file before deploying it, and also a task called "Azure App Service Settings" available to use that will do it after deployment?

Azure App Service Settings用于部署后更改设置。

这是模板,您可以参考一下:

steps:
- task: AzureRmWebAppDeployment@4
....

- task: AzureAppServiceSettings@1
  displayName: 'Azure App Service Settings: kevin0608'
  inputs:
    azureSubscription: '7.28-8.28'
    appName: kevin0608
    resourceGroupName: Kevintest
    appSettings: |
     [
        {
         "name": "ThingToPointTo",
         "value": "valueabcd",
         "slotSetting": false
        }
     
     ]

这里有一篇关于Json Transform的文档,你也可以参考一下

此外,您可以查看 :

Settings from appsettings.json are not displayed in Azure App Service Configuration, but >settings defined there override values in appsettings.json

任务中的 appsettings 配置可以显示在 azure 应用程序服务配置中,并且可以覆盖 appsettings.json 中的值。

更新:

以上是没有嵌套变量的情况

如果变量是嵌套值,可以按照下面的结构:

"first" : {
  "second": {
    "third" : "value"
  }
}



-first.second.third value1

如果您的应用服务是 linux,您可以使用 __ 替换 .

例如- first__second__third value1

注:变量名匹配为case-sensitive

感谢@Kevin Lu-MSFT 与我讨论这些选项

我们发现对于具有嵌套值的网络应用...例如

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
    }
  },
  "MySettings": {
    "ThingToPointTo": "http://localhost",
  }
}

...AZDO YAML 管道中的 AppSettings 确实是...

- task: AzureFunctionApp@1
  inputs:
    azureSubscription: 'OurAzureSubServiceConnection'
    appType: functionApp
    appName: $(azfuncappname)
    package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
    AppSettings: '-MySettings:ThingToPointTo "https://productionservice"'

对于功能应用程序来说,这是不同的!

如果 JSON 中有“值”,则不要使用值:枚举 !!!!...

例如...

{
  "Values": {
    "ThingToPointTo": "http://localhost",
  }
}

...最终成为...

AppSettings: '-ThingToPointTo "https://productionservice"'

好像有double-standard功能应用!所以当心 (其中大部分是使用 .Net Core 和 Windows 在 Azure 中适用的设置完成的)