skip to Main Content

for example, I have something like this:

- job: Build
  dependsOn: CheckTest
  pool: ${{ parameters.setPool }}
  services:
    redis: redis
    rabbitmq: rabbitmq
  steps:
    - checkout: self

My question is – is it possible to manage services list? For example, for some cases I need only redis container, for another only rabbitmq and sometimes I don’t need any containers at all. Is it possible to implement dynamic services list?

2

Answers


  1. I would consider choosing with a conditional approach. For example

    variables:
      ${{ if eq(parameters.Environment, 'Production') }}: 
        serviceToDeploy: redis
      ${{ else }}:
        serviceToDeploy: rabbitmq
    

    And then you could use

     services:
        service1: $(serviceToDeploy)
    
    Login or Signup to reply.
  2. This can be done using object parameters.

    Pipeline:

    parameters:
    - name: myObject
      type: object
      default:
        serviceToDeploy:
        - redis
        - rabbitmq
    
    steps:
      - script: echo "Test step before parameter validation"
      - ${{ if ne(length(parameters.myObject.serviceToDeploy), 0) }}:
        - ${{ each service in parameters.myObject.serviceToDeploy }}:
          - script: echo ${{ service }}
            displayName: "Task for installing ${{ service }}"
    

    Run pipeline with 4 services:

    enter image description here

    Result:

    enter image description here


    Run pipeline with "No" services

    enter image description here

    Result:

    enter image description here

    PS: I do not have any container pools to test, but the conditions and loop should work exactly the same

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search