簡體   English   中英

具有非字符串類型的任務組參數

[英]Task group parameters w/ type other than string

我有一個用例,我想在我的任務組中添加一個pickList類型的參數。 這似乎是可行的; UI 渲染良好,並且在使用任務組運行管道時變量也是正確的。 我知道您不能直接將參數配置為選擇列表,並且必須通過 JSON 手動完成。

然后我做了什么:

  • 用我的任務和必要的變量創建了一個任務組。 一切都好,變量作為參數出現, $(someType)$(someValue)
  • 我希望$(someType)變量成為一個pickList - 我導出了所述任務組,從 JSON 中刪除了與原始任務組的所有關系(id's and what-not),將我的輸入字段編輯為pickList類型,並添加options -array 的必要選項。
  • 使用編輯后的值導入所述任務組。 工作正常。 在發布管道中使用它。 工作得很好。
  • 我在任務組中的一個腳本遇到問題,去編輯任務組。 我一按保存,它會將所有參數轉換為字符串,現在它顯然已經壞了。

我已經包含了一個任務組的最小工作 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,將任務組添加到發布管道並運行它,您將看到它正確輸出您從選擇列表中選擇的任何內容。

如果你然后 go 編輯任務組,一旦你保存它,它將被破壞(將多行和選擇列表轉換為字符串類型)。

如果可以以任何方式實現(使用字符串類型以外的參數),任何人都有這方面的經驗嗎?

請不要在 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

您可以嘗試使用此電源 shell 腳本來更新電源 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 命令行開關(我不是 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM