Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer a variable in parameters block in azure pipelines

I'd like to set default value of a parameter from a variable. Unfortunately, below pipeline throws this error The 'Environment' parameter value '$[variables.deployToEnv]' is not a valid value.

I tried it with other expressions too but it doesn't work. How can I solve this problem?

variables:
  - name: deployToEnv
    ${{ if in(variables['Build.SourceBranchName'], 'Development') }}:
      value: dev
    ${{ if in(variables['Build.SourceBranchName'], 'Staging', 'Testing') }}:
      value: test
    ${{ if in(variables['Build.SourceBranchName'], 'Production') }}:
      value: prod

parameters:
  - name: Environment
    type: string
    displayName: Environment
    default: $(deployToEnv)
    # default: $[variables.deployToEnv]
    # default: ${{ variables.deployToEnv }}
    values:
      - dev
      - test
      - prod
like image 205
Lukasz Dynowski Avatar asked Oct 15 '25 22:10

Lukasz Dynowski


1 Answers

From my experience, it was simply impossible to reference a variable in parameters context.

I found solution for my problem by flipping dependency. Instead setting parameter base on a variable, I set a variable base on a parameter.

parameters:
  - name: Environment
    type: string
    displayName: Environment
    default: none
    values:
      - none
      - dev
      - test
      - prod

variables:
  - name: deployToEnv
    ${{ if not(eq(parameters.Environment, 'none')) }}:
      value: ${{ parameters.Environment }}
    ${{ if and(eq(parameters.Environment, 'none'), in(variables['Build.SourceBranchName'], 'Development')) }}:
      value: dev
    ${{ if and(eq(parameters.Environment, 'none'), in(variables['Build.SourceBranchName'], 'Staging', 'Testing')) }}:
      value: test
    ${{ if and(eq(parameters.Environment, 'none'), in(variables['Build.SourceBranchName'], 'Production')) }}:
      value: prod
  - group: k8s-cluster-${{ variables.deployToEnv }}
like image 64
Lukasz Dynowski Avatar answered Oct 19 '25 04:10

Lukasz Dynowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!