我正在尝试在Microsoft托管和自托管(包含容器)构建代理中运行的管道中使用Container Job模板。
ContainerJob模板在Microsoft Hosted
中运行时效果很好,但在自托管代理中显示cannot run container inside a containerized build agent
时失败。错误是有道理的。
我认为,如果可以有条件地添加/删除以下部分,则在两个代理中都可以使用相同的Container Job模板。
${{ if not(parameters.isSelfHosted) }}:
container:
image: ${{ parameters.generalImage}}
endpoint: ${{ parameters.endpoint}}
environment: ${{ parameters.environment }}
但是条件始终为true,并且总是添加container
节,并且在自托管代理程序中始终失败。我认为不允许在模板内随意使用表达式。
我可以将该模板分为2个模板,并将它们分别加载到各自的构建代理中,但这是最后的选择。我们高度赞赏在动态创建/更改模板方面的任何帮助。
答案 0 :(得分:0)
您是否正在寻找有条件的集装箱作业?查看我的脚本:
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)