Azure Template Parameters and Variables Are Confusing

What I’m trying to do

Initially, I was trying to use a variable in a variable group I assigned to a stage in a template as follows:

  - stage: TestStage
    dependsOn: LogVariables
    variables:
      - group: APIParamsDemo
    jobs:
      - template: templates/test-template.yml

The variable group contains a variable called Environment.

  - deployment: DeployJobFoobar
    environment: $(Environment)
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                echo "Deploying to foobar environment!"
              displayName: 'Deploy to foobar'

Unfortunately the macro syntax variable, $(Environment), here doesn’t expand to insert the value into the template. Neither does the template expression equivalent ${{ Environment }}. Nor does the runtime expression version $[variables.Environment].

I also tried passing the Environment as a parameter to the template with all three forms and non of those were inserted properly either.

My Workaround

Since my parent yaml has conditions to determine which environment and therefore which stage to use anyway, I decided to not try to use the Environment variable I placed in my variable group for this.

Instead I am hardcoding the name of the environment when setting the parameter.

  - stage: TestStage
    dependsOn: LogVariables
    variables:
      - group: APIParamsDevelopment
    jobs:
      - template: templates/test-template.yml
        parameters:
          Environment: 'development-api'

Then I could just put multiple deployments in the template with a condition and a hardcoded environment. Since I still cannot get any form of a variable to resolve to its value as a value on the environment key.

  - deployment: DeployJobDemoAPI
    condition: eq('${{ parameters.Environment }}', 'deveopment-api')
    environment: 'development-api'
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                echo "Deploying to development-api environment!"
              displayName: 'Deploy to development-api'

  - deployment: DeployJobFoobar
    condition: eq('${{ parameters.Environment }}', 'foobar-api')
    environment: 'foobar-api'
    strategy:
      runOnce:
        deploy:
          steps:
            - script: |
                echo "Deploying to foobar environment!"
              displayName: 'Deploy to foobar'

Or, if thats the case, there may not be any point in using a template for the deployment at all.