Home > Blockchain >  Azure DevOps Pipeline conditional script arguments
Azure DevOps Pipeline conditional script arguments

Time:01-10

Is it possible to conditionally add arguments to a script within a pipeline task, based on a value of a pipeline paramater?

Example:

 parameters:
   - name: paramName
     displayName: DisplayName
     type: boolean
     default: trueasd

 steps:
 - task: PowerShell@2
   displayName: TaskName
   inputs:
     filePath: $(System.DefaultWorkingDirectory)/someScript.ps1
     arguments:  -DefaultArg1 DefaultArg1_Value `
                 -DefaultArg2 DefaultArg2_Value
     ${{ if eq(parameters.paramName, 'false') }}:
                 -ConditionalParameter

ConditionalParameter is a [switch] parameter. The script which is called in PowerShell@2 task is also meant for local use, which is why a [switch] parameter is much easier for the users, as you do not have to provide a value to it, as with [boolean] type, and the script has a bunch of such parameters.

Or will i have to change both the pipeline parameter and powershell script parameter types to boolean, as it is not possible to use a compile time pipeline expression in this case?

CodePudding user response:

It is not correct to set the condition in the value string of the input parameter on pipeline task.

To conditionally set the value of input parameter on pipeline task, you can do like as below.

. . .

  - task: PowerShell@2
    displayName: 'TaskName'
    inputs:
      filePath: '$(System.DefaultWorkingDirectory)/someScript.ps1'
      ${{ if eq(parameters.paramName, 'false') }}:
        arguments: >
          -DefaultArg1 DefaultArg1_Value
          -DefaultArg2 DefaultArg2_Value
          -ConditionalParameter
      ${{ else }}:
        arguments: >
          -DefaultArg1 DefaultArg1_Value
          -DefaultArg2 DefaultArg2_Value

For more details, you can reference the following documents:

  • Related