0

I'm working on a Xamarin.Forms app for both iOS and Android, in the shared project I have an appsettings.json file where I'm placing some base settings keys/values.

Those key's values I want to be replaced depending on config variables I have in the .YML file inside my build pipelines, so for instance the key { "url": "" } for Debug Pipeline will be { "url": "http://google.com" } and for Release Pipeline will be { "url": "http://amazon.com" }.

I've handled this scenario previously in .NET Core projects but I've no idea how to do it with Xamarin, I know there is a File Transform task in the Azure Pipeline pre defined tasks, but it requires a .zip package or folder path to find the .json and transform it, but for mobile apps I think there is no suck thing like a pre-build folder.

I request your assistance.

Anon Dev
  • 1,361
  • 3
  • 14
  • 29
  • Any update for this issue? Could you resolve this issue with REST API or the task Replace Tokens? If not, would you please let me know the latest information about this issue? – Leo Liu Apr 20 '20 at 08:24

1 Answers1

0

How to replace json config values before Xamarin iOS and Android build task in pipeline

If you have different pipelines for the different config variables. You could just use the task Replace Tokens to update the key's value of the url in the appsettings.json file directly.

You could check my previous thread for the details.

Besides, if you are using one pipeline for the different config variables, we need use the Logging Command and REST API (Definitions - Update) to update the value of the build definition variable from a build task.

  • Add a Inline powershell task with following Logging Command to set the variable URL:

    $config= $Env:configuration
    Write-Host "Current config is $config"
    if ($config -eq "Debug")
    {
      Write-Host ("##vso[task.setvariable variable=URL]http://google.com")
     }
    
    elseif ($config -eq "release")
    {
      Write-Host ("##vso[task.setvariable variable=URL]http://amazon.com")
    }  
    
  • Use REST API (Definitions - Update) to update the value of the build definition variable with the value URL.

  • Use the task Replace Tokens to update the key's value of the url in the appsettings.json file

Hope this helps.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135