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

Jam*_*s B 19 npm azure-pipelines

我正在尝试使用vsts-npm-auth获取 VSTS 包存储库的身份验证令牌。在我的开发机器上我可以运行命令

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

它成功地为我提供了身份验证令牌。我现在尝试将其重新创建为 VSTS 上的构建步骤,因此我创建了 powershell 脚本auth-vsts.ps1

$npmrcFile = "$PSScriptRoot\path-to-my\.npmrc";
npm install -g vsts-npm-auth;
vsts-npm-auth -config $npmrcFile;
Run Code Online (Sandbox Code Playgroud)

并将其添加为 powershell 任务。但是任务失败如下

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
Run Code Online (Sandbox Code Playgroud)

该错误没有说明为什么无法获取凭据。有什么想法可能是为什么吗?

小智 69

在您的项目中,您可以打开终端并运行

 vsts-npm-auth -F -C .npmrc
Run Code Online (Sandbox Code Playgroud)

此脚本刷新 npm 令牌。这里我设置了两个参数:-F强制刷新(如果不设置,只有已经过期的 token 才会刷新),而-C fileName 定义配置文件。

  • 强制选项对我来说很有效,谢谢! (3认同)

小智 13

我在尝试通过 Visual Studio Code 的powershell终端执行时遇到了这个问题

vsts-npm-auth -config .npmrc
Run Code Online (Sandbox Code Playgroud)

但通过简单的控制台运行相同的命令解决了这个问题,我被重定向到身份验证窗口。

可以建议由于内部限制 powershell 禁用打开另一个窗口。

  • 我必须使用“-F”“强制”标志才能使其工作。https://learn.microsoft.com/en-us/azure/devops/artifacts/npm/npmrc?view=azure-devops&tabs=windows%2Cclassic#unable-to-authenticate (27认同)

Thu*_*huy 5

我们运行这个命令:

  1. npm i -g vsts-npm-auth

  2. Set-ExecutionPolicy -Scope Process -ExecutionPolicy 绕过

  3. vsts-npm-auth-config .npmrc -F

  4. 尼普我


Edd*_*SFT 3

该错误确实表明了无法获取凭据的原因:

The prompt option is invalid because the process is not interactive.
Run Code Online (Sandbox Code Playgroud)

这可能是由于构建代理未在交互模式下运行而导致无法提示凭据对话框。如果您使用托管构建代理,构建代理将作为服务运行,并且没有任何方法可以更改为交互模式。

但是,这里的问题是,如果您想在构建步骤中使用提要,则在构建过程中提示凭据对话框是没有意义的,因为构建步骤无法自动输入所需的凭据。不确定您的环境是否有任何特定要求,但一般工作流程应该是将本地计算机中生成的 .npmrc 文件上传到源代码管理,以便 npm 可以使用文件中的身份验证令牌将包安装/发布到 VSTS喂养。