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
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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With