Search code examples
azure-devopsyamlazure-pipelinesdynamics-crmazure-pipelines-yaml

Runtime Variable in Yaml Azure DevOps


If have created the Pipeline for One of our CRM Solution in Yaml I have Created variables SolutionExit,SolutionName,SolutionVersion

#back-up.yaml
parameters:
  - name: Solution
    displayName: Solution Need to be backed up
    type: object
    default: [solution1,Solution2]

trigger:
- none
variables:
  SolutionExit: ''
  SolutionVersion: ''
  SolutionName: ''
pool:
  vmImage: windows-2019

steps:
- task: MSCRMToolInstaller@12
  inputs:
    nugetFeed: 'official'
    psFeed: 'official'
- ${{each value in parameters.Solution}}:
    - task: MSCRMGetSolution@12
      inputs:
        crmConnectionString: '$(ConnectStr)'
        solutionName: '${{value}}'
        existsVariableName: 'SolutionExit' #replace existing variable with output value True or False
        versionVariableName: 'SolutionVersion' #replace existing variable with output value
        displayVariableName: 'SolutionName' #replace existing variable with output value
    - template: template/validate-solution.yml
      parameters:
        SolutionExit: $(SolutionExit)
#validate-solution.yml
parameters:
  SolutionExit: ""
steps:
- ${{if eq(parameters.SolutionExit, 'True')}}:
    - script: echo "Solution exist $(SolutionExit),$(SolutionVersion),$(SolutionName),${{parameters.SolutionExit}}"
- ${{else}}:
    - bash: echo "solution doesnt exit $(SolutionExit),$(SolutionVersion),$(SolutionName),${{parameters.SolutionExit}}"

But runtime parameters are not accepted If condition never satisfies. How to pass the runtime parameter

This Is the result for both cases If the solution available

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.214.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
Script contents:
echo "solution doesnt exit True,1.0.0.11,Solution1,True"
"C:\Program Files\Git\bin\bash.exe" -c pwd
/d/a/_temp
========================== Starting Command Output ===========================
"C:\Program Files\Git\bin\bash.exe" /d/a/_temp/237f7dc2-debc-4c47-ba88-41a6a97c49d9.sh
solution doesnt exit True,1.0.0.11,soultion1,True
Finishing: Bash

if solution not available

Starting: Bash
==============================================================================
Task         : Bash
Description  : Run a Bash script on macOS, Linux, or Windows
Version      : 3.214.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/bash
==============================================================================
Generating script.
Script contents:
echo "solution doesnt exit False,,,False"
"C:\Program Files\Git\bin\bash.exe" -c pwd
/d/a/_temp
========================== Starting Command Output ===========================
"C:\Program Files\Git\bin\bash.exe" /d/a/_temp/49e5bea1-b380-4a74-a0ce-b89d0dfb3a07.sh
solution doesnt exit False,,,False
Finishing: Bash

How to fix this issue. There is compile time and Runtime variables,as per microsoft I tried ${{if eq(variables['SolutionExit'], 'True')}}: this too didnt work. We cannot use the macro variable in conditions like $(SolutionExit)


Solution

  • From your YAML definition, the variable will generate during the process of Pipeline Run.

    The Variable should be Runtime variable. It will be expand at runtime. But the If expression and parameters will be expand at Compile time.

    In this case, If expression will not work.

    To solve this issue, you need to change to use Condition to replace the If expression.

    At the same time, you don't need to use parameters to pass the variable value. You can directly use the variable in the condition.

    For example:

    Main YAML :

    parameters:
      - name: Solution
        displayName: Solution Need to be backed up
        type: object
        default: [solution1,Solution2]
    
    trigger:
    - none
    variables:
      SolutionExit: ''
      SolutionVersion: ''
      SolutionName: ''
    pool:
      vmImage: windows-2019
    
    steps:
    - task: MSCRMToolInstaller@12
      inputs:
        nugetFeed: 'official'
        psFeed: 'official'
    - ${{each value in parameters.Solution}}:
        - task: MSCRMGetSolution@12
          inputs:
            crmConnectionString: '$(ConnectStr)'
            solutionName: '${{value}}'
            existsVariableName: 'SolutionExit' #replace existing variable with output value True or False
            versionVariableName: 'SolutionVersion' #replace existing variable with output value
            displayVariableName: 'SolutionName' #replace existing variable with output value
        - template: template/validate-solution.yml
    

    Template YAML:

    steps:
    - script: echo "Solution exist $(SolutionExit),$(SolutionVersion),$(SolutionName),${{parameters.SolutionExit}}"
      condition: eq(variables['SolutionExit'], 'true')
    
    - bash: echo "solution doesnt exit $(SolutionExit),$(SolutionVersion),$(SolutionName),${{parameters.SolutionExit}}"
      condition: ne(variables['SolutionExit'], 'true')