Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow an empty string for a runtime parameter?

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

like image 547
Walter Vos Avatar asked Apr 08 '20 19:04

Walter Vos


2 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

like image 104
Krzysztof Madej Avatar answered Nov 11 '22 04:11

Krzysztof Madej


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.

like image 7
pjivers Avatar answered Nov 11 '22 04:11

pjivers