Tips on utilizing a connected service in a custom Azure DevOps extension's index.ts file

I have created a unique extension for Azure DevOps that includes a specialized Connected Service and Build task. When setting up the task through the pipeline visual designer, I am able to utilize the Connected Service to choose a service and then populate a picklist with data from my API.

My question is, how can I access the selected service when the task is running? In the index.ts file, I can obtain the Guid of the service using code similar to the snippet below. But, is there a way to use this Guid to retrieve the service or its details?


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;
    } ...

I have searched extensively and reviewed various articles but have not come across any examples that address this specific scenario.

https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops

https://learn.microsoft.com/en-us/azure/devops/extend/develop/service-endpoints?view=azure-devops

Answer №1

Utilizing additional functions from the azure-pipelines-task-lib/task library, specifically the tl object, is key to solving this issue:

If your custom Connected Service incorporates an authentication scheme categorized as

ms.vss-endpoint.endpoint-auth-scheme-token
, then the token input's identifier would be apitoken. In order to retrieve and use this token, you would need to implement code similar to the following:

const endpoint = tl.getEndpointUrl(serviceString, true);
// The second parameter in this context pertains to the input name associated with the specific type of authenticationScheme being utilized.
const token = tl.getEndpointAuthorizationParameter(serviceString, "apitoken", false)

My knowledge on this topic was gained through hands-on experimentation.

Diverse Authentication Methods

In my personal experience, aligning with the sentiments expressed by others' perspectives: Azure DevOps does not readily accommodate fully bespoke Connected Services. Instead, it provides a selection that covers various requirements. Depending on the chosen method(s), the value passed for the second parameter of

tl.getEndpointAuthorizationParameter
varies:

ms.vss-endpoint.endpoint-auth-scheme-token

This scheme includes a standard input:

  1. apitoken

ms.vss-endpoint.endpoint-auth-scheme-basic

This scheme boasts two inputs:

  1. username
  2. password

Illustrative Instance with a Recommendation

Firstly, consider renaming your serviceString variable to connectedServiceId (or any variation that clarifies its purpose as representing the connected service's ID) to enhance code readability.

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 provided');
            return;
        }

        const endpoint = tl.getEndpointUrl(connectedServiceId, true);
        const token = tl.getEndpointAuthorizationParameter(connectedServiceId, "apitoken", false)
    }
    finally {
        // Handling potential failures here
    }
}

Furthermore, incorporating the connectedServiceId == undefined check ensures secure usage of the variable in subsequent function calls.

Useful Examples I Referenced/Created

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for preventing the occurrence of a final empty line in Deno's TextLineStream

I executed this snippet of code: import { TextLineStream } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7201061632425c4341445c42">[email protected]</a>/streams/mod.ts"; const cm ...

Exploring the Differences Between Native Script and Ionic: A Guide to Choosing the Right Framework for Your Hybrid Apps

When deciding between Ionic and Native Script for hybrid app development, which technology would you recommend? Or do you have another suggestion knowing that I am familiar with Angular 6? Also, I am looking for a Native Script tutorial, preferably in vide ...

Guide to creating a Map with typescript

I've noticed that many people are converting data to arrays using methods that don't seem possible for me. I'm working with React and TypeScript and I have a simple map that I want to render as a list of buttons. Here is my current progres ...

How to Eliminate Lower Borders from DataGrid Component in Material UI (mui)

I've been trying to customize the spacing between rows in a MUI Data Grid Component by overriding the default bottom border, but haven't had much success. I've experimented with different approaches such as using a theme override, adding a c ...

Is it true that "Conditional types" are not compatible with actual functions?

Checking out https://www.typescriptlang.org/docs/handbook/2/conditional-types.html I'm curious as to why this code is not functioning properly? interface IdLabel { id: number } interface NameLabel { name: string } type NameOrId<T extends num ...

Animation on React child component disappears when re-rendered

