【问题标题】:Can't conditionally add script to Azure Devops Yaml pipeline无法有条件地将脚本添加到 Azure Devops Yaml 管道
【发布时间】:2020-11-13 12:16:41
【问题描述】:

我正在尝试在 Microsoft 托管和自托管(容器化)构建代理上运行的管道中使用容器作业模板。

ContainerJob 模板在 Microsoft Hosted 中运行时运行良好,但在自托管代理中显示 cannot run container inside a containerized build agent 时失败。错误是有道理的。

我认为如果可以有条件地添加/删除以下部分,那么相同的容器作业模板适用于两个代理。

${{ if not(parameters.isSelfHosted) }}:
  container:
    image: ${{ parameters.generalImage}}
    endpoint: ${{ parameters.endpoint}} 
  environment: ${{ parameters.environment }}

但条件始终为真,并且始终添加 container 部分,并且始终在自托管代理中失败。我认为模板中不允许随意使用表达式。

我可以将此模板分成 2 个模板,然后将它们加载到各自的构建代理中,但这是最后的手段。非常感谢您在动态创建/更改模板方面的任何帮助。

【问题讨论】:

    标签: azure-devops yaml azure-devops-hosted-agent azure-devops-pipelines


    【解决方案1】:

    您在寻找条件容器工作吗?查看我的脚本:

    parameters:
    - name: isSelfHosted
      displayName: isSelfHosted
      type: boolean
      default: true
      values:
      - false
      - true
    
    stages:
    - stage: Build
      displayName: Build stage
    
      jobs:
      - job: Build
        displayName: Build
        ${{ if not(parameters.isSelfHosted) }}:
          container: mcr.microsoft.com/dotnet/core/sdk:2.2
          steps:
            - task: CmdLine@2
              inputs:
                script: |
                    echo 2.2 SDK
    
        ${{ if eq(parameters.isSelfHosted, 'true') }}:
          container: mcr.microsoft.com/dotnet/core/sdk:2.1
          steps:
            - task: CmdLine@2
              inputs:
                script: |
                    echo 2.1 SDK
    

    我可以通过isSelfHosted 参数的值来选择哪个容器(.net core 2.1 或 .net core 2.2)。

    它使用Runtime parameters,所以我可以在运行管道时管理条件(选中或取消选中该框):

    更新:

    Job, stage, and step templates with parameters

    将以下内容移动到模板文件中:

    stages:
    - stage: Build
      displayName: Build stage
    
      jobs:
      - job: Build
        displayName: Build
        ${{ if not(parameters.isSelfHosted) }}:
          container: mcr.microsoft.com/dotnet/core/sdk:2.2
          steps:
            - task: CmdLine@2
              inputs:
                script: |
                    echo 2.2 SDK
    
        ${{ if eq(parameters.isSelfHosted, 'true') }}:
          container: mcr.microsoft.com/dotnet/core/sdk:2.1
          steps:
            - task: CmdLine@2
              inputs:
                script: |
                    echo 2.1 SDK
    

    那么主azure-pipelines.yml应该是

    parameters:
    - name: isSelfHosted
      displayName: isSelfHosted
      type: boolean
      default: true
      values:
      - false
      - true
    
    stages:
    - template: templates/xxx.yml  # Template reference
      parameters:
        isSelfHosted: xxx (Value of isSelfHosted parameter)
    

    【讨论】:

    • 谢谢。但我试图在template 中使用它。所以我会将参数传递给模板并将条件放入模板中。
    • 谢谢。但问题是在xxx.yml 模板中访问它并根据该条件添加container 脚本。我在我的问题中提供的 sn-p 应该在 xxx.yml 中,并且应该仅在 isSelfHosted 为 false 时添加。
    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 2022-09-23
    • 2019-04-06
    • 2020-09-15
    • 2022-11-14
    • 2020-05-23
    • 2022-11-05
    • 1970-01-01
    相关资源
    最近更新 更多