azure-devopsyamlazure-pipelinessalesforceazure-pipelines-yaml

Getting "command not found" while printing variable in Azure DevOps yaml Pipeline


While creating yml for my build. I am having multiple environments, namely Dev, QA and Production, and have three Git branches acting as base feature branches representing each environment.

I have created their matching variable groups and trying to create a pipeline which runs on merge of Pull Request against each of these branches. Variables' name in all three of them groups are identical, just their value differs.

Following is my YAML for Azure DevOps pipeline i have created

# specific branch build with batching
trigger:
  batch: true
  branches:
    include:
      - master
      - DEV
      - QA
  paths:
    exclude:
      - README.md

pr: none

jobs:
  - job: Deploy
    variables:
      - ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/master') }}:
          group: sfdx-org-prod
      - ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/QA') }}:
          group: sfdx-org-qa
      - ${{ if eq(variables['System.PullRequest.TargetBranch'], 'refs/heads/DEV') }}:
          group: sfdx-org-dev
    # Define the pipeline jobs
    steps:
      - script: |
          echo $(System.PullRequest.TargetBranch)
          echo $(OrgAlias)
          echo $(SalesforceUserName)
        displayName: "Selected Variable Group"

When I am running the build, I am getting error saying "OrgAlias: command not found"
Image of Error Output
I have checked all variable groups, their labels, etc. can't find anything off.
sfdx-org-dev Variable Group
Since I want this build to be automated and free from user inputs, i am not implementing "runtime parameters".

Any input would be great.

I cross checked the variable labels and matched them against the one in YAML. Changed bash to script in job.

Ideally i would like it to dynamically fetch the values of variables in variable groups.


Solution

  • The problem

    According to predefined variables, variable System.PullRequest.TargetBranch is not available in templates (compile time). This means you cannot use it in template expressions such as ${{ if eq(...) }}.

    Also, variable System.PullRequest.TargetBranch is not available in CI triggers like the one defined in your pipeline:

    trigger:
      batch: true
      branches:
        include:
          - master
          - DEV
          - QA
      paths:
        exclude:
          - README.md
    

    Workaround

    Instead of having a single pipeline, consider creating a pipeline for each branch/environment.

    A base pipeline (using extends templates) can be used to reduce code duplication.

    /pipelines/base-pipeline.yaml

    parameters:
      - name: environment
        displayName: Environment to deploy to
        type: string
        values:
          - dev
          - qa
          - prod
    
    jobs:
      - job: Deploy
        variables:
          - group: sfdx-org-${{ parameters.environment }}
        # Define the pipeline jobs
        steps:
          - script: |
              echo $(OrgAlias)
              echo $(SalesforceUserName)
            displayName: "Selected Variable Group"
    

    /pipelines/dev-pipeline.yaml

    Dev specific branch:

    trigger:
      batch: true
      branches:
        include:
          - DEV
      paths:
        exclude:
          - README.md
    
    pr: none
    
    extends:
      template: /pipelines/base-pipeline.yaml
      parameters:
        environment: dev # <-------------- hard-coded value
    

    /pipelines/qa-pipeline.yaml

    QA specific branch:

    trigger:
      batch: true
      branches:
        include:
          - QA
      paths:
        exclude:
          - README.md
    
    pr: none
    
    extends:
      template: /pipelines/base-pipeline.yaml
      parameters:
        environment: qa # <-------------- hard-coded value
    

    /pipelines/prod-pipeline.yaml

    Prod specific branch:

    trigger:
      batch: true
      branches:
        include:
          - master
      paths:
        exclude:
          - README.md
    
    pr: none
    
    extends:
      template: /pipelines/base-pipeline.yaml
      parameters:
        environment: prod # <-------------- hard-coded value
    

    Notes: