Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read variable as a Number in Azure DevOps Pipelines?

I have an Azure DevOps Build Pipeline YAML file that takes variables from the 'Variables' button as opposed to having "variables:" in the YAML file itself.

I'm trying to pass a Number to a step that requires a Number as the parameter, however the pipeline is failing to run because it's saying the value is not a valid Number.

Inside the "Variables" button, I have the variable VersionId with the given value 12345.

extends:
  template: Directory/To/The/Template.yaml@Name
  parameters:
    projectVersionId: $(VersionId)

Is there a way I can explicitly state that this a number, and not a string?

I have tried using both ${{variables.VersionId}} and $[variables.VersionId]

like image 439
Stefan Avatar asked Sep 11 '25 15:09

Stefan


2 Answers

The issue was that run-time variables are not yet available at the time the value pass the template parameter.

And you need to specify the value type via parameter definition:

give_parameter.yml

trigger:
- none

pool:
  vmImage: ubuntu-latest

variables:
  VersionId: 12345


extends:
  template: Directory/To/The/Template.yml
  parameters:
    projectVersionId: ${{variables.VersionId }}

Directory/To/The/Template.yml

parameters:
  - name: projectVersionId
    type: number
    default: 1


steps:

- task: PowerShell@2
  condition: ${{ eq(parameters.projectVersionId, 12345)}}
  inputs:
    targetType: 'inline'
    script: |
     # Write your PowerShell commands here.
        

You can see that we pass the check step successfully:

enter image description here

like image 100
Bowman Zhu Avatar answered Sep 14 '25 21:09

Bowman Zhu


Try $[ variables['VersionId'] ]

This somewhat explains the reason why this works https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#understand-variable-syntax

like image 34
Miguel Avatar answered Sep 14 '25 19:09

Miguel