How to allow an empty string for a runtime parameter?

Viewed 14367

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

3 Answers

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

build with runtime parameters

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.

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.

Related