nrs
nrs

Reputation: 945

How to configure schedules to run pipelines on two different agents on Azure?

I have created "Test dev" pipeline on Azure and there are two agents configured "XC1" and "XC2".

I want to XC1 and XC2 agents should run on "Test dev" pipeline at different timings(4 PM and 6 PM)

Here is the yaml code

initial.yml

trigger:
- none

# Setting Timer

schedules:
- cron: 16 * * 1-6
  displayName: Regression Run
  branches:
    include:
      - master
  always: true
  
 
 # Parameters for UI and default Parameters

parameters:
- name: agents
  displayName: Agent
  type: string
  default: TT1
  values:
  - TT1
  - TT2
- name: tests
  displayName: "Testing agents"
  type: string
  
  
stages:
- template: core.yml
  parameters:
    agents: ${{ parameters.agents }}
    tests: ${{ parameters.tests }}


core.yml

- stage: testagent
  displayName: Middaytest
  
  variables:
    - group: Credentials
    - name: 'AGENT'
      value: ${{ parameters.agents }}
    - name: 'TEST_SEQUENCE'
      value: ${{ parameters.tests }}
    

  jobs:
  - job: testagent
    displayName: Middaytest
    timeoutInMinutes: 400
    pool:

      name: nightlytest
      demands:
      - agent.name -equals $(AGENT)
    steps: #empty

Currently schedule is working properly on "XC1" agent but how to configure for "XC2" agent.

Please help me.

Upvotes: 1

Views: 2202

Answers (1)

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30373

You can add an additional stage to set the agent variable according to the time it gets triggered. And add dependency on this stage for the following stages: See below detailed steps and yaml example:

1, Add an additional stage(ie. AgentStage) as the top stage.

2, Add a job(ie. AgentJob) in the AgentStage stage and run below script to set the agent Variable. See here for more information about Set a multi-job output variable.

stages:
- stage: AgentStage
  jobs:
  - job: AgentJob
    pool: 
      vmImage: windows-latest
    steps:
    - powershell: |
        $Time = Get-Date
        $hour = $Time.ToUniversalTime().Hour #convert scheduled time to the UTC time.

        #noted the time here is UTC time, please change the time value accordingly
        if($hour -eq 4){
          echo "##vso[task.setvariable variable=Agent;isOutput=true]agent1"
        }
        if($hour -eq 6){
          echo "##vso[task.setvariable variable=Agent;isOutput=true]agent2"
        }
      name: AgentTask

3, Add dependency for the following stages. And refer the output Variable using below format. (Noted: No need to define the agents in the parameters section.)

 stageDependencies.stageName.jobName.outputs['stepName.variableName'].

- template: core.yml
  parameters:
    agents: $[stageDependencies.AgentStage.AgentJob.outputs['AgentTask.Agent']]
    tests: ${{ parameters.tests }}

 variables:
    - group: Credentials
    - name: 'AGENT'
      value: $[stageDependencies.AgentStage.AgentJob.outputs['AgentTask.Agent']]
    - name: 'TEST_SEQUENCE'
      value: ${{ parameters.tests }}

See below full yaml example:

parameters:
- name: tests
  displayName: "Testing agents"
  type: string

stages:
- stage: AgentStage
  jobs:
  - job: AgentJob
    pool: 
      vmImage: windows-latest
    steps:
    - powershell: |
        $Time = Get-Date
        $hour = $Time.ToUniversalTime().Hour #convert scheduled time to the UTC time.

        #noted the time here is UTC time, please change the time value accordingly 
        if($hour -eq 4){
          echo "##vso[task.setvariable variable=Agent;isOutput=true]agent1"
        }
        if($hour -eq 6){
          echo "##vso[task.setvariable variable=Agent;isOutput=true]agent2"
        }
      name: AgentTask
    

- template: core.yml
  parameters:
    agents: $[stageDependencies.AgentStage.AgentJob.outputs['AgentTask.Agent']]
    tests: ${{ parameters.tests }}


- stage: testagent
  
  # dependsOn AgentStage stage

  dependsOn: AgentStage
  displayName: Middaytest
  
  variables:
    - group: Credentials
    - name: 'AGENT'
      value: $[stageDependencies.AgentStage.AgentJob.outputs['AgentTask.Agent']]
    - name: 'TEST_SEQUENCE'
      value: ${{ parameters.tests }}
    

  jobs:
  - job: testagent
    displayName: Middaytest
    timeoutInMinutes: 400
    pool:

    name: nightlytest
    demands:
    - agent.name -equals $(AGENT)
    steps: #empty

The stage in template core.yml should be added dependsOn AgentStage stage too

# core.yml

parameters:
  agents: ""
  tests: ""

stages:
- stage:
  dependsOn: AgentStage
  variables: 
    agent: ${{parameters.agents}}

Update:

You can create a separate pipeline and trigger the main pipeline according to the scheduled time via rest api. See below example:

1, Triggerring pipeline:

  trigger:
- none

# Setting Timer

schedules:
- cron: 16 * * 1-6
  displayName: Regression Run
  branches:
    include:
      - master
  always: true

 pool: 
   vmImage: windows-latest
 steps:
 - powershell: |
         $Time = Get-Date
         $hour = $Time.ToUniversalTime().Hour #convert scheduled time to the UTC time.

         #noted the time here is UTC time, please change the time value accordingly 
         $body=""
    
         if($hour -eq 4){  
            
                $body= '{
                         "templateParameters":{
                             "agents": "XC1"
                           }
                        }'
          }


         if($hour -eq 6){
             $body= '{
                         "templateParameters":{
                             "agents": "XC2"
                           }
                        }'
            }
         $url = "https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1"
         Invoke-RestMethod -Uri $trurl -Headers @{authorization = "Bearer $(System.AccessToken)"} -Method post -Body $body -ContentType "application/json"
    
        

So when this triggering pipeline gets triggered on the scheduled timing. It triggers your main pipeline via rest api. And the parameters agents in the mail pipeline will be overrode by the agents you defined in the request body.

Upvotes: 1

Related Questions