skip to Main Content

I’m a bit new to azure pipelines and at moment I can’t solve this problem.
I have a variable stored in my azure pipeline which is: ENABLE_BUILD and the value is set to YES.

On my yaml file I have set my variables like this:

variables:
  - group: my-var-group
  - name: enableBuild
    value: '$(ENABLE_IDB)'
  - name: buildPath
    ${{ if eq(variables['enableBuild'], 'YES') }}:
      value: 'path/build-file.jar*'
    ${{ else }}:
      value: '.'

Now my question is: the enableBuild is getting the right value, which is ‘YES’.
But when I try to set the buildPath variable it always going to else statement, even if my ENABLE_IDB is filled with YES.

When I used echo, the enableBuild is getting the ‘YES’ value, and buildPath is getting ‘.’ value. Looking on internet I was not able to identify what is wrong with my if equals statement. I’m doing this validation in the correct way? Thanks.

2

Answers


  1. This happens because of the compile vs runtime time variables, while here you’re referencing the enableBuild variable at compile-time, the value of enableBuild is fetched at runtime (since it tries to access an environment variable named ENABLE_IDB).

    What are you trying to do exactly? Is this ENABLE_IDB supposed to be manually set?

    You could instead get it from a parameter:

    parameters:
    - name: enableBuild
      type: string
      values:
      - YES
      - NO
    

    And reference it like so:

      - name: buildPath
        ${{ if eq(parameters.enableBuild, 'YES') }}:
          value: 'path/build-file.jar*'
        ${{ else }}:
          value: '.'
    
    Login or Signup to reply.
  2. According to Set variables in scripts, we can set a variable for future use with scripts.

    The following is set an output variable for use in the same job. In this way, we can set the buildPath variable value based on the enableBuild value.

    trigger:
    - none
    
    variables:
    - group: my-var-group
    - name: enableBuild
      value: '$(ENABLE_IDB)'
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - task: PowerShell@2
      displayName: set buildPath value
      inputs:
        targetType: 'inline'
        script: |
          if ('$(enableBuild)' -eq 'YES') {
              echo "##vso[task.setvariable variable=buildPath]path/build-file.jar*"
          } else {
              echo "##vso[task.setvariable variable=buildPath]."
          }
    
    - bash: |
            echo $(buildPath)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search