我已经为Azure Devops编写了一个自定义扩展,其中包含一个自定义的连接服务和构建任务。在通过管道可视化设计器配置任务时,我可以使用Connected Service来选择一个服务,然后使用该服务用我的API中的数据填充选择列表。
但是,在执行任务时,如何使用所选服务。我需要从index.ts访问该服务。服务告诉我端点和API密钥。
在index.ts中,我可以使用类似以下代码的内容访问服务的Guid,但我是否可以使用Guid来获取服务或其详细信息?
import tl = require('azure-pipelines-task-lib/task');
async function run() {
try {
const serviceString: string = tl.getInput('TestService', true);
if (serviceString == 'bad') {
tl.setResult(tl.TaskResult.Failed, 'Bad input was given');
return;
} ...我做了很多搜索和阅读(包括下面的文章),但没有找到任何示例。
https://docs.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops
https://docs.microsoft.com/en-us/azure/devops/extend/develop/service-endpoints?view=azure-devops
发布于 2021-02-02 09:31:01
诀窍是使用azure-pipelines-task-lib/task中的更多函数(在本例中为tl):
如果您的自定义连接服务添加了ms.vss-endpoint.endpoint-auth-scheme-token类型的身份验证方案,则令牌输入的id将为apitoken,您需要添加的代码将如下所示:
const endpoint = tl.getEndpointUrl(serviceString, true);
// The second parameter here is the name of the associated input
// value(s) of the specific authenticationSchemes type (more on that later).
const token = tl.getEndpointAuthorizationParameter(serviceString, "apitoken", false)我怎么知道的?Experimentation。
其他身份验证类型
我目前的经验与过去的others'类似: Azure DevOps不能很好地处理完全自定义的连接服务。它确实附带了几个覆盖大多数基础的版本。根据您使用的值的不同,您为tl.getEndpointAuthorizationParameter的第二个参数传递的值会发生变化:
ms.vss-endpoint.endpoint-auth-scheme-token
附带一个标准输入:
apitokenms.vss-endpoint.endpoint-auth-scheme-basic
附带两个输入:
usernamepassword示例和建议
首先建议:为了使您的代码更清晰,请将serviceString变量重命名为connectedServiceId (或者为清楚起见,将表示所连接服务的ID的某种变体)。
import tl = require('azure-pipelines-task-lib/task');
async function run() {
try {
const connectedServiceId = tl.getInput('TestService', true);
if (connectedServiceId == 'bad' || connectedServiceId == undefined) {
tl.setResult(tl.TaskResult.Failed, 'Bad input was given');
return;
}
const endpoint = tl.getEndpointUrl(connectedServiceId, true);
const token = tl.getEndpointAuthorizationParameter(connectedServiceId, "apitoken", false)
}
finally {
// Probably report some failure here, right?
}
}此外,添加connectedServiceId == undefined检查允许在以后的函数调用中安全地使用connectedServiceId。
我发现有帮助的示例/创建的示例
https://stackoverflow.com/questions/56369857
复制相似问题