Azure Pipelines (yaml) using conditional to set a new variable

Viewed 63

I'm using Azure Devops, yaml pipelines, for deploying several microservices parallel. Now the idea is to have an option to manually change the set of microservices to deploy through setting a variable using the UI (see image below) in ADO.

Setting of the variable using the UI in ADO

1

In my deploy template where the variable will be used, there is also a parameter called "microservices", which either has a number of microservices given by the template referencing this template, or otherwise takes the default as seen below.

I try to set a variable under the variables section, called "microservicesSet", by using a conditional, to set it to either the $(MICROSERVICES) variable from the ui, or to the params.microservices value in case $(MICROSERVICES) from the UI is empty/not set. As variables are always a string, I try to make the paramter a string (using join()) from the parameter.microservices.

I then added three scripts to check the values of the three players:

  • parameters.microservices
  • $(microservices)
  • variables.microservicesSet

Unfortunately the "microservicesSet" always stays empty (I have tried many different notations), I feel something inside the conditional setting the variable is already going wrong.

Now where do I need the "microservicesSet" variable string for? I use it in the powershell script at the bottom, where it is passed to as an argument. In the script I'm setting a $microservicesArray variable, which should hold the, to array converted, string holding a certain set of microservices names, separating by a comma. The array is used in the loop below.

I hope someone can help. Thank you.

    parameters:
  - name: microservices
    type: object
    default: ms-1, ms-2
  - name: environment
    type: string
  - name: azureEnvironment
    type: string
  - name: devopsEnvironment
    type: string
  - name: stageName
    type: string

stages:
  - stage: ${{ parameters.stageName }}
    pool:
      name: 'somePool'
      demands: azureps
    jobs:
      - deployment: Deploy
        environment: ${{ parameters.devopsEnvironment }}
        variables:
          azureSubscription: 'some-${{ parameters.azureEnvironment }}-sub'
          resourceGroupName: 'some-${{ parameters.azureEnvironment }}'
          -name: microservicesSet
          ${{ if eq( '', variables['MICROSERVICES'] ) }}:
            value: ${{ join(',', parameters.microservices) }}
          ${{ if ne( '', variables['MICROSERVICES'] ) }}:
            value: $(MICROSERVICES)
        strategy:
          runOnce:
            deploy:
              steps:
                - script: echo "parameter microservices = ${{ parameters.microservices }}"
                - script: echo "UI variable = $(MICROSERVICES)"
                - script: echo "variable microservicesSet = $(microservicesSet)"
                - task: AzureCLI@2
                  displayName: Deploy microservices
                  timeoutInMinutes: 20
                  inputs:
                    azureSubscription: ${{ variables.azureSubscription }}
                    scriptType: 'ps'
                    scriptLocation: inlineScript
                    arguments: >
                      -microservices ('${{ variables.microservicesSet }}')
                      -environment ${{ parameters.environment }}
                      -artifactPath ('$(Pipeline.Workspace)/Azure Deployable zip files')
                      -resourceGroup ('${{variables.resourceGroupName}}')
                    inlineScript: |
                      param (
                          [Parameter(Mandatory = $true)][String]$microservices,
                          [Parameter(Mandatory = $true)][String]$environment,
                          [Parameter(Mandatory = $true)][String]$artifactPath,
                          [Parameter(Mandatory = $true)][String]$resourceGroup
                      )
                      
                      $microservicesArray = $microservices.Split(",")

                      $jobs = @()
                      ForEach ($service in $microservicesArray) {
                          Write-Host "Start deploying microservice: " $service -ForegroundColor Green
                          etc.........
1 Answers

When defining variables, under the same 'variables' key, you should use the consistent syntax to defined the variables. Do not use the inconsistent syntax.

variables:
- name: azureSubscription
  value: 'some-${{ parameters.azureEnvironment }}-sub'
- name: resourceGroupName
  value: 'some-${{ parameters.azureEnvironment }}'
- name: microservicesSet
  ${{ if eq(variables['MICROSERVICES'], '') }}:
    value: ${{ join(',', parameters.microservices) }}
  ${{ else }}:
    value: ${{ variables['MICROSERVICES'] }}

In addition, if the default value (ms-1,ms-2) of the parameter "microservices" would not be used by other stages in the same pipeline, I think the variable "MICROSERVICES" is not required. You can directly use the value of "microservices", since you also can provide a new value to override the default value every time when you start a new run.

enter image description here

Related