具有非字符串类型的任务组参数

Task group parameters w/ type other than string

我有一个用例,我想在我的任务组中添加一个 pickList 类型的参数。这似乎是可行的; UI 渲染良好,并且当 运行 使用任务组连接管道时变量也是正确的。我知道您不能将参数直接配置为选择列表,而这必须通过 JSON.

手动完成

然后我做了什么:

我提供了一个任务组的最小工作 JSON 示例,其中包含 multilinepickList 类型参数。

{
   "tasks":[
      {
         "environment":{
            
         },
         "displayName":"PowerShell Script",
         "alwaysRun":false,
         "continueOnError":false,
         "condition":"succeeded()",
         "enabled":true,
         "timeoutInMinutes":0,
         "inputs":{
            "targetType":"inline",
            "filePath":"",
            "arguments":"",
            "script":"Write-Host \"$(picklisttype)\"\nWrite-Host \"$(mlvalue)\"",
            "errorActionPreference":"stop",
            "failOnStderr":"false",
            "showWarnings":"false",
            "ignoreLASTEXITCODE":"false",
            "pwsh":"false",
            "workingDirectory":""
         },
         "task":{
            "id":"e213ff0f-5d5c-4791-802d-52ea3e7be1f1",
            "versionSpec":"2.*",
            "definitionType":"task"
         }
      }
   ],
   "runsOn":[
      "Agent",
      "DeploymentGroup"
   ],
   "name":"my-task-group-with-picklist",
   "version":{
      "major":1,
      "minor":0,
      "patch":0,
      "isTest":false
   },
   "iconUrl":"https://my-own-custom-image.com/images/icon.png",
   "friendlyName":"My Task Group w/ PickList",
   "description":"This task group contains a picklist. Awesome.",
   "category":"Deploy",
   "definitionType":"metaTask",
   "author":"Myself",
   "demands":[
      
   ],
   "groups":[
      
   ],
   "inputs":[
      {
        "aliases": [],
        "options": {
            "option1": "First option",
            "option2": "Second option (default)",
            "option3": "Third option"
        },
        "properties": {},
        "name": "picklisttype",
        "label": "Pick a type",
        "defaultValue": "option2",
        "required": true,
        "type": "pickList",
        "helpMarkDown": "Just pick a type!",
        "groupName": ""
      },
      {
         "aliases":[],
         "options":{},
         "properties":{},
         "name":"mlvalue",
         "label":"Write several lines",
         "defaultValue":"This contains\nseveral lines\nof text.\nHowever, you it is\nquite small and it\nis not possible to alter\nits appearance...",
         "required":true,
         "type":"multiLine",
         "helpMarkDown":"Write some awesome text.",
         "groupName":"",
         "visibleRule": "picklisttype != option3"
      }
   ],
   "satisfies":[
      
   ],
   "sourceDefinitions":[
      
   ],
   "dataSourceBindings":[
      
   ],
   "instanceNameFormat":"Default name with default value $(picklisttype)",
   "preJobExecution":{
      
   },
   "execution":{
      
   },
   "postJobExecution":{
      
   }
}

如果您导入所说 JSON,将任务组添加到发布管道并 运行 它,您会看到它正确输出您从选择列表中选择的任何内容。

如果你再去编辑任务组,一旦你保存它,它就会被破坏(将多行和选择列表转换为字符串类型)。

有没有人对此有任何经验,是否有可能以任何方式实现(使用非字符串类型作为参数)?

请不要更新 UI 中的任务组任务,您应该通过 REST API.

更新它

通过REST API

获取任务组信息
GET https://dev.azure.com/{organization}/{project}/_apis/distributedtask/taskgroups/{taskGroupId}?api-version=6.0-preview.1

然后通过这个REST API

更新任务组信息
PUT https://dev.azure.com/{organization}/{project}/_apis/distributedtask/taskgroups/{taskGroupId}?api-version=6.0-preview.1

您可以试试这个 power shell 脚本来更新 power shell 打印信息。

$url = "https://dev.azure.com/{org name}/{project name}/_apis/distributedtask/taskgroups/{task group ID}?api-version=6.0-preview.1"

$connectionToken="{pat}"

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$taskGroups = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get

#Write-Host $taskGroups.value.tasks.inputs.script

$taskGroups.value.tasks.inputs.script = 'Write-Host "$(picklisttype)"
Write-Host "$(mlvalue)" 
Write-Host "$(picklisttype)"'

$json = $taskGroups.value | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $json -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT

#Write-Host $taskGroups.value.tasks.inputs.script

结果:

任务组页面:

发布打印信息:

作为@Vito Liu-MSFT 回答的扩展,这是我最终使用的一个简单的 PS commandlet(我不是 PS 脚本方面的专家,所以这可能是改进):

Function Get-TaskGroup {
    [CmdletBinding()]
        param (
        [Parameter(Mandatory=$true, HelpMessage="DevOps organization")][string]$org,
        [Parameter(Mandatory=$true, HelpMessage="Organization project")][string]$project,
        [Parameter(Mandatory=$true, HelpMessage="Task group ID")][string]$tgId,
        [Parameter(Mandatory=$true, HelpMessage="DevOps PAT")][string]$pat,
        [Parameter(Mandatory=$false, HelpMessage="DevOps PAT")][string]$path
    )
    
    $auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
    $url = "https://dev.azure.com/$org/$project/_apis/distributedtask/taskgroups/$tgId`?api-version=6.0-preview.1"
    $tg = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $auth"} -Method GET -ErrorAction Stop
    if ($tg -and $tg.value) {
        echo $tg.value
            
        if ($path) {
            echo "$($tg.value | ConvertTo-Json -Depth 10)" > $path
        }
    }
    else {
        throw "Failed to fetch task group with id $tgId"
    }
}

Function Put-TaskGroup {
    [CmdletBinding()]
        param (
        [Parameter(Mandatory=$true, HelpMessage="DevOps organization")][string]$org,
        [Parameter(Mandatory=$true, HelpMessage="Organization project")][string]$project,
        [Parameter(Mandatory=$true, HelpMessage="Task group ID")][string]$tgId,
        [Parameter(Mandatory=$true, HelpMessage="DevOps PAT")][string]$pat,
        [Parameter(Mandatory=$false, HelpMessage="Valid task group object as JSON.")][string]$json,
        [Parameter(Mandatory=$false, HelpMessage="Path to file containing valid task group object as JSON.")][string]$path
    )
    BEGIN {
        if (!$json -and !$path) {
            throw "Must provide either a valid JSON string using the -json parameter or a path to a file with a valid JSON object using the -path parameter."
        }
        ElseIf ($json -and $path) {
            echo "Both -json and -path supplied, using string provided with -json"
        }
        ElseIf (!$json -and $path -and ![System.IO.File]::Exists($path)) {
            throw "$path does not exist"
        }
    }
    PROCESS {
        $body = ""
        if ($json) {
            $body = $json
        }
        else {
            $body = Get-Content $path
        }
        
        $auth = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
        $url = "https://dev.azure.com/$org/$project/_apis/distributedtask/taskgroups/$tgId`?api-version=6.0-preview.1"
        $tg = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $body -Headers @{authorization = "Basic $auth"} -Method PUT -ErrorAction Stop -TimeoutSec 10
        if ($tg) {
            echo $tg
        }
    }
}

示例:

$tg = Get-TaskGroup -org $org -project $project -tgId $tgId -pat $pat -path $path
$tg = Put-TaskGroup -org $org -project $proj -tgId $tgId -pat $pat -path $path