I have an azure pipeline yml, I added some conditionals but now I'm getting errors messages in two conditionals
When I run the pipeline I get this error message: /build-pipeline.yml (Line: 208, Col: 9): While parsing a block mapping, did not find expected key.
After that I installed prettier and in VScode it shjows me the next error messages:
A block sequence may not be used as an implicit map keyYAML Implicit keys need to be on a single lineYAML Implicit map keys need to be followed by map valuesYAML
The code in the two conditionals is the same since is the same code but executed in different parts of the pipeline.
The error that I get is in the first conditional - ${{ if eq(variables.isQa, true) }}: that checks if the environment is QA but I'm guessing that the whole code is badly formatted
- task: Docker@0
displayName: 'Build an image'
inputs:
command: 'buildAndPush'
- ${{ if eq(variables.isQa, true) }}:
buildContext: $(Pipeline.Workspace)/enext-data-platform-backend/
- ${{ if eq(variables.isDev, true) }}:
buildContext: enext-data-platform-backend
containerregistrytype: 'Container Registry'
imageName: "$(Build.Repository.Name):$(Build.BuildId)"
- ${{ if eq(variables.isQa, true) }}:
Dockerfile: $(Pipeline.Workspace)/enext-data-platform-backend/Dockerfile
- ${{ if eq(variables.isDev, true) }}:
Dockerfile: 'Dockerfile'
This exact part of code is executed in another part of the pipeline and I'm getting the same error in the same conditional.
I've tried acommodating the spaces a bunch of times and also modifying the conditionals, but since I'm new working with yaml files I know I'm missing something, This is the only part of the pipeline where I get the error, in other conditionals that I added are working fine, I can upload all of the pipeline code if you need it.
There seem to be some misunderstandinds to use the docker
task syntax. For docker@0
task, we cannot use the property of buildContext
and it should be used docker@1
or docker@2
instead. See the syntax differences in Docker@0 - Docker v0 task | Microsoft Learn, Docker@1 - Docker v1 task | Microsoft Learn and Docker@2 - Docker v2 task | Microsoft Learn.
Here is a modified YAML for your reference, where the conditional insertions begin with no dash -
to infer sequence. Since you have been evaluating the values of two variables isQa
and isDev
, please make sure their values are not set as true
at the same time and you may also consider using ${elseif}
expressions to have the condtion eq(variables.isDev, true)
only work when nq(variables.isQa, true)
(see Conditional insertion for more information).
steps:
- task: Docker@1
displayName: 'Build an image'
inputs:
command: 'buildAndPush'
${{ if eq(variables.isQa, true) }}:
buildContext: $(Pipeline.Workspace)/enext-data-platform-backend/
${{ elseif eq(variables.isDev, true) }}:
buildContext: enext-data-platform-backend
containerregistrytype: 'Container Registry'
imageName: "$(Build.Repository.Name):$(Build.BuildId)"
${{ if eq(variables.isQa, true) }}:
Dockerfile: $(Pipeline.Workspace)/enext-data-platform-backend/Dockerfile
${{ elseif eq(variables.isDev, true) }}:
Dockerfile: 'Dockerfile'
In addition, kindly be also advised to use the Validate
function of a pipeline editor to check and locate syntax errors.