Search code examples
azure-devopstfsyamlazure-pipelinesazure-pipelines-yaml

How to run deployment job using only agent pool agents instead of environment agents


- deployment: Job_Start_Installation
    displayName: Start Installation
    cancelTimeoutInMinutes: 1
    environment:
      name: MyEnvironment
      resourceType: VirtualMachine
      tags: Active
    strategy:
      runOnce:
        deploy:
          steps:          
          - template: '../Steps/StartInstallation.yml'

This is my YAML code for one of my deployment jobs. I am currently using Environments to execute the steps on multiple machines within the environment. This works very well.

Now, I want to achieve the same result using regular Agent pool agents, without relying on Environments or deployment jobs anymore. What is the best way for doing this. I found that there something exists like each loops, strategy with parallel properties, etc... Need to know what is the recommended and best way for doing this.


Solution

  • I'm afraid there's no way the agent job can do exactly the same thing as the deployment job. But you can use job.strategy and pool.demands as a workaround.

    When using self-hosted agents, you can add new user-defined capabilities and use pool.demands to filter the agents with the target capabilities. For your case, you can add Build.BuildNumber as user-defined capability. For the sake of illustration, I have added a capability named "tag" with the value "test" to my self-hosted agent. enter image description here

    1. Using strategy: parallel

    jobs:
    - job: Test
      strategy:
        parallel: 4
      pool: 
        name: Default
        demands: tag -equals "test"
      steps:
    

    Using this strategy, the same job will be executed multiple times, depending on the value of parallel. But you need to make sure you have purchased enough parallel jobs, otherwise the same job may be run multiple times on the same agent.

    2. Using strategy: matrix, maxParallel

    jobs:
    - job: Test
      strategy:
        matrix:
          VM1:
            agentName: VZIYAN620VM
          VM2:
            agentName: VTIANM336VM
    
      pool: 
        name: Default
        demands: 
        - Agent.Name -equals $(agentName)
        - tag -equals test
    

    Using this strategy, the job will try to run on each agent. It will run successfully on agents that meet the demand and fail on others. But you need to define all your agents' name in the matrix. enter image description here