'Yaml Variable not working for dockerfile using variables

So I have the following variable comdition defined in my YAML

  ${{ if endsWith( variables['Build.SourceBranchName'], 'main' ) }}: 
    dFile: 'Dockerfile'
  ${{ if endsWith( variables['Build.SourceBranchName'], 'development' ) }}: 
    dFile: 'Development.Dockerfile'

I then use this in a build image task like this:

      - task: Docker@0
        displayName: 'Build an image'
        inputs:
          azureSubscription: 'Azure Registry'
          azureContainerRegistry: 'redacted'
          dockerFile: $[variables.dFile]
          imageName: '$(Build.Repository.Name)-$(build.sourceBranch):$(Build.BuildNumber)'
          includeLatestTag: true

But for some reason this is the output I get:

##[error]Unhandled: No Docker file matching  /home/vsts/work/1/s/$[variables.dockerFile]  was found.

I also tried '$(dFile)' and got the same error



Solution 1:[1]

Defines the variables as name-value pair.

Here is what your variables block would look like:

variables:
  - ${{ if endsWith( variables['Build.SourceBranchName'], 'main' ) }}:
      - name: dFile
        value: 'Dockerfile'

  - ${{ if endsWith( variables['Build.SourceBranchName'], 'development' ) }}:    
      - name: dFile
        value: 'Development.Dockerfile'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sudarshan_SMD