David Shochet
David Shochet

Reputation: 5385

Azure DevOps: how to set a variable to a value based on the stage

In my Xamarin.Forms application (that supports iOS, Android, UWP), I have a config.json file that defines some variables, e.g.

{ "HideExperimentalFeature": false }

Is it possible in the AzureDevops pipeline to inject a value for a variable based on stage, e.g. QA or Production? E.g. for QA it can be set to false, and for Production true. Is it possible to do it using user interface?

Upvotes: 0

Views: 580

Answers (2)

J.Wincewicz
J.Wincewicz

Reputation: 962

Yes, you can use Replace Tokens extension.

in config.json file just add key markers, and tell the extension which files should be scanned for replacement.

{
  "ConnectionStrings": {
    "BloggingDatabase": "#{connection_string}#"
  },
  { "HideExperimentalFeature": #{HideExperimentalFeature}# }
}

connection_string will be replaced with proper value based on stage. Also, you can use Release stage which means "use it everywhere". What is more, you can you Release + stage_x, and then Release will be used as default, and stage_x will override it on its stage.

Consider Missing variable behavior set as Fail pipeline due to the fact, Log Warn is usually not noticed in the woods of logs.

Also, Azure DevOps variables nature allows you using Settable as release time option next to the variable definition. Then, you can pass the value

enter image description here

Upvotes: 1

gszu
gszu

Reputation: 46

You can replace tokens in files:

  1. swap it in your json file to a token, ie. { "HideExperimentalFeature": #{HideExperimentalFeature}# }

  2. In your release pipeline you set these variables:

    • HideExperimentalFeature: true for Prod stage
    • HideExperimentalFeature: false for QA stage
  3. Add a Replace Tokens task to your pipeline:
    • Target files: path/to/config.json
    • Missing variables / action: fail

Upvotes: 3

Related Questions