You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从文件部署应用设置至Azure标准逻辑应用?

问题

我正尝试将应用设置部署至Azure标准逻辑应用,希望从文件读取应用设置而非直接写入YAML文件。目前使用Azure Logic Apps DevOps任务部署工作流等内容,其他环节均正常,但应用设置从文件读取部署的需求无法实现。

曾尝试读取存储应用设置的JSON文件传递给部署任务但未成功;也尝试用az logicapp config appsettings set命令以文件作为输入,但该命令文档未提及文件输入(az functionapp config appsettings支持@filename格式,但仅适用于Function App)。

当前模板YAML配置了两种部署方式:

  1. 通过AzureLogicAppsStandardRelease任务,此方式可正常工作但需在主YAML中声明应用设置字符串;
  2. 最后一步用Azure CLI尝试读取文件部署,但未生效。

当前模板YAML代码如下:

parameters:
- name: logicAppName
  type: string
- name: parametersSubFolder
  type: string
- name: resourceGroupName
  type: string
- name: appSettings
  type: object
  default: []


jobs:
- job: logic_app_build
  displayName: 'Build and publish logic app'

  - task: AzureLogicAppsStandardBuild@0
    displayName: 'Azure Logic Apps Standard Build'
    inputs:
      sourceFolder: '$(Build.SourcesDirectory)'
      deploymentFolder: '$(System.DefaultWorkingDirectory)/deployment/'
      deploymentWorkflowParametersFile: '$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/parameters.json'
      archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'

  #deployar zip-filen
  - task: AzureLogicAppsStandardRelease@0
    displayName: 'Azure Logic Apps Standard Release'
    inputs:
        connectedServiceName: "serviceConnection"
        appName : '${{ parameters.resourceGroupName }}-${{ parameters.logicAppName }}'
        resourceGroupName: '${{ parameters.resourceGroupName }}'
        appSettings: |
          ${{ convertToJson(parameters.appSettings) }}
        package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'

  - task: AzureCLI@2
    inputs:
      azureSubscription:  "sc-sp-pi-${{ parameters.resourceGroupName }}-logicapps-deployment"
      scriptType: 'pscore'
      scriptLocation: 'inlineScript'
      inlineScript: |
        /usr/bin/az logicapp config appsettings set --resource-group ${{ parameters.resourceGroupName }} --name ${{ parameters.resourceGroupName }}-${{ parameters.logicAppName }} --settings "@$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/AppSettings.txt"

请问有什么可行的实现方法?是否只能将应用设置直接写入YAML文件?

可行方案

方案1:通过PowerShell读取JSON文件并传递给部署任务

先新增PowerShell任务读取JSON格式的应用设置文件,将内容转为JSON字符串存入变量,再在AzureLogicAppsStandardRelease任务中引用该变量:

  1. 添加读取文件的PowerShell任务:
- task: PowerShell@2
  displayName: 'Load App Settings from JSON'
  inputs:
    targetType: 'inline'
    script: |
      $appSettingsContent = Get-Content -Path "$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/appsettings.json" -Raw
      Write-Host "##vso[task.setvariable variable=AppSettingsJson;]$appSettingsContent"
  1. 修改AzureLogicAppsStandardRelease任务的appSettings输入:
- task: AzureLogicAppsStandardRelease@0
  displayName: 'Azure Logic Apps Standard Release'
  inputs:
      connectedServiceName: "serviceConnection"
      appName : '${{ parameters.resourceGroupName }}-${{ parameters.logicAppName }}'
      resourceGroupName: '${{ parameters.resourceGroupName }}'
      appSettings: $(AppSettingsJson)
      package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'

方案2:改进Azure CLI命令,手动解析文件内容

由于az logicapp config appsettings set不支持@filename格式,可以通过脚本读取文件内容,将其转为命令可接受的参数格式后传递:

若文件为键值对格式(每行KEY=VALUE):

- task: AzureCLI@2
  inputs:
    azureSubscription:  "sc-sp-pi-${{ parameters.resourceGroupName }}-logicapps-deployment"
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $settingsLines = Get-Content -Path "$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/AppSettings.txt"
      $settingsArgs = $settingsLines | ForEach-Object { "--settings $_" }
      az logicapp config appsettings set --resource-group ${{ parameters.resourceGroupName }} --name ${{ parameters.resourceGroupName }}-${{ parameters.logicAppName }} $settingsArgs

若文件为JSON格式:

- task: AzureCLI@2
  inputs:
    azureSubscription:  "sc-sp-pi-${{ parameters.resourceGroupName }}-logicapps-deployment"
    scriptType: 'pscore'
    scriptLocation: 'inlineScript'
    inlineScript: |
      $appSettings = Get-Content -Path "$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/appsettings.json" -Raw | ConvertFrom-Json
      $settingsArgs = @()
      foreach ($key in $appSettings.PSObject.Properties.Name) {
          $settingsArgs += "--settings $key=$($appSettings.$key)"
      }
      az logicapp config appsettings set --resource-group ${{ parameters.resourceGroupName }} --name ${{ parameters.resourceGroupName }}-${{ parameters.logicAppName }} $settingsArgs

方案3:使用ARM模板部署应用设置

创建ARM模板定义应用设置资源,再通过模板部署任务读取参数文件完成部署:

  1. 编写ARM模板文件(比如appsettings-template.json):
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "logicAppName": {
      "type": "string"
    },
    "appSettings": {
      "type": "object"
    }
  },
  "resources": [
    {
      "type": "Microsoft.Web/sites/config",
      "name": "[concat(parameters('logicAppName'), '/appsettings')]",
      "apiVersion": "2022-03-01",
      "properties": "[parameters('appSettings')]"
    }
  ]
}
  1. 添加ARM模板部署任务:
- task: AzureResourceManagerTemplateDeployment@3
  displayName: 'Deploy App Settings via ARM'
  inputs:
    deploymentScope: 'Resource Group'
    azureResourceManagerConnection: "serviceConnection"
    resourceGroupName: '${{ parameters.resourceGroupName }}'
    templateLocation: 'Linked artifact'
    csmFile: '$(System.DefaultWorkingDirectory)/appsettings-template.json'
    csmParametersFile: '$(System.DefaultWorkingDirectory)/deployment/workflowparameters/${{ parameters.parametersSubFolder }}/appsettings-params.json'
    deploymentMode: 'Incremental'

内容的提问来源于stack exchange,提问作者Emma Gustafsson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 19:04:50