gityamlazure-pipelinesazure-pipelines-yaml

Yaml Pipeline: Add condition for each environment to use a specific agent from the agent pool


I need to add the condition for each environment to use specific Agent.Name. For dev and sandbox environment it should be using the same Agent.Name "agent-dev"

Environment Agent.Name
dev and sandbox agent-dev
int agent-int
preprod agent-preprod
prod agent-prod

Yaml code is shown below. How can I write the condition for dev and sandbox environment to use the same Agent.Name "agent-dev"?

pool:
  vmImage: 'linux'

trigger: none

parameters:
  - name: env
    values:
      - dev
      - sandbox
      - int
      - preprod
      - prod
    type: string
    displayName: "Environment"

stages:
  - stage: Configure${{ upper(parameters.env) }}
    displayName: "Configure Resources in ${{parameters.env}} 

    jobs:
      - job: Configure_${{ upper(parameters.env) }}
        pool:
          name: Project-Linuxpool
          demands:
            - Agent.Name -equals agent-$(environment)

Solution

  • How can I write the condition for dev and sandbox environment to use the same Agent.Name "agent-dev"?

    You can use the if condition and the Or expression to let the dev and sandbox env to use the same agent.

    Here is an example:

    trigger: none
    
    parameters:
      - name: env
        values:
          - dev
          - sandbox
          - int
          - preprod
          - prod
        type: string
        displayName: "Environment"
    
    stages:
      - stage: Configure${{ upper(parameters.env) }}
        displayName: "Configure Resources in ${{parameters.env}}"
    
        jobs:
          - job: "Configure_${{ upper(parameters.env) }}"
            pool:
              name: Project-Linuxpool
              ${{if or(eq( parameters.env, 'dev'), eq( parameters.env,'sandbox') )}}:
               demands:
                 - Agent.Name -equals agent-dev
              ${{ else }}:
               demands:
                 - Agent.Name -equals agent-${{parameters.env}}      
    

    When you select dev or sandbox in env parameters, it will use the dev agent. Or it will select the agent name based on the env parameters value.