sppc42
sppc42

Reputation: 3302

Azure Devops: Run a task based on input json config condition

I have an Azure DevOps release pipeline which reads an input JSON in Powershell having the format

{
  "activities": [
     "activityA",
     "activityB",
     "activityC"
  ]
}

Now, depending on which activity has been specified, I need to run that corresponding task in the release pipeline and not all the activities. So, in the above, if activityB is not specified, then the task to install it should not run.

I know we can use custom conditional expressions to run a task, but not sure how to read a JSON array in PowerShell and use that in the custom conditional expression.

Any help would be appreciated.

Upvotes: 0

Views: 257

Answers (1)

Shayki Abramczyk
Shayki Abramczyk

Reputation: 41765

You can read the JSON with ConvertFrom-Json method, then check what he contain and set variables for use in the conditional expression.

For example:

$json = '{
  "activities": [
     "activityA",
     "activityB",
     "activityC"
  ]
}' | ConvertFrom-Json

if($json.activities.Contains("activityA"))
{
    Write-Host "##vso[task.setvariable variable=activityA]true"

}
if($json.activities.Contains("activityB"))
{
    Write-Host "##vso[task.setvariable variable=activityB]true"
}
if($json.activities.Contains("activityC"))
{
    Write-Host "##vso[task.setvariable variable=activityC]true"
}

Now in the conditional expression use the variables activityA activityB activityC.

Upvotes: 1

Related Questions