如何使用 azure devops 构建管道验证 npm 注册表?

问题描述 投票:0回答:3

我有一个应用程序,正在为 Azure DevOps 创建构建管道。它使用私有 npm 注册表中的 npm 包(包含从不同的 Azure DevOps 组织创建的代码)。当我运行

npm ci
(或
npm install
)时,它失败并出现以下错误:

npm 错误!代码 E401

npm 错误!无法进行身份验证,需要:Bearerauthorization_uri=https://login.windows.net/b2d01466-6e2c-4b55-8b90-e3ed41afca4a,Basicrealm="https://pkgsproduks1.pkgs.visualstudio.com/",TFS -联邦

失败的具体包是来自其他组织的包,当尝试获取它们时会返回

401

我认为验证这一点的最佳实践是在 Azure DevOps 中创建服务连接。我在托管 npm 包的组织内创建了一个个人访问令牌,并使用它在包含我的构建管道的组织中创建了一个服务连接。然后我将其包含在我的构建管道 yaml 中,如下所示:

- task: Npm@1
  displayName: Install npm packages
  inputs:
    command: 'ci'
    workingDir: 'Path/To/Working/Directory'
    customEndpoint: 'Custom npm registry'

在此之前我也尝试过使用

npm authenticate
构建步骤(无论安装步骤中是否有
customEndpoint: 'Custom npm registry'
),虽然
npm authenticate
成功运行,但它对我的错误没有任何影响得到。我还尝试将服务连接设置为使用我的用户名和密码而不是 PAT,但这也没有什么区别。

我的项目中的

.npmrc
如下(稍作修改):

registry=https://registry.npmjs.org/
@{scope}:registry=https://pkgs.dev.azure.com/{organisation}/_packaging/{feedName}/npm/registry/
@{scope}:always-auth=true

任何人都可以看到身份验证出了什么问题,或者链接到提供跨多个 Azure DevOps 组织执行此操作的示例的文章吗?

authentication npm azure-devops
3个回答
4
投票

我刚刚花了几个小时使用 Azure DevOps 的托管构建代理对类似的 NPM 身份验证问题进行故障排除。

我确实在管道中有

NPM Authenticate
工作,但我仍然遇到这个错误:

npm ERR! code E401
npm ERR! Unable to authenticate, your authentication token seems to be invalid.
npm ERR! To correct this please trying logging in again with:
npm ERR!     npm login

事实证明,该项目在分散的

https://pkgs.dev.azure.com/<myorg>/_packaging
文件中混合了
https://<myorg>.pkgs.visualstudio.com/_packaging
.npmrc
(同一 NPM 注册表的旧 URL)。

让它们全部一致使用

https://pkgs.dev.azure.com/<myorg>/_packaging
解决了我的问题。


1
投票

.npmrc
应如下所示:

registry=https://pkgs.dev.azure.com/{organization}/{project}/_packaging/{feed}/npm/registry/
@{scope}:registry=https://pkgs.dev.azure.com/{otherorganization}/_packaging/{feed}/npm/registry/
@{otherscope}:registry=https://{thirdPartyRepository}/npm/registry/
always-auth=true

0
投票

我遇到的问题是我的组织名称包含空格。由于某种原因,ADO 添加了空格而不是使用 %20 对其进行编码。

因此,它没有使用

Subname1%20Subname2
对其进行编码,而是使用
Subname1Subname2
进行编码,这会在各处创建 404。

我做了什么:将 %20 硬编码到

.npmrc
文件中的我的组织名称部分

© www.soinside.com 2019 - 2024. All rights reserved.