0

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="reports@companyxyz.com" 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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Cataster
  • 3,081
  • 5
  • 32
  • 79

1 Answers1

2

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

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • Interesting, thanks Kevin. Wouldn't the variable value have to be ONLY `` since the token prefix and suffix are ` and ` respectively? – Cataster May 31 '21 at 03:55
  • 1
    According to my test, if and are not added to the variable value, the prefix and suffix will be lost. It will lead to the web. config cannot maintain the original format. So this is required. If not add, the format will change from ` AA ` to `AA` – Kevin Lu-MSFT May 31 '21 at 04:09
  • 1
    It worked! Thank you very much! One thing I had to make sure of is that my Azure App Deploy Task `packageForLinux` attribute had to be set to `'$(System.DefaultWorkingDirectory)/Build Artifact Name/ArtifactName'` otherwise the change wasnt happening. Also I noticed if i disabled 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: an ugly long XML element. but if i enable it, it will format/transfom it to neat XML :) – Cataster May 31 '21 at 05:04
  • 1
    Great! Glad to know that it could work now. If the answer could give you some help, you may consider accepting it. thanks . – Kevin Lu-MSFT May 31 '21 at 05:09
  • 1
    yes of course :) do you wanna add my comments as well for future readers? it may help if someone follows the steps but has the wrong package set in the Azure App Deploy – Cataster May 31 '21 at 05:11
  • Thank you for your reminder, your comments are valuable. I have updated the answer. Have a nice day. – Kevin Lu-MSFT May 31 '21 at 05:21