Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tbolon opened this issue Apr 2, 2024 · 4 comments

Comments

@tbolon
Copy link

tbolon commented Apr 2, 2024

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.

@tbolon tbolon changed the title How to authenticate using accesstoken to call devops API from custom Powershell task How to authenticate using AccessToken to call DevOps API from custom PowerShell task Apr 2, 2024
@vmapetr vmapetr added question and removed triage labels Apr 2, 2024
@vmapetr
Copy link
Contributor

vmapetr commented Apr 4, 2024

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

@DenisNikulin5
Copy link
Contributor

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)

@aleksandrlevochkin
Copy link
Contributor

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

@tbolon
Copy link
Author

tbolon commented Apr 24, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants