问题描述
我的JavaScript项目成功地利用了Dockerfile
中的多阶段构建过程。在“构建”阶段,将使用作为Docker构建arg提供的密钥动态构建.npmrc
文件,如下所示:
FROM node:12.16-alpine AS build
ARG ACCESS_TOKEN
WORKDIR /app
COPY package*.json ./
RUN echo "//npm.pkg.github.com/:_authToken=$ACCESS_TOKEN" > .npmrc && \
echo "@my_org:registry=https://npm.pkg.github.com" >> .npmrc && \
npm ci --production && \
rm -f .npmrc
# ...remainder of file omitted
我知道我可以在命令行中提供令牌,如下所示:NPM_TOKEN=$ACCESS_TOKEN npm ci --production
。但是,此操作失败,如下所示:
... earlier docker build output omitted
---> 68c5ba096f60
Step 6/16 : RUN NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/
---> Running in 418c51a85c15
npm ERR! code E401
npm ERR! Unable to authenticate,need: Basic realm="GitHub Package Registry"
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-09-09T11_07_17_789Z-debug.log
failed to build: couldn't build "my_org/my-project": unable to stream build output: The command '/bin/sh -c NPM_TOKEN=$ACCESS_TOKEN npm ci --production --registry https://npm.pkg.github.com/' returned a non-zero code: 1
我正在尝试吗?还是必须总是有一个.npmrc
文件?某处用于身份验证?
解决方法
您尝试通过在npm cmd之前设置为function removeEmoji($text) {
return preg_replace('/([0-9|#][\x{20E3}])|[\x{00ae}|\x{00a9}|\x{203C}|\x{2047}|\x{2048}|\x{2049}|\x{3030}|\x{303D}|\x{2139}|\x{2122}|\x{3297}|\x{3299}][\x{FE00}-\x{FEFF}]?|[\x{2190}-\x{21FF}][\x{FE00}-\x{FEFF}]?|[\x{2300}-\x{23FF}][\x{FE00}-\x{FEFF}]?|[\x{2460}-\x{24FF}][\x{FE00}-\x{FEFF}]?|[\x{25A0}-\x{25FF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{FE00}-\x{FEFF}]?|[\x{2600}-\x{27BF}][\x{1F000}-\x{1FEFF}]?|[\x{2900}-\x{297F}][\x{FE00}-\x{FEFF}]?|[\x{2B00}-\x{2BF0}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F9FF}][\x{FE00}-\x{FEFF}]?|[\x{1F000}-\x{1F9FF}][\x{1F000}-\x{1FEFF}]?/u','',$text);
}
吗?
ENV
在您的命令行中,您正在传递ENV NPM_TOKEN=$ACCESS_TOKEN
,我认为应该是NPM_TOKEN=$ACCESS_TOKEN
,因为dockerfile中的ACCESS_TOKEN=${NPM_TOKEN}
变量是ARG
https://docs.npmjs.com/docker-and-private-modules#update-the-dockerfile