3
votes

We looking to create a pipeline to update our multi-tenant azure environment. We need to perform some actions during the update per tenant. To accomplish this, we would like to create a job per tenant, so we can process tenants in parallel. To accomplish this, I want to use a runtime parameter to pass the tenants to update to my pipeline as follows:

parameters:
- name: tenants
  type: object

the value of the tenants parameter might look like something like this:

- Name: "customer1"
  Someotherproperty: "some value"
- Name: "customer2"
  Someotherproperty: "some other value"

to generate the jobs, we do something like this:

stages:
- stage:
  jobs:
  - job: Update_Tenant
    strategy:
      matrix:
        ${{ each tenant in parameters.Tenants }}:
          ${{ tenant.tenantName }}:
            name: ${{ tenant.tenantName }}
            someproperty: ${{ tenant.otherProperty }}
      maxParallel: 2
    steps:
      - checkout: none
      - script: echo $(name).$(someproperty) 

Now what we need, is some way to fill this tenants parameter. Now I tried a few solutions:

  • Ideally I would like to put a build stage before the Update_Tenants stage to call a REST api to get the tenants, and expand the tenants parameter when the Update_Tenants stage starts, but this is not supported AFAIK, since parameter expansion is done when the pipeline starts.

  • A less ideal but still workable option would have been to create a variable group yaml file containing the tenants, and include this variable group in my pipeline, and use the ${{ variables.Tenants }} syntax to reference them. However, for some reason, variables can only be strings.

The only solution I can currently think of, is to create a pipeline that calls a REST api to get the tenants to update, and then uses the azure devops api to queue the actual update process with the correct parameter value. But this feels like a bit of a clunky workaround to accomplish this.

Now my question is, are there any (better?) alternatives to accomplish what I want to do?

2

2 Answers

0
votes

To accomplish this, we would like to create a job per tenant, so we can process tenants in parallel.

Apart from rolling deployment strategy, you can also check Strategies and Matrix.

You can try something like this unless you have to use Runtime parameters:

jobs:
- job: Update
  strategy:
    matrix:
      tenant1:
        Someotherproperty1: '1.1'
        Someotherproperty2: '1.2'
      tenant2:
        Someotherproperty1: '2.1'
        Someotherproperty2: '2.2'
      tenant3:
        Someotherproperty1: '3.1'
        Someotherproperty2: '3.2'
    maxParallel: 3
  steps:
      - checkout: none
      - script: echo $(Someotherproperty1).$(Someotherproperty2) 
        displayName: 'Echo something'
0
votes

Maybe this can help. I was able to use external source (.txt file) to fill array variable in azure pipelines.

Working example

          # Create a variable
          - bash: |
              arrVar=()
              for images in `cat my_images.txt`;do
                arrVar+=$images
                arrVar+=","
              done;
              echo "##vso[task.setvariable variable=list_images]$arrVar"

          # Use the variable
          # "$(list_images)" is replaced by the contents of the `list_images` variable by Azure Pipelines
          # before handing the body of the script to the shell.
          - bash: |
              echo my pipeline variable is $(list_images)

Sources (there is also example for matrix)

Other sources