【问题标题】:How to configure schedules to run pipelines on two different agents on Azure?如何配置计划以在 Azure 上的两个不同代理上运行管道?
【发布时间】:2020-11-02 16:13:07
【问题描述】:

我在 Azure 上创建了“Test dev”管道,并且配置了两个代理“XC1”和“XC2”。

我希望 XC1 和 XC2 代理应在不同时间(下午 4 点和下午 6 点)在“测试开发”管道上运行

这里是 yaml 代码

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

目前计划在“XC1”代理上正常工作,但如何为“XC2”代理配置。

请帮帮我。

【问题讨论】:

  • 嗨,您有时间查看以下步骤和脚本吗?对你有用吗?
  • @LeviLu-MSFT 它正在工作,但我需要按照问题中提到的那样配置 cron。我想在“测试开发”管道上运行 XC2 代理。默认情况下,它在“XC1”代理上运行,应该是这样,但现在我想将 XC2 代理作为值传递,并在特定时间执行该代理,就像我提到的 cron 计划一样。
  • @LeviLu-MSFT 如果您对此有任何想法,请提供帮助。
  • 您可以尝试将带有预定触发器的AgentStage 阶段移动到单独的管道。并添加一个powershell任务通过rest api触发主管道。然后,您可以在请求正文中传递 XC2 代理。有关更多信息,请参阅this thread。通过这种方式,您可以在不使用依赖项和输出变量的情况下保留原来的主 yaml 管道。
  • 你能帮我修改一下吗?我是新手,所以在这里得到了一些帮助。谢谢

标签: azure-devops yaml


【解决方案1】:

您可以添加一个附加阶段来根据触发时间设置代理变量。并在此阶段添加dependency,用于以下阶段:详细步骤和yaml示例如下:

1,添加一个额外的阶段(即AgentStage)作为顶层阶段。

2,在AgentStage 阶段添加一个作业(即AgentJob)并运行下面的脚本来设置代理变量。有关设置多作业输出变量的更多信息,请参阅here

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、为后续阶段添加依赖。并使用以下格式引用输出变量。 (注意:不需要在参数部分定义agents。)

 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 }}

请参阅下面的完整 yaml 示例:

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

模板core.yml中的stage也要加上dependsOnAgentStagestage

# core.yml

parameters:
  agents: ""
  tests: ""

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

更新:

您可以通过rest api创建一个单独的管道并根据预定时间触发主管道。见下例:

1、触发管道:

  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"
    
        

所以当这个触发管道在预定的时间被触发时。它通过 rest api 触发您的主管道。并且邮件管道中的参数agents 将被您在请求正文中定义的代理覆盖。

【讨论】:

猜你喜欢
  • 2022-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 2022-01-11
  • 2022-11-02
  • 2020-09-30
相关资源
最近更新 更多