简体   繁体   中英

TFS 2018 - Custom Build Task with boolean parameters

I've created a custom build task and its extension for TFS 2018. Two of my parameter are boolean. On task.json I set the default value to false. When I execute a build definition with my task I get the following error:

System.Management.Automation.ParameterBindingArgumentTransformationException: Cannot process argument transformation on parameter 'isToDeploy'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. ---> System.Management.Automation.ArgumentTransformationMetadataException: Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. ---> System.Management.Automation.PSInvalidCastException: Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Here is my powershell

[CmdletBinding()]
param(
    [string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] $qaApiEndpoint,
    [string][Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] $deployfxEndpoint,
    [bool][Parameter(Mandatory=$true)] $isToDeploy,
    [string][Parameter(Mandatory=$false)] $deploymentType,
    [string][Parameter(Mandatory=$false)] $environment,
    [bool][Parameter(Mandatory=$false)] $isToArchivePackage,
    [string][Parameter(Mandatory=$false)] $archiveLocation
)

Here is my Task.json (input part) (ignore ??? as this task is still in development)

"inputs": [
    {
      "name": "qaApiEndpoint",
      "type": "string",
      "label": "QA Web API Endpoint",
      "defaultValue": "",
      "required": true,
      "helpMarkDown": "Endpoint for the QA Web API.",
      "groupName": "GeneralGroup"
    },
    {
      "name": "deployfxEndpoint",
      "type": "string",
      "label": "Deploy Fx Endpoint???",
      "defaultValue": "",
      "required": true,
      "helpMarkDown": "???",
      "groupName": "GeneralGroup"
    },
    {
      "name": "isToDeploy",
      "type": "boolean",
      "label": "Deploy?",
      "defaultValue": false,
      "required": false,
      "helpMarkDown": "Should the task perform the application's deployment?",
      "groupName": "DeploymentGroup"
    },
    {
      "name": "deploymentType",
      "type": "string",
      "label": "Deployment Type",
      "defaultValue": "",
      "required": false,
      "helpMarkDown": "Ex: Full, Update, Patch",
      "groupName": "DeploymentGroup"
    },
    {
      "name": "environment",
      "type": "string",
      "label": "Environment to deploy",
      "defaultValue": "",
      "required": false,
      "helpMarkDown": "Ex: DEV, TST, QA, PRD",
      "groupName": "DeploymentGroup"
    },
    {
      "name": "isToArchivePackage",
      "type": "boolean",
      "label": "Archive Package?",
      "defaultValue": false,
      "required": false,
      "helpMarkDown": "Should the package be archived?",
      "groupName": "PackageGroup"
    },
    {
      "name": "archiveLocation",
      "type": "string",
      "label": "Archive Location",
      "defaultValue": "",
      "required": false,
      "helpMarkDown": "Path for the package archive",
      "groupName": "PackageGroup"
    }
  ]

Like the error message says, I've already tried to change the value, in Task.json, for the booleans to $False, 0 and even "false". I've also tried to change the type to bool instead and even change the parameters in json and powershell to string and then convert the values to boolean in powershell. All my tries ended up with the same error.

One thing even weirder is that I've deletied the booleans from powershell and json but I got the same error...which makes no sense at all and makes me question if i'm hitting some cache issue or something. (yes, I've restarted the machine in between some of the times).

==Edit==

The weird behavior above happens because I wasn't updating the task.json and vss-extension.json id. Need to do it between each try.

==End Edit==

The way I do to "update" my customs task is, I simply delete the task in the build definition, delete the extension from the collection and uninstall it from TFS, then I install everything again.

Combinations tested

  • boolean and false
  • boolean and 0
  • boolean and "false"
  • boolean and $False
  • boolean and "$False"
  • boolean and "$false"
  • boolean and $false
  • bool and 0

Not sure if this is still relevant or if you found a solution already.

Just providing an answer because I ran into the same issue a couple of days ago.

Was getting the same error and it appears that TFS stores its inputs as strings. For me the solution was to add -AsBool to the declaration of my variable / parameter in the executed PS1 script:

[CmdletBinding()]
[bool]$ExcludeGated = Get-VstsInput -Name ExcludeGated -AsBool

This however does require the use of function Get-VstsInput which comes with VstsTaskSdk

The JSON part for this particular Boolean looks like a standard entry:

{
          "name": "ExcludeGated",
          "type": "Boolean",
          "label": "Gated Exclusion flag",
          "defaultValue": "true",
          "required": false,
          "helpMarkDown": "your helpful markdown comment here"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM