blogs4t
blogs4t

Reputation: 2737

how to add arrays as azure devops variable groups variable value?

I have used azure devops variable group variables to define my appsettings properties. However, I could not use an array as a value for one of my appsettings properties.

Here is a screenshot of what i am trying to do:

enter image description here

But it appears on the appsettings.json as follows which results in a 500 internal server error Value cannot be null. Parameter name: collection

"AzureAd": {
    "Instance": "https://login.microsoftonline.com",
    "ClientId": "",
    "TenantId": "",
    "Audience": "[ https://api.doesnotexist.com ]"
}

The code in Startup.cs that is failing is as follows:

services.AddAuthentication(o =>
{
    o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(o =>
{
    o.Authority = $"{azureADSettings.Instance}/{azureADSettings.TenantId}/";
    o.TokenValidationParameters = new TokenValidationParameters
    {
        ValidateAudience = true,
        ValidAudiences = new List<string>(azureADSettings.Audience),
    };
});

Upvotes: 2

Views: 10293

Answers (2)

Terence Usai
Terence Usai

Reputation: 39

You can also set collection using the variables as explained in the microsoft documentation

By adding the index of the element in the variable name example

Upvotes: 4

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35119

Please Refer to this doc:

All variables are stored as strings and are mutable. The value of a variable can change from run to run or job to job of your pipeline.

In Azure Devops, Variables are String type by default. And we couldn'change it.

So when you define an array in variable, it will pass the array as a string to your appsettings.json file.

To solve this issue, you need to change to use Parameters. The parameter supports the value of the input array type.

Here is an example:

parameters:
- name: 'param'
  type: object
  default: [123,234,456]


steps:
- ${{ each p in parameters.param }}:
  - script: echo ${{ p }}

Note: Parameters can only be used in YAML pipeline.

Upvotes: 1

Related Questions