0

我之前在这里问过如何使用 Azure KeyVault 进行变量替换,并且能够让它大部分工作,除了最后一个问题。由于某种未知的原因,替换完美地发生了,但它在末尾添加了一个额外的单引号。由于这些是 web.config 中的连接字符串,因此额外的单引号会破坏它。我不知道是什么添加了额外的单引号。我做了四次检查 KeyVault 以确保它没有错字。FileTransform@2我已经尝试按照我上一个问题的答案所推荐的IISWebAppDeploymentOnMachineGroup@0任务以及XmlVariableSubstitution设置为 true的任务进行 XML 变量替换。两者都添加了额外的单引号

管道运行之前的 web.config 示例(这是签入 Git 的内容):

<connectionStrings>
  <add name="DbConnection" connectionString="Placeholder"/>
  ...
</connectionStrings>

在管道完成 XML 变量替换后

<connectionStrings>
  <add name="DbConnection" connectionString="DataSource=TheDatabase;CheckConnectionOnOpen=true;UserId=MyUser;Password=ThePassword;'"/>
  ...
</connectionStrings>

如您所见,它正确连接到 Azure KeyVault,获取值并进行替换。就是“ThePassword;”后面的那个额外的单引号;这使得应用程序无法解析连接字符串。

这是我的管道片段:

- task: AzureKeyVault@1
  displayName: 'Get secrets from KeyVault'
  inputs:
    azureSubscription: '${{parameters.keyVault.keyVaultServiceConnection}}'
    KeyVaultName: '${{parameters.keyVault.keyVaultName}}'
    SecretsFilter: '*'
# KeyVault has an app name prefix for each connection string as well as an environment name postfix so this loops removes that prefix so the transformation can match the names/keys properly
- ${{ each secret in parameters.keyVault.secrets }}:
    - task: CmdLine@2
      displayName: 'Set KeyVault secret to match config name'
      inputs:
        script: echo ##vso[task.setvariable variable=${{secret.configSecretName}}]$(${{secret.secretName}}-${{parameters.environment}})
- task: IISWebAppManagementOnMachineGroup@0
  displayName: 'Set up app pool and web site'
  inputs:
    IISDeploymentType: 'IISWebsite'
    ActionIISWebsite: 'CreateOrUpdateWebsite'
    WebsiteName: '${{parameters.webSiteName}}'
    WebsitePhysicalPath: '${{parameters.webSitePhysicalPathRoot}}'
    WebsitePhysicalPathAuth: 'WebsiteUserPassThrough'
    CreateOrUpdateAppPoolForWebsite: true
    AppPoolNameForWebsite: '${{parameters.webAppPool}}'
    DotNetVersionForWebsite: '${{parameters.webAppPoolDotNetVersion}}'
    PipeLineModeForWebsite: '${{parameters.pipeLineModeForWebsite}}'
    AppPoolIdentityForWebsite: '${{parameters.appPoolIdentityForWebsite}}'
- task: IISWebAppDeploymentOnMachineGroup@0
  displayName: 'Deploy web site'
  inputs:
    WebSiteName: '${{parameters.webSiteName}}'
    VirtualApplication: '${{parameters.webAppName}}'
    Package: '$(System.ArtifactsDirectory)\*.zip'
    RemoveAdditionalFilesFlag: ${{parameters.removeAdditionalFiles}} # Set to true
    XmlTransformation: ${{parameters.xmlTransformation}} # Set to false
    XmlVariableSubstitution: ${{parameters.xmlVariableSubstitution}} # Set to true
    TakeAppOfflineFlag: true

我也尝试设置XmlVariableSubstitution为 false 并使用FileTransform@2上面提到的:

- task: FileTransform@2
  inputs:
    folderPath: '${{parameters.webSitePhysicalPathRoot}}'
    xmlTargetFiles: 'web.config'
4

0 回答 0