如何创建带[+ Add]按钮、类Get sources工作区映射的VSTS/TFS构建任务动态输入?
嘿,我之前刚好折腾过自定义VSTS/TFS构建任务的动态输入功能,给你详细说下怎么实现类似Get sources里的可添加多行映射的效果:
核心方案:使用
multiInput字段类型 VSTS/TFS的构建任务系统原生支持一种叫multiInput的输入类型,专门用来实现这种可动态添加多行输入的场景,和Get sources的工作区映射逻辑完全一致。
1. 在task.json中定义动态输入字段
首先修改你的任务配置文件task.json,添加一个multiInput类型的字段,用来承载多行的路径映射。下面是一个完整的示例片段:
{ "$schema": "https://raw.githubusercontent.com/microsoft/azure-pipelines-task-lib/master/tasks.schema.json", "id": "YourCustomTaskId", "name": "Custom Workspace Mapper", "friendlyName": "Custom Workspace Mapper", "description": "Map server paths to local paths dynamically", "author": "Your Name", "version": { "Major": 1, "Minor": 0, "Patch": 0 }, "instanceNameFormat": "Workspace Mapper", "inputs": [ { "id": "workspaceMappings", "name": "Workspace Mappings", "type": "multiInput", "label": "Map server paths to local paths", "defaultValue": "", "required": false, "properties": { "editorExtension": "ms.vss-build-web.multi-input-editor", "inputs": [ { "id": "serverPath", "name": "Server Path", "type": "string", "label": "Server Path (e.g. $/MyProject)", "required": true, "helpMarkDown": "The path in your TFS/VSTS repository" }, { "id": "localPath", "name": "Local Path", "type": "string", "label": "Local Path (e.g. src/MyProject)", "required": true, "helpMarkDown": "The local path on the build agent" } ] } } ], "execution": { "Node16": { "target": "index.js" } } }
关键字段说明:
type: "multiInput":指定这是一个可动态添加的多行输入字段editorExtension: "ms.vss-build-web.multi-input-editor":指定使用官方的多输入编辑器,这样就能自动生成[+ Add]和[- Remove]按钮inputs数组:定义每一行输入包含的子字段,这里我们模拟Get sources的结构,设置了服务器路径和本地路径两个输入框
2. 在任务代码中读取动态输入的值
接下来在你的任务执行代码(比如Node.js的index.js)中,读取用户添加的所有映射项。官方的azure-pipelines-task-lib库提供了专门的方法来处理这种输入:
const tl = require('azure-pipelines-task-lib/task'); try { // 获取所有映射行,每行用换行符分隔 const mappingLines = tl.getDelimitedInput('workspaceMappings', '\n'); // 遍历每一行,拆分服务器路径和本地路径(官方multiInput用|分隔子字段) mappingLines.forEach((line, index) => { const [serverPath, localPath] = line.split('|'); if (!serverPath || !localPath) { tl.warning(`Mapping line ${index + 1} is invalid, skipping.`); return; } // 这里添加你的业务逻辑,比如创建本地目录、配置工作区等 console.log(`Processing mapping: Server Path "${serverPath}" -> Local Path "${localPath}"`); // 示例:创建本地目录 tl.mkdirP(localPath); }); tl.setResult(tl.TaskResult.Succeeded, 'Workspace mappings processed successfully'); } catch (err) { tl.setResult(tl.TaskResult.Failed, err.message); }
注意点:
- 每一行的子字段是用
|分隔的,所以要用split('|')来拆分 - 记得做合法性校验,避免用户输入空值导致错误
3. 打包并测试任务
完成配置和代码后,用tfx-cli工具把任务上传到你的VSTS/TFS实例:
# 安装tfx-cli(如果没装过) npm install -g tfx-cli # 登录到你的VSTS/TFS tfx login --service-url https://your-account.visualstudio.com # 上传任务 tfx build tasks create --task-path ./your-task-folder
然后在构建管道中添加这个任务,你就能看到和Get sources完全一样的动态添加界面了——点击[+ Add]可以添加新的映射行,点击每行的[-]可以删除,完美符合你的需求。
额外优化建议
- 可以给子字段添加
helpMarkDown来提示用户输入格式 - 如果需要默认的映射项,可以在
defaultValue中用换行和|分隔,比如:"defaultValue": "$/MyProject|src/MyProject\n$/AnotherRepo|src/AnotherRepo" - 支持更多子字段类型,比如把本地路径改成
filePath类型,让用户可以选择本地目录
内容的提问来源于stack exchange,提问作者Thad Peiffer
相关产品推荐
相关产品推荐

