我正在尝试使用 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;
并将其添加为 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 `-- [email protected]
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
该错误没有说明为什么无法获取凭据。有什么想法吗?
在您的项目中,您可以打开终端并运行
vsts-npm-auth -F -C .npmrc
此脚本刷新 npm 令牌。这里我设置了两个参数:-F强制刷新(如果不设置,只有已经过期的token才刷新),而-CfileName定义配置文件。
我在尝试通过 Visual Studio Code 的 powershell 终端执行时遇到了这个问题
vsts-npm-auth -config .npmrc
但是通过简单的控制台运行相同的命令解决了这个问题,我被重定向到身份验证窗口。
可以建议由于内部限制,powershell 禁用以打开另一个窗口。
我们运行了这个命令:
npm i -g vsts-npm-auth
设置-ExecutionPolicy -范围流程-ExecutionPolicy绕过
vsts-npm-auth -config .npmrc -F
npm 我
该错误确实表明了无法获取凭据的原因:
The prompt option is invalid because the process is not interactive.
这可能是由于构建代理未在交互模式下运行而导致无法提示凭据对话框。如果您使用托管构建代理,构建代理将作为服务运行,并且没有任何方法可以更改为交互模式。
但是,这里的问题是,如果您想在构建步骤中使用提要,则在构建过程中提示凭据对话框是没有意义的,因为构建步骤无法自动输入所需的凭据。不确定您的环境中是否有任何特定要求,但一般工作流程应该是将本地计算机中生成的 .npmrc 文件上传到源代码管理,以便 npm 可以使用文件中的身份验证令牌将包安装/发布到 VSTS饲料。
vsts认证系统有时会通过弹出浏览器窗口来认证使用。如果您运行命令的终端不是交互式的(例如 ssh 终端、vscode 终端),它将无法弹出该窗口,并且身份验证将失败。
这对我有用
npx vsts-npm-auth -config .npmrc
您可以尝试删除根 .npmrc 的内容,您通常可以在 Users 文件夹中找到它。
然后再尝试一次
npx vsts-npm-auth -config .npmrc