Kyrellos
Kyrellos

Reputation: 19

How to access a service connection from environment variable in azure?

I have defined a service connection of type Generic, called 'DowntimeApiKeyGen' in azure. The encrypted value of the apikey is saved under 'Password/Token Key'. How can i access the value of this apikey from an environment variable in a powershell-task under the agent phase?

I have tried various calls like following without success. enter image description here

I think, there is something wrong with calling the variable. Does anyone have an idea, how to solve this issue?

Upvotes: -1

Views: 658

Answers (2)

Kyrellos
Kyrellos

Reputation: 19

The solution involving the addition of an API key under the 'Variables' tab, whether in pipeline variables or variable groups, is working, and I was already aware of it before posting my question. However, in this approach, it requires defining the API key separately for each scope within the project. Consequently, establishing the API key as a service connection not only saves time and effort but is particularly advantageous when it comes to editing or changing it. With this setup, modifications only need to be made in one central location, rather than in every variable saved in pipeline variables or variable groups.

In recent days, I fortunately discovered a way to do that and it is as following:

  1. adding the 'Authenticated PowerShell' task in the agent phase enter image description here
  2. then select the desired service connection from the dropdown list enter image description here
  3. and finally proceed to fill in the remaining details as usual.

That's it!

Upvotes: 0

Vishal Bakshi
Vishal Bakshi

Reputation: 121

I believe you are interpreting the concept of a generic service connection in azure devops incorrectly. The username and Password/Token key is required to connect to your service which is hosted by the URL that you put in the Server URL field, while creating the generic service connection under project settings. It is not supposed to store environment variables for you. Judging by your screenshot, it looks you are using classic pipelines instead of YAML based pipelines. The powershell task that you are trying to configure doesn't allow you to add a service connection. However Azure Powershell task will allow you to do that.

But for your requirement, all you need to do is add your api keys/secrets under the variables tab, beside Tasks. That should fix your issue.

enter image description here

Upvotes: 0

Related Questions