If a variable is defined twice within the same scope (e.g. there are two entries within a Stage's variables map, each with a different value), what is the behavior? Does the last entry in the map prevail? Is this an error case?
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
@kevinhartman Thank you for the question.
This is a duplicate of https://github.com/MicrosoftDocs/vsts-docs/issues/1142. Please refer there for any additional questions or comments.
@msebolt This issue may technically duplicate #1142, but #1142 was never given a full answer. What happens when the same variable is declared twice within a scope?
Yeah, I meant to reopen this. #1142 doesn't answer my question, which is: "what happens if you re-declare a variable in the same scope?"
For example, consider the following variable template:
parameters:
uid: '' # unique environment ID (e.g. 'prod', 'staging', 'test', 'e2e')
variables:
deployVars.terraform.skipDeploy: true
${{ if eq(parameters.uid, 'prod') }}:
deployVars.terraform.skipDeploy: false
If I instantiate this template with uid: prod, what will be the value of deployVars.terraform.skipDeploy?
Technically, deployVars.terraform.skipDeploy is redeclared with a different value in that case. Is the result well-defined? Does lexical order define the precedence?
@kevinhartman The last defined value in the scope will win out if the variables are defined in the same scope and there are no other definitions that take higher priority. I'll clarify this in the docs. Here's a simplified example:
trigger: none
variables:
- name: skipDeploy
value: true
- name: skipDeploy
value: false
stages:
- stage: one
displayName: Stage One
variables:
- name: a
value: alpha
- name: a
value: beta
jobs:
- job: I
displayName: Job I
variables:
- name: b
value: uno
- name: b
value: dos
steps:
- script: echo $(a) #outputs beta
- script: echo $(b) #outputs dos
- script: echo $(skipDeploy) #outputs false
Thanks @juliakm. I tried this this morning and unfortunately I get the error 'deployVars.terraform.skipDeploy' is already defined.
Could this be a product bug or a recent behavior change?
@PRMerger17 guessing you're a bot, but this issue should not be closed.
I am also seeing the same error and don't understand why this issue has been closed. It is clearly unresolved.
Please note only documentation changes can be made here.
All product-related issues are to be submitted here to solicit feedback for further guidance from the Azure DevOps community.
@kevinhartman I'm not sure your exact scenario but something like this may work. Please open an issue in the Developer Community if it does not. It sounds like there is a product enhancement request that the product team may be interested in.
parameters:
- name: uid # unique environment ID (e.g. 'prod', 'staging', 'test', 'e2e')
default: "prod"
type: string
variables:
${{ if eq( parameters.uid, 'prod' ) }}:
skipDeploy: false
${{ if ne( parameters.uid, 'prod' ) }}:
skipDeploy: true
steps:
- script: echo $(skipDeploy)