Azure Devops:即使在设置 NPM 身份验证后也无法使用 NPM 私有注册表构建映像

Azure Devops: Cannot Build an Image using NPM private registry even after setting NPM Authenticate

设置 NPM Authenticate 后,我总是在 npm install 上收到错误。我想在映像构建期间对我的 npm 私有注册表进行身份验证并安装我需要的所有依赖项。也许我误解了这个身份验证过程是如何工作的,但这就是我正在做的:

构建管道

我尝试从项目设置页面建立服务连接,如 Service connections for builds and releases

之后,我还按照 With a Task Runner (e.g. make gulp work)

中的步骤设置了我的 NPM 身份验证任务

但这不起作用。这些是我收到的错误:

在'NPM Authenticate'阶段:

[warning]Found and overrode credentials for the myregistry.pkgs.visualstudio.com registry in the selected .npmrc file. Remove credentials from the file and store them in an npm service connection instead (recommended), or remove the npm Authenticate task from your build to use credentials checked into an .npmrc.

在'Build an Image'阶段:

Step 4/7 : RUN npm install --production ---> Running in 8724f713f1db [91mnpm ERR! code[0m[91m E404 [0m[91mnpm [0m[91mERR! 404[0m[91m Not Found: @myregistry/service-logging@latest npm ERR![0m[91m A complete log of this run can be found in: npm ERR!
/root/.npm/_logs/2018-09-11T04_20_00_513Z-debug.log [0mThe command '/bin/sh -c npm install --production' returned a non-zero code: 1 [error]The command '/bin/sh -c npm install --production' returned a non-zero code: 1 [error]/usr/local/bin/docker failed with return code: 1 [section]Finishing: Build an image

这是我的 .npmrc 文件:

unsafe-perm=true
package-lock=false
registry=https://myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/
always-auth=true
//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:_authToken=${NPM_TOKEN}
//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/:_authToken=${NPM_TOKEN}

这是我的 Dockerfile:

FROM node:8.9-alpine

ARG NPM_TOKEN

WORKDIR /usr/src/srv/

COPY package.json package.json

COPY .npmrc .npmrc

RUN npm install --production

RUN rm -f .npmrc

COPY . .

EXPOSE 8080

CMD npm start

非常感谢任何帮助我解决这个问题的人!谢谢!

您需要在 .npmrc 文件中包含令牌或在 运行 npm install 命令之前更新此文件。

我最终通过删除 .npmrc 文件中的最后两行解决了管道中的这个问题。最后一行引起了问题。在 NPM Authenticate 任务之后,我的 .npmrc 文件被修改为:

unsafe-perm=true
package-lock=false
registry=https://myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/
always-auth=true

//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/:_authToken=${NPM_TOKEN}

//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:username=VssToken
//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:_password=***
//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:email=VssEmail
//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:always-auth=true

不知何故,以下配置被考虑在内,NPM Authenticate 插入的配置被忽略,导致管道错误:

//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/:_authToken=${NPM_TOKEN}

此外,无需包含以下行,因为 NPM Authenticate 将为您完成这项工作:

//myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/:_authToken=${NPM_TOKEN}

通过删除上面的行,此警告消失了:

[warning]Found and overrode credentials for the myregistry.pkgs.visualstudio.com registry in the selected .npmrc file. Remove credentials from the file and store them in an npm service connection instead (recommended), or remove the npm Authenticate task from your build to use credentials checked into an .npmrc.

总而言之,让您的 .npmrc 文件尽可能简单:

unsafe-perm=true
package-lock=false
registry=https://myregistry.pkgs.visualstudio.com/_packaging/myregistry/npm/registry/
always-auth=true

Dockerfile 一切正常。

我最近遇到了这个,把我发现的东西贴在这里,以防将来对人们(或我自己)有所帮助。

我试图在 Azure DevOps Pipeline 构建期间使用 NpmAuthenticate 任务运行程序针对私有 NPM 源进行身份验证。

我最初的管道是这样的:

- task: npmAuthenticate@0
  displayName: Authenticate Npm Feed
  inputs:
    workingFile: './source/WebApplication/.npmrc'
    customEndpoint: ExternalFeedServiceConnection

- task: Npm@1
  inputs:
    command: 'install'
    workingDir: ./source/WebApplication #path to package.json
    customRegistry: 'useNpmrc'
    displayName: Install NPM Packages

Npm@1 任务将不断失败并出现登录错误。无论尝试什么排列。最终起作用的是用脚本步骤代替它:

  - script: 'npm install'
    workingDirectory: ./source/WebApplication
    displayName: npm install

脚本步骤似乎正在执行与 Npm@1 任务完全相同的命令,但能够很好地进行身份验证。

npmAuthenticate azure devops 任务文档含糊地暗示了这一点,但不清楚为什么它适用于脚本步骤而不适用于 Npm@1 任务。