确保相关作业在同一个自托管代理中运行

问题描述 投票:0回答:1

我有以下场景,使用 YAML 管道:

  • 多个自托管代理
  • 具有多个作业的管道中的一个阶段。这些工作不能合并为一个工作
  • 这些工作相互依存
  • 不同的作业必须在同一个代理中运行

我尝试了多种方法,但似乎没有任何效果。

是否有推荐的方法来强制依赖作业在同一个代理中运行?

注意:我不需要将它们绑定到特定代理,而是绑定到同一个代理。我实际上希望使用所有代理,但阶段中的作业必须在单个代理中运行。其他阶段或其他管道运行可以在其他代理中运行。

编辑

提供更多关于为什么我需要使用代理池中的单个代理的背景信息:

  • 其中一个作业需要在自我管理的 docker 容器中运行,因为它需要在主机网络模式下运行,并且与 Azure 使用用户网络相冲突
  • 其他作业正在实现不同的功能,使用azure托管容器中的现成任务(我的容器没有运行这些任务的功能,因此我需要其他作业)
  • 在自管理容器中运行的作业中生成的资产在其他作业中是需要的,并且这些资产不会共享到 Azure Artifacts,从而防止在其他代理中运行的其他作业使用。我不知道如何将生成的工件共享到 Azure Artifacts 存储库,而且我什至不确定用于获取这些工件的自我管理容器是否能够上传到 Azure Artifacts
  • 如果我保证使用同一个代理,则具有多个作业的阶段当前正在工作。目前,我可以通过在池中放置一个自托管代理来保证这一点,但是一旦我在池中放入更多代理,保证会中断,作业就会失败。
  • 如果我可以为阶段中的所有作业强制使用代理,那么当我转向使用池中的多个代理时,保持当前架构的工作将非常容易
azure-devops azure-pipelines
1个回答
0
投票

您可以使用需求功能来确保同一代理运行特定阶段的所有作业。

使用每个构建代理独有的功能非常重要,例如

Agent.Name

Build agent - system capabilities

第一个作业将设置一个具有该值的输出变量:

echo "##vso[task.setvariable variable=CurrentAgent;isOutput=true]$(Agent.Name)"

剩余的工作将使用基于该变量的需求:

pool:
  name: MyAgentPool
  demands: Agent.Name -equals $(CurrentAgent)

工作示例

trigger: none

stages:
  - stage: A
    jobs:
      - job: jobA0
        displayName: A0
        pool:
          name: Default
        steps:
          - checkout: none
          - script: |
              echo "Running job using agent $(Agent.Name)"
              echo "##vso[task.setvariable variable=CurrentAgent;isOutput=true]$(Agent.Name)"
            name: setAgent
            displayName: 'Set current agent'

      - job: jobA1
        displayName: A1
        dependsOn: jobA0
        variables:
          CurrentAgent: $[ dependencies.jobA0.outputs['setAgent.CurrentAgent'] ]
        pool:
          name: Default
          demands: Agent.Name -equals $(CurrentAgent)
        steps:
          - checkout: none
          - script: |
              echo "Running job using agent $(Agent.Name)"
            displayName: 'Display current agent'

      - job: jobA2
        displayName: A2
        dependsOn:
          - jobA0 # add the dependency on the job that sets the variable
          - jobA1
        variables:
          CurrentAgent: $[ dependencies.jobA0.outputs['setAgent.CurrentAgent'] ]
        pool:
          name: Default
          demands: Agent.Name -equals $(CurrentAgent)
        steps:
          - checkout: none
          - script: |
              echo "Running job using agent $(Agent.Name)"
            displayName: 'Display current agent'

      - job: jobA3
        displayName: A3
        dependsOn:
          - jobA0 # add the dependency on the job that sets the variable
          - jobA2
        variables:
          CurrentAgent: $[ dependencies.jobA0.outputs['setAgent.CurrentAgent'] ]
        pool:
          name: Default
          demands: Agent.Name -equals $(CurrentAgent)
        steps:
          - checkout: none
          - script: |
              echo "Running job using agent $(Agent.Name)"
            displayName: 'Display current agent'
© www.soinside.com 2019 - 2024. All rights reserved.