My React application utilizes Material UI with a component (let's call it DateSelector) as shown below, slightly altered for demonstration purposes. Material UI provides animations for clicks and interactions within its components. An interesting obs ...

When using the `create-react-app` command with the `--typescript` flag, you may encounter an issue when attempting to compile namespaces with the `--

I started a project using npx create-react-app my-app-ts --typescript, then I added two new files, sw-build.js and sw.js in the src/ directory. The content of these files was taken from the discussion on Workbox guidelines at (Guidlines for Using Workbox) ...

Using optional chaining on the left side in JavaScript is a convenient feature

Can the optional chaining operator be used on the left side of an assignment (=) in JavaScript? const building = {} building?.floor?.apartment?.number = 3; // Is this functionality supported? ...

I'm having trouble importing sqlite3 and knex-js into my Electron React application

Whenever I try to import sqlite3 to test my database connection, I encounter an error. Upon inspecting the development tools, I came across the following error message: Uncaught ReferenceError: require is not defined at Object.path (external "path ...

How to exclude specific {} from TypeScript union without affecting other types

Consider the following union type: type T = {} | ({ some: number } & { any: string }) If you want to narrow this type to the latter, simply excluding the empty object won't work: type WithEntries = Exclude<T, {}> This will result in neve ...

What could be causing this TypeScript class to not perform as anticipated?

My goal with this code snippet is to achieve the following: Retrieve a template using $.get(...), Attach an event listener to the input element within the template I am using webpack to transpile the code without encountering any issues. The actual cod ...

Resolving Problems with Ion-Split-pane in the Latest Versions of Angular and Ionic

There seems to be a perplexing issue with the ion-split-pane element that I just can't wrap my head around. Once I remove the split-pane element, the router-outlet functions perfectly fine. The navigation is displayed after a successful login. The on ...

The issue of NestJS dependency injection not working within the MongooseModule.forFeatureAsync function call has been encountered

I'm having an issue setting up a pre-save hook on my User model. Here's the code snippet from my users.module.ts: @Module({ controllers: [ UsersController, ], exports: [ UsersService, ], providers: [ UsersService, ], imp ...

Issue with debugging Azure Functions TypeScript using f5 functionality is unresolved

I am encountering issues running my Azure TypeScript function locally in VS code. I am receiving the errors shown in the following image. Can someone please assist me with this? https://i.stack.imgur.com/s3xxG.png ...

Ensuring the inclusion of library licenses in the production build is a crucial step

We have numerous dependencies (node_modules) in our Angular app, which are typically licensed under Apache 2.0 or MIT. From my understanding of the licenses, the production build is considered a "derived work" and we are required to include copyright notic ...

Utilizing handpicked information in Angular: A beginner's guide

Being new to Angular and programming in general, I am currently navigating through the intricacies of Angular and could use some guidance on utilizing selected data. For instance, I would like to use a personnel number view image here and send it to the b ...

Leverage the power of function overloading in TypeScript for efficient code

How can function overloading be reused effectively in TypeScript? Consider a scenario where a function is overloaded: function apply(value: number): number; function apply(value: string): string; function apply(value: any): any { return value; } No ...

When attempting to run a Typescript Playwright test in Visual Studio Code's debug mode, it inexplicably fails to execute

I am fairly new to TypeScript and Playwright, but I have experience in coding. I believe I have a good understanding of what I am trying to achieve, but I am encountering a problem that I can't seem to figure out. I'm hoping someone can help me. ...

Issue arising from passing an object through a component in a Next.js application during build time when the object is retrieved from a database

Can anyone provide assistance with solving this issue? I have an object called "diary" coming from a database, which is passed to a component where a useState hook is expecting that object. During build time, the following error is occurring. An error is ...

Exploring the capabilities of google-diff-match-patch within the Angular framework

Seeking a way to incorporate the google diff/match/patch lib into an Angular application for displaying the variance between two texts. Here's how I plan on using it: public ContentHtml: SafeHtml; compare(text1: string, text2: string):void { var ...