Can't conditionally add script to Azure Devops Yaml pipeline

965 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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)