使用服务连接填充Azure自定义任务选择列表的技术咨询
Azure自定义任务选择列表通过服务连接加载选项的实现细节
一、可加载的数据类型与支持的数据源
- 数据类型:核心支持字符串键值对(对应下拉选项的显示文本
label和实际取值value),也可加载JSON数组用于复杂选择场景(比如多级下拉)。 - 支持的数据源:
- Azure云资源:通过Azure REST API可访问的资源组、存储账户、虚拟机等各类Azure服务资源
- Azure DevOps内部资源:借助Azure DevOps REST API可获取的项目、管道、工件、仓库、变量组等
- 自定义HTTP服务:只要服务连接能完成身份认证,任何返回合规JSON格式的第三方API/后端服务
二、能否访问Azure变量组、仓库等资源?
完全可以。通过Azure DevOps Services类型的服务连接,结合Azure DevOps官方REST API,直接对变量组、Git仓库(分支/标签)、工件源等资源发起请求,提取所需数据填充选择列表。
三、实现示例
示例1:从指定变量组加载变量名作为选项
在自定义任务的task.json中配置带数据源的输入字段:
{ "id": "targetVariable", "name": "选择变量组中的变量", "type": "pickList", "label": "变量名称", "required": true, "properties": { "EditableOptions": "False", "DataSource": "azdo-rest-api", "EndpointUrl": "{{endpoint.url}}/_apis/distributedtask/variablegroups/123?api-version=7.1-preview.2", "RequestVerb": "GET", "RequestHeaders": "{\"Authorization\":\"Bearer {{endpoint.auth.parameters.AccessToken}}\"}", "ResponseSelector": "jsonpath:$.variables.*.name", "ValueSelector": "jsonpath:$.variables.*.name" } }
说明:
EndpointUrl中的123替换为目标变量组的ID,也可通过其他输入参数动态传入- 用JSONPath从API返回结果中提取变量名,同时作为选项的显示文本和取值
示例2:从Git仓库加载分支列表
{ "id": "targetBranch", "name": "选择Git分支", "type": "pickList", "label": "分支名称", "required": true, "properties": { "EditableOptions": "True", "DataSource": "azdo-rest-api", "EndpointUrl": "{{endpoint.url}}/_apis/git/repositories/456/refs?filter=heads/&api-version=7.1-preview.1", "RequestVerb": "GET", "RequestHeaders": "{\"Authorization\":\"Bearer {{endpoint.auth.parameters.AccessToken}}\"}", "ResponseSelector": "jsonpath:$.value[*].name.replace('refs/heads/','')", "ValueSelector": "jsonpath:$.value[*].name" } }
说明:
- 通过
filter=heads/过滤仅获取分支,用replace去掉refs/heads/前缀优化显示 EditableOptions设为True允许用户手动输入分支名
示例3:从自定义API加载选项
如果需要对接自有后端服务,配置如下:
{ "id": "customOption", "name": "自定义业务选项", "type": "pickList", "label": "选择业务项", "required": true, "properties": { "EditableOptions": "False", "DataSource": "endpoint", "EndpointUrl": "{{endpoint.url}}/api/business/options", "RequestVerb": "GET", "RequestHeaders": "{\"Authorization\":\"Bearer {{endpoint.auth.parameters.AccessToken}}\"}", "ResponseSelector": "jsonpath:$.data[*].displayName", "ValueSelector": "jsonpath:$.data[*].id" } }
说明:
DataSource设为endpoint对应通用HTTP服务连接- 要求后端API返回的JSON包含
data数组,每个元素带displayName(显示文本)和id(实际取值)字段
内容的提问来源于stack exchange,提问作者CodeMonkey
相关产品推荐
相关产品推荐

