1

kinda stuck at this point and haven't been able to find any clear documentation for this example.

I have some parameters to a YAML pipeline shown below (SonarProjectKey & sonarProjectName). I am trying to set a variable to either true or false depending on whether these params have a value.

I have set them to initial values of ' ' deliberately so that you can run the pipeline without having to provide a value.

The following example doesnt work:

   parameters:
    - name: sonarProjectKey                                 # Sonar project key parameter
      displayName: Sonar Project Key
      default: ' '
    - name: sonarProjectName        
      displayName: Sonar Project Name
      default: ' '  
    
    variables:
    - name: runSonarAnalysis
      value: $[and( not(eq('${{ parameters.sonarProjectKey }}', '')), not(eq('${{ parameters.sonarProjectName }}', '')))]

I have trued so many different ways and can't seem to get it to work.

Someone please help!

1
  • The default values provided in the parameters section are (non-empty) strings with a space, while in the last row you are comparing with empty strings, that of course are NOT equal to the ones above. First of all, I'd suggest to retry by putting empty strings also in the parameters section. Commented Apr 11, 2022 at 9:18

1 Answer 1

4

Try to created variable with below syntax

parameters:
- name: sonarProjectKey                                 # Sonar project key parameter
  displayName: Sonar Project Key
  default: ' '
- name: sonarProjectName        
  displayName: Sonar Project Name
  default: ' '    
variables:
- ${{ if eq(${{ parameters.sonarProjectKey }}', ' ' ) }}:
- name: runSonarAnalysis
  value: false
- ${{ else }}:
- name: runSonarAnalysis
  value: true
Sign up to request clarification or add additional context in comments.

2 Comments

Searched in so many platforms for this answer. This solution perfectly worked for me with small indentation fix. ``` - ${{ if eq(${{ parameters.sonarProjectKey }}', ' ' ) }}: - name: runSonarAnalysis value: false - ${{ else }}: - name: runSonarAnalysis value: true ```
If someone gets this error "unexpected value 'variable'", please add the indentation for all the lines below variables:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.