Making one task to run on particular container without effecting the sequence of order of preivous task - container agent

Shyam Kumar 846 Reputation points
2025-05-30T06:21:40.5566667+00:00

we are using agent which is container to run our pipeline.

We have main template and there is stage template and inside stage template there is a task template where in side this task template last one is vstest task template. of course it happens which is last task

this particular task (vstask) needs to run on different agent/container, is that possible, with out disturbing the order of all previous tasks.

If yes how?

Azure DevOps
0 comments No comments
{count} votes

Accepted answer
  1. Durga Reshma Malthi 4,530 Reputation points Microsoft External Staff Moderator
    2025-05-30T11:22:42.89+00:00

    Hi Diptesh Kumar

    Yes, you can run the VSTest task on a different agent/container without disturbing the order of previous tasks by using container jobs in Azure DevOps.

    Use the container property in YAML to specify a different container for the VSTest task.

    trigger:
      - master
    pool:
      vmImage: 'ubuntu-latest'
    jobs:
      - job: BuildAndDeploy
        container: my-default-container
        steps:
          - script: echo "Running previous tasks in default container"
          - task: AzurePowerShell@5
            inputs:
              azureSubscription: 'xxxx'
              ScriptType: 'FilePath'
              ScriptPath: 'test.ps1'
              pwsh: true
      - job: RunVSTest
        dependsOn: BuildAndDeploy  # Ensures previous tasks complete first
        container: my-vstest-container
        steps:
          - script: echo "Running VSTest in a different container"
          - task: VSTest@2
            inputs:
              testSelector: 'testAssemblies'
              testAssemblyVer2: '**\*test.dll'
    

    Hope this helps!

    Please Let me know if you have any queries.

    If you found the information helpful, please click "Upvote" on the post to let us know and consider accepting the answer as the token of appreciation. Thank You.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Deepanshu katara 16,565 Reputation points MVP Moderator
    2025-05-30T06:56:18.5566667+00:00

    Hello Diptesh , Welcome to MS Q&A

    In Azure DevOps, you cannot change the agent or container for a single task within a job—the agent/container is defined at the job level, and all tasks in that job run on the same agent/container.

    However, you can achieve your goal by splitting your tasks into separate jobs within the same stage or across stages. Each job can specify its own agent pool or container. You can use dependsOn to ensure the order is preserved.

    Example Solution

    Suppose you have a sequence of tasks, and only the last one (VSTest) needs a different container:

    stages:
    - stage: BuildAndTest
      jobs:
        - job: BuildAndOtherTasks
          container: my-default-container
          steps:
            - script: echo "Task 1"
            - script: echo "Task 2"
            # ... other tasks
    
        - job: VSTestJob
          dependsOn: BuildAndOtherTasks
          container: my-vstest-container
          steps:
            - task: VSTest@2
              inputs:
                # vstest inputs here
    
    
    

    Key Points:

    • Each job can have its own container or pool.
    • Use dependsOn to ensure VSTestJob runs after BuildAndOtherTasks.
    • This preserves the order and allows the last task to run in a different environment.

    Summary: You must split the tasks into separate jobs to use a different agent/container for the last task. This is the only supported way in Azure DevOps YAML pipelines.

    Please check and let us know if any further ques

    Kindly accept answer if it helps

    Thanks
    Deepanshu

    0 comments No comments

  2. Shyam Kumar 846 Reputation points
    2025-05-30T11:31:53.3266667+00:00

    Hi Durga,

    is it possible without using job?

    because task we are planning to use container image separately is in the task template which is in stage template.

    0 comments No comments

  3. Durga Reshma Malthi 4,530 Reputation points Microsoft External Staff Moderator
    2025-06-02T03:21:53.8633333+00:00

    Hi Diptesh Kumar

    Yes, it is possible to run the VSTest task on a different container without using a separate job by specifying the container image directly within the task template inside the stage template.

    steps:
      - task: VSTest@2
        displayName: "Run Tests in Separate Container"
        container: my-vstest-container
        inputs:
          testSelector: 'testAssemblies'
          testAssemblyVer2: '**\*test.dll'
    

    This isolates the VsTest task to run in a completely different container without disturbing the build tasks. Hope this helps!

    Please Let me know if you have any queries.

    If you found the information helpful, please click "Upvote" on the post to let us know and consider accepting the answer as the token of appreciation. Thank You.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.