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.
Are you looking for conditional container job? See my script:
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:
Update:
See Job, stage, and step templates with parameters
Move the content below into a template file:
Then the main
azure-pipelines.yml
should be