Azure Devops:即使设置了 NPM 身份验证,也无法使用 NPM 私有注册表构建映像 [英] Azure Devops: Cannot Build an Image using NPM private registry even after setting NPM Authenticate

查看:37
本文介绍了Azure Devops:即使设置了 NPM 身份验证,也无法使用 NPM 私有注册表构建映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am always getting an error on npm install after setting NPM Authenticate. I would like to authenticate to my npm private registry during image build and install all the dependencies I need. Maybe I misunderstood how this authentication process works but this is what I am doing:

构建管道

我尝试从项目设置页面建立服务连接,如构建和发布的服务连接

I tried establishing a service connection from the project settings page as in Service connections for builds and releases

之后,我还按照 使用 Task Runner(例如使 gulp 工作)

但这行不通.这些是我得到的错误:

But this is not working. These are the errors I am getting:

在NPM 身份验证"阶段:

During 'NPM Authenticate' phase:

[警告]找到并覆盖了选定的 .npmrc 文件中的 myregistry.pkgs.visualstudio.com 注册表.从文件中删除凭据并将它们存储在 npm 服务中连接代替(推荐),或删除 npm Authenticate 任务从您的构建中使用已签入 .npmrc 的凭据.

[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.

在构建映像"阶段:

第 4/7 步:运行 npm install --production ---> 在 8724f713f1db 中运行[91mnpm 错误!代码[0m[91m E404 [0m[91mnpm [0m[91mERR!404[0m[91m 无发现:@myregistry/service-logging@latest npm ERR![0m[91m A complete可以在以下位置找到此运行的日志:npm ERR!
/root/.npm/_logs/2018-09-11T04_20_00_513Z-debug.log [0m命令'/bin/sh -c npm install --production' 返回非零代码:1[错误]命令'/bin/sh -c npm install --production'返回非零代码:1 [错误]/usr/local/bin/docker failed with return代码:1 [部分]完成:构建图像

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 文件:

This is my .npmrc file:

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:

And this is my 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

任何帮助我解决这个问题的帮助将不胜感激!谢谢!

Any help to unblock me from this issue will be highly appreciated! Thanks!

推荐答案

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

I finally resolved this issue in my pipeline by removing the last two lines in my .npmrc file. the last line was causing an issue. After the NPM Authenticate task, my .npmrc file was being modified to:

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 插入的配置被忽略,导致管道错误:

Somehow, the following config was being taken into consideration and the config the NPM Authenticate inserted was being ignored, causing the pipeline error:

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

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

Also, no need to include the following line since NPM Authenticate will do the job for you:

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

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

By removing the line above, this warning disappeared:

[警告]找到并覆盖了选定的 .npmrc 文件中的 myregistry.pkgs.visualstudio.com 注册表.从文件中删除凭据并将它们存储在 npm 服务中连接代替(推荐),或删除 npm Authenticate 任务从您的构建中使用已签入 .npmrc 的凭据.

[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 文件像这样简单:

So, to conclude, just keep your .npmrc file as simple as this:

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

Dockerfile 一切正常.

Everything was fine with the Dockerfile.

这篇关于Azure Devops:即使设置了 NPM 身份验证,也无法使用 NPM 私有注册表构建映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