Home > Net >  Is it possible to use condition for Azure Pipeline Service Containers?
Is it possible to use condition for Azure Pipeline Service Containers?

Time:12-24

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?

CodePudding user response:

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

CodePudding user response:

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)
  • Related