0

I'm trying to make use of Container Job template in a pipeline which runs on both Microsoft Hosted and Self-Hosted (Containerized) build agents.

ContainerJob template works well when run in Microsoft Hosted but fails in Self-Hosted agent saying cannot run container inside a containerized build agent. Error makes sense though.

I figured that if below section can be added/removed conditionally then same Container Job template works in both agents.

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

But the condition is always true and container section is added always and failed in self-hosted agent always. I think expressions are not allowed randomly inside a template.

I can separate out this template into 2 templates and load them in respective build agent but that's the last resort. Any help in dynamically creating/altering a template is highly appreciated.

Venkata Dorisala
  • 4,783
  • 7
  • 49
  • 90

1 Answers1

0

Are you looking for conditional container job? See my script:

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

I can choose which container (.net core 2.1 or .net core 2.2) via the value of isSelfHosted parameter.

It uses Runtime parameters, so I can manage the condition(check or uncheck the box) when running the pipeline:

enter image description here

Update:

See Job, stage, and step templates with parameters

Move the content below into a template file:

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

Then the main azure-pipelines.yml should be

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)
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thanks. But i'm trying to use it in `template`. So i will be passing the parameter into the template and put the condition inside the template. – Venkata Dorisala Jul 24 '20 at 11:19
  • Thanks. But the the problem is accessing it inside the `xxx.yml` template and adding `container` script based on that condition. The snippet i provided in my question should be in `xxx.yml` and should be adding only when `isSelfHosted` is false. – Venkata Dorisala Jul 27 '20 at 11:00