Cataster
Cataster

Reputation: 3541

How to perform XML Element substitution in web.config using Replace Tokens?

We have a Web.config file with the following system.net mailSettings attribute:

  <system.net>
    <mailSettings>
      <smtp configSource="mailSettings.config" />
    </mailSettings>
  </system.net>

We want to replace this line in the web config: <smtp configSource="mailSettings.config" /> with a pipeline variable whose value will be <smtp from="[email protected]" deliveryMethod="Network"> <network enableSsl="true" host="smtp.sendgrid.net" port="587" userName="apiKey" password="12346576fgb" /> </smtp>.

Also because the developer doesn't want to change the web config file to accommodate prefixes and suffices required by the Replace Tokens task, we want to use custom Token Prefix, as such: <mailSettings> and Suffix: </mailSettings>

steps:
- task: qetza.replacetokens.replacetokens-task.replacetokens@4
  displayName: 'Replace tokens in **/*.config'
  inputs:
    tokenPattern: custom
    tokenPrefix: '<mailSettings>'
    tokenSuffix: '</mailSettings>'

Can the above be accomplished? Also do I place this Replace Tokens task before Azure App Deploy task? or after?

Upvotes: 0

Views: 1688

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35514

Can the above be accomplished?

Yes. This can be accomplished.

The replace token task uses the Pipeline varible to set the value in Web.config file.

In your case, the varaible name is <smtp configSource="mailSettings.config" />.

And in the replacement process, the prefix and suffix will be replaced at the same time.

So you could try to set the variable:

<smtp configSource="mailSettings.config" /> : <mailSettings> <smtp configSource="AA.config" /></mailSettings>

Here is an example:

enter image description here

Definition:

- task: qetza.replacetokens.replacetokens-task.replacetokens@3
  displayName: 'Replace tokens in web.config'
  inputs:
    rootDirectory: '$(build.sourcesdirectory)'
    targetFiles: web.config
    tokenPrefix: '<mailSettings>'
    tokenSuffix: '</mailSettings>'

Result:

enter image description here

Also do i place this Replace Tokens task before Azure App Deploy task? or after?

You need to add this task before the Azure App Deploy task.

Then the changes will apply to the Azure App Deploy task.

Update:

As Cataster said in the comments:

Here are two key points in Azure App Service Task:

  1. Make sure of is that my Azure App Deploy Task packageForLinux attribute had to be set to $(System.DefaultWorkingDirectory)/Build Artifact Name/ArtifactName

  2. Disable XML transformation option in the Azure App Deploy task the format of the token replaced will be the same as that in the pipeline variable. but if we enable this option, it will format/transfom it to neat XML

Upvotes: 2

Related Questions