我有一个工作正常的Dockerfile:
FROM node:10
RUN npm set unsafe-perm true
RUN npm install -g '@oresoftware/r2g@0.0.132'
但镜像上述Dockerfile的相同CircleCI config.yml文件不起作用:
{
"version": 2,
"jobs": {
"build": {
"docker": [
{
"image": "circleci/node:10"
}
],
"steps": [
{
"run": "npm set unsafe-perm true"
},
{
"run": "npm install -g --loglevel=warn '@oresoftware/r2g@0.0.132'"
}
]
}
}
}
我使用上面的config.yml文件在CircleCI上收到以下错误:
#!/bin/bash -eo pipefail
npm install -g --loglevel=warn @oresoftware/r2g
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR! stack:
npm ERR! 'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'access',
npm ERR! path: '/usr/local/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
npm ERR! A complete log of this run can be found in:
npm ERR! /home/circleci/.npm/_logs/2018-06-18T18_26_53_651Z-debug.log
Exited with code 243
CircleCI 2.0应该使用Docker,所以我不确定为什么会发生这种权限错误。
答案 0 :(得分:4)
如前所述,顶部的Dockerfile与CircleCI-config中的Dockerfile并不完全相同。在Dockerfile中,基本映像为node
,默认情况下,该映像在root
用户下运行。
另一方面,circleci/node
图像将落给无特权的circleci
用户。因此,基于node
映像的100%相同的Dockerfile看起来像这样:
FROM node:10
RUN useradd -m circleci
USER circleci
RUN npm set unsafe-perm true
RUN npm install -g '@oresoftware/r2g@0.0.132'
使用此Dockerfile,出现与CircleCI中相同的错误。
一个解决方案是使用sudo
,问题是您必须在每个使用已安装的节点软件包的命令上使用sudo
(因为使用sudo,实际上安装在/root
用户无法访问的circleci
目录中)。
我认为更好的选择是将软件包安装在circleci
主目录中。
{
"version": 2,
"jobs": {
"build": {
"docker": [
{
"image": "circleci/node:10"
}
],
"steps": [
{
"run": "npm set prefix=/home/circleci/npm && echo 'export PATH=$HOME/circleci/npm/bin:$PATH' >> /home/circleci/.bashrc"
},
{
"run": "npm install -g --loglevel=warn '@oresoftware/r2g@0.0.132'"
}
]
}
}
}
这样,您不必每次使用软件包都sudo
。
答案 1 :(得分:3)
在CircleCI上,您需要使用sudo
。默认用户为circleci
,具有无密码sudo访问权限。
答案 2 :(得分:1)
npm install --prefix=$HOME/.local --global serverless`
serverless
。 CircleCI当前的circleci/node:lts-buster
图片在路径上具有以下内容:
/home/circleci/.local/bin:/home/circleci/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
由于写权限被阻止,我无法写入/home/circleci/bin
。
/home/circleci/.local/bin
--prefix=$HOME/.local
命令中添加npm install
选项意味着随后将全局软件包安装到/home/circleci/.local/bin
serverless
)是可执行的。答案 3 :(得分:0)
对我有用的是将以下环境变量添加到作业中:
environment:
NPM_CONFIG_PREFIX: "~/.npm-global"
然后一步一步修改$PATH:
steps:
- echo 'export PATH=~/.npm-global/bin:$PATH' >> $BASH_ENV
- npm install
- npm install -g some-global-package