我已经在Azure上创建了“测试开发”管道,并且配置了两个代理“ 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”代理。
请帮助我。
答案 0 :(得分:0)
您可以添加一个附加阶段来根据其触发时间设置代理变量。并在此阶段的以下阶段添加dependency:请参见下面的详细步骤和yaml示例:
1,添加一个额外的阶段(即AgentStage
)作为顶层。
2,在AgentJob
阶段添加一个作业(即AgentStage
),并运行以下脚本来设置代理变量。有关设置多作业输出变量的更多信息,请参见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中的阶段也应添加取决于AgentStage
阶段
# 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
将被您在请求正文中定义的代理覆盖。