22

I'm just starting to work with runtime parameters in Azure Pipelines and there's something I can't quite figure out. Considering this Azure Pipelines YAML:

parameters:
- name: deployEnvironment
  displayName: Select your target environment.
  type: string
  default: Build_only
  values:
  - Build_only
  - TST
  - PP
  - P
- name: releaseName
  type: string
  default: ''

steps:
- task: ....

Why is releaseName a required parameter? I was hoping that by specifying default: '' it would be optional to be left empty. The documentation doesn't mention if parameters can be made optional.

image

Following up on Kryzstof's answer, I experimented a little further and it seems that a string consisting only of whitespaces is interpreted as empty:

It seems that this single whitespace is interpreted as empty (I've also tried multiple whitespaces).

parameters:
- name: myString
  type: string
  default: ' '

steps:
- task: PowerShell@2
  inputs:
    targetType: inline
    script: |
      $MS = $ENV:MS
      Write-Host "myString value is '$MS'"
      Write-Host "Its length is $($MS.Length)"
      Write-Host "is it null or empty? $([System.String]::IsNullOrEmpty($MS))"
      Write-Host "Is it null or whitespace? $([System.String]::IsNullOrWhiteSpace($MS))"
  env:
    MS: ${{ parameters.myString }}

This yields:

myString value is '' Its length is 0 is it null or empty? True Is it null or whitespace? True

Walter Vos
  • 436
  • 1
  • 3
  • 13
  • 2
    There is a pending feature request for this: https://developercommunity.visualstudio.com/t/optional-runtime-parameter/975396 – mhu Nov 07 '22 at 12:21

3 Answers3

24

This is really strange. But if you put a space ' ' instead of an empty string '', then you will be able to trigger pipeline, even deleting that space from field.

build with runtime parameters

Kashyap
  • 15,354
  • 13
  • 64
  • 103
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • That's interesting, thanks! Do you know where I would file a bug report for Azure Pipelines? I'll accept your answer, as it provides a decent workaround. – Walter Vos Apr 09 '20 at 09:39
  • 4
    Seems this workaround no longer works - spaces no longer count as text in this field. @WalterVos have you found another solution since this? – mochsner Mar 14 '22 at 23:05
  • 1
    It actually works, if in the pipeline YAML the default is set to `' '`, but when it is set to `''`, it is not possible to set a single space as parameter value when triggering the pipeline. – kap Feb 22 '23 at 13:12
12

As per the documentation, "parameters cannot be optional".

Putting a space ' ' as the default value will work (as Krzysztof suggested), but the space will remain in the field. If you delete the space in the field so that there is no text value, the default value of ' ' will be used when the pipeline is run.

pjivers
  • 1,769
  • 18
  • 27
5

Single whitespace of runtime parameter is not interpreted as empty. In the example demonstrated by author, the run time parameter is firstly passed to an environment variable MS. This step can trim leading and trailing whitespaces. That's why MS is an empty string. If try this:

parameters:
- name: myString
  type: string
  default: ' '

steps:
- script: |
    echo ">>${{ parameters.myString }}<<"

it will produce:

>> <<

Anyway, passing runtime parameter to script through environment variable is a good workaround trimming whitespace. Otherwise, we have to deal with the whitespace issue in script.

Xin Wang
  • 51
  • 1
  • 1