7

I am trying to set variables based on a parameter value in a yaml pipeline. It seems that I've read many other posts which show examples like the one below that the authors have said worked, but I cannot get past issues when trying to do something like below.

I've tried many variations on this example as well, too many to list here. Sometimes it will show 'values' as a duplicate key. In other cases I've been able to try and start a run and get the prompt with environment selection, but then opening the stage dialog throws a parse error.

Is there some sort of difference between variable declaration at the top of the file vs in a stage or job? That seems to be the difference that I notice when reading through other examples.

Ultimately what I'm trying to do is set the ServiceConnection variable value based on the value of the environment parameter.

parameters:
- name: environment
  displayName: Environment
  type: string
  values:
  - DEV
  - TEST

pr: none
trigger: none

pool: PrivateAgentPool

variables:
  - name: 'isMain'
    value: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
  - name: 'buildConfiguration'
    value: 'Release'

  - name: 'environment'
    value: ${{ parameters.environment }}

  - name: 'ServiceConnection'
    ${{ if eq(variables['environment'], 'DEV') }}:
      value: 'svcConnectionDev'
    ${{ if eq(variables['environment'], 'TEST') }}:
      value: 'svcConnectionTest'
Mike
  • 1,010
  • 1
  • 14
  • 33

2 Answers2

4

Looks like your solution is almost correct. Consider the below example.

parameters:
  - name: region
    type: string
    default: westeurope
    values:
      - westeurope
      - northeurope

variables:
  ${{ if eq(parameters['region'], 'westeurope') }}:
    ServiceConnection: "svcConnectionDev"
  ${{ else }}:
    enter code here

if you want to used this ServiceConnectionvar across you can do it just by calling $ServiceConnection

ElonMusk99
  • 69
  • 2
  • Thanks for the response, I appreciate it. I'm not clear on how this is what's needed: the goal is to set a ServiceConnection variable, you're right, but it is conditional on the parameter value. If parameter.environment = 'DEV', $ServiceConnection = 'svcConnectionDev', and if parameter.environment = 'TEST', $ServiceConnection = 'svcConnectionTest'. If I attempt to copy/past the code above into my pipeline, it won't compile...the values following this block have a "bad indentation of a mapping entry" error. – Mike Oct 16 '21 at 14:30
  • 1
    That code might work. but if you want to conditionally set ServerConnection, I am getting duplicate key error if I put ServiceConnection: "svcAlternateConn" in the else portion where you have "enter code here" – spacebread Mar 02 '22 at 22:35
  • Thanks it solve the issues, the if conditions is useful – Gautam Sharma Apr 27 '23 at 06:24
0

you could use bash with conditions:

steps:
- bash: |
    echo "##vso[task.setvariable variable=ServiceConnection]svcConnectionDev"
  condition: eq('${{parameters.environment}}', 'DEV') 
ElGio74
  • 1
  • 2