您可以添加一个附加阶段来根据触发时间设置代理变量。并在此阶段添加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 将被您在请求正文中定义的代理覆盖。