microsoft/azure-pipelines-task-lib

How to authenticate using AccessToken to call DevOps API from custom PowerShell task

tbolon opened this issue · 4 comments

Environment

azure-pipelines-task-lib version: 4.10.1

Issue Description

Hi,

I want to create a custom PowerShell task to promote packages uploaded to Azure Artifacts automatically during the build pipeline.

I have successfully created a custom task, but I can't find a way to authenticate my request.

I have found a similar issue #579 but for the typescript language.

I build the url using the environment variables, which works:

$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)_apis/packaging/feeds/$feedName/nuget/packages/" + $pkgName + "/versions/" + $pkgVersion + "?api-version=7.0"

I wanted to authenticate using an Authentication HTTP header build manually using $env:SYSTEM_ACCESSTOKEN and using Invoke-RestMethod but it seems the access token is not available in the scope of the custom task.

How am I supposed to call the Azure DevOps server API from inside a PowerShell custom task?

I did not find an existing task doing the same using PowerShell.

There seems to exists a VSTS .NET Library wrapper, but I am not sure I can easily PATCH the package version to publish it to a field, and I can not find how I am supposed to import this library.

Hi @tbolon, thank you for your question! We'll take a look at this soon.

Hi @tbolon
You can access the token in your pipeline yaml like this:

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      # Write your PowerShell commands here.
      
      Write-Host $(System.AccessToken)

Hi @tbolon! I'll close this issue since it seems to be resolved. If you still have this problem, feel free to re-open.

Hello @aleksandrlevochkin and @DenisNikulin5, sorry for not replying ealrier, I was sure I had answered your suggestion.

This comment does not work, because you imply that I want to use a powershell script. But in my case I want to create a powershell custom task, and I can't access variables inside my custom script.