Parameters and Variables in YAML Scripts

When I was working through my Node.js and Azure Functions demo, I ran into an interesting problem using YAML variables and parameters thinking these two were one and the same.

A variable is a placeholder for configuration data within your YAML script.

A parameter is a prompt for a user to make a choice. The response to this information can be stored as a variable or left as a parameter.

As you can see from the above screenshot, both are different and when consumed in code both are different as well.

parameters:
- name: DeploymentSlotName
  type: string
  default: 'staging'
  values:
    - noder
    - staging     


variables:
    azureSubscription: 'myserviceconnection'   # Replace with your Azure DevOps service connection
    functionAppName: 'myfunction'                 # Replace with your Azure Function App name
    packagePath: '$(System.DefaultWorkingDirectory)/$(Build.BuildId).zip'

Accessing Variables and Parameters

Here is where it gets interesting: in my limited wisdom, I presumed I would access both relatively the same way – not so.

To access a variable, you use the following syntax;

$(azureSubscription)

To access a parameter, you use the following syntax;

slotName: ${{ parameters.DeploymentSlotName }}

I guess it makes sense for them to be accessed separately, but this has me stumped for a bit, and it wasn’t until I started debugging their values that I figured out why.

Again, if you’re looking for a quick debug output script, you can use the following snippet;

- script: echo "Deploying to slot - ${{ parameters.DeploymentSlotName }}"

    displayName: 'Debug Parameter DeploymentSlot'