简体   繁体   English

vsts-npm-auth 无法在 VSTS 构建中获取身份验证令牌

[英]vsts-npm-auth can't get authentication token on VSTS build

I'm attempting to use vsts-npm-auth to get the authentication token for our VSTS package repository.我正在尝试使用vsts-npm-auth获取我们的 VSTS package 存储库的身份验证令牌。 On my development machine I can run the commands在我的开发机器上,我可以运行命令

npm install -g vsts-npm-auth vsts-npm-auth -config path-to-my\.npmrc

and it succeeds in providing me with an authentication token.它成功地为我提供了一个身份验证令牌。 I'm now trying to recreate this as a build step on VSTS, so I create the powershell script auth-vsts.ps1我现在正在尝试将其重新创建为 VSTS 上的构建步骤,因此我创建了 powershell 脚本auth-vsts.ps1

$npmrcFile = "$PSScriptRoot\path-to-my\.npmrc";
npm install -g vsts-npm-auth;
vsts-npm-auth -config $npmrcFile;

and add it as a powershell task.并将其添加为 powershell 任务。 However, the task fails as follows但是,任务失败如下

2017-05-30T09:37:41.1082686Z ##[section]Starting: auth-vsts
2017-05-30T09:37:41.1092712Z ==============================================================================
2017-05-30T09:37:41.1092712Z Task         : PowerShell
2017-05-30T09:37:41.1092712Z Description  : Run a PowerShell script
2017-05-30T09:37:41.1092712Z Version      : 1.2.3
2017-05-30T09:37:41.1092712Z Author       : Microsoft Corporation
2017-05-30T09:37:41.1092712Z Help         : [More Information](https://go.microsoft.com/fwlink/?LinkID=613736)
2017-05-30T09:37:41.1092712Z ==============================================================================
2017-05-30T09:37:41.1112679Z ##[command]. 'd:\a\1\s\auth-vsts.ps1' 
2017-05-30T09:37:47.3792461Z C:\NPM\Modules\vsts-npm-auth -> C:\NPM\Modules\node_modules\vsts-npm-auth\bin\vsts-npm-auth.exe
2017-05-30T09:37:47.3792461Z C:\NPM\Modules
2017-05-30T09:37:47.3802239Z `-- vsts-npm-auth@0.25.0 
2017-05-30T09:37:47.3802239Z 
2017-05-30T09:37:47.3802239Z 
2017-05-30T09:37:47.3802239Z vsts-npm-auth v0.25.0.0 
2017-05-30T09:37:47.3802239Z -----------------------
2017-05-30T09:37:47.3802239Z Creating npmrcFile. Path: D:\a\1\s\.npmrc
2017-05-30T09:37:47.3802239Z Getting new credentials for source:https://our-domain/_packaging/SharedLib/npm/registry/, scope:vso.packaging_write vso.drop_write
2017-05-30T09:37:49.8729702Z Caught exception: The prompt option is invalid because the process is not interactive.
2017-05-30T09:37:49.8729702Z Parameter name: PromptType
2017-05-30T09:37:49.8729702Z Caught exception: The prompt option is invalid because the process is not interactive.
2017-05-30T09:37:49.8729702Z Parameter name: PromptType
2017-05-30T09:37:49.8729702Z Couldn't get an authentication token for //our-domain/_packaging/SharedLib/npm/registry/:_authToken.
2017-05-30T09:37:50.1769711Z ##[error]Process completed with exit code 1.
2017-05-30T09:37:50.1809715Z ##[section]Finishing: auth-vsts

The error gives no indication as to why it can't obtain the credentials.该错误没有说明为什么它无法获取凭据。 Any ideas why this might be?任何想法为什么会这样?

The error did indicate why it cannot obtain the credentials:该错误确实表明它无法获取凭据的原因:

The prompt option is invalid because the process is not interactive.

This could be caused by the build agent does not run in interactive mode which make the credential dialog cannot be prompted.这可能是由于构建代理未在交互模式下运行导致无法提示凭据对话框。 If you are using Hosted Build Agent, the build agent is run as service and there isn't any way to change to interactive mode.如果您使用的是托管构建代理,构建代理将作为服务运行,并且无法更改为交互模式。

However, the issue here is that if you want to use the feed in a build step, it does not make sense to prompt a credential dialog during the build process since the build step cannot enter the required credential automatically.但是,这里的问题是,如果您想在构建步骤中使用提要,则在构建过程中提示凭据对话框是没有意义的,因为构建步骤无法自动输入所需的凭据。 Not sure if there is any specific requirement in your environment, but the general workflow should be uploading the .npmrc file generated in your local machine to the Source Control so that npm can use the auth token in the file to install/publish packages to VSTS Feed.不确定您的环境是否有任何特定要求,但一般工作流程应该是将本地机器中生成的 .npmrc 文件上传到源代码管理,以便 npm 可以使用文件中的身份验证令牌来安装/发布包到 VSTS喂养。

I faced this issue while trying to execute via Visual Studio Code`s powershell terminal我在尝试通过 Visual Studio Code 的powershell终端执行时遇到了这个问题

vsts-npm-auth -config .npmrc

But running the same command via simple console solved this issue and I was redirected to authentication window.但是通过简单的控制台运行相同的命令解决了这个问题,我被重定向到身份验证窗口。

Can suggest that due to internal limitations powershell disabled to open another windows.可以提示由于内部限制powershell禁用打开另一个窗口。

The vsts authentication system sometimes authenticates the use by popping up a browser window. vsts 认证系统有时通过弹​​出浏览器窗口来认证使用。 If the terminal you're running the command from is not interactive (eg, ssh terminal, vscode terminal) it won't be able to pop up that window, and the authentication will fail.如果您运行命令的终端不是交互式终端(例如,ssh 终端、vscode 终端),它将无法弹出该窗口,并且身份验证将失败。

This worked for me这对我有用

npx vsts-npm-auth -config.npmrc npx vsts-npm-auth -config.npmrc

Inside your project, you can open a terminal and run在你的项目中,你可以打开一个终端并运行

 vsts-npm-auth -F -C .npmrc

This script refreshes the npm token.此脚本刷新 npm 令牌。 Here I set two parameters: -F forces the refresh (if not set, the token is refreshed only if it is already expired), while -C fileName defines the configuration file.这里我设置了两个参数: -F强制刷新(如果不设置,token只有已经过期才会刷新),而-C fileName定义了配置文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM