全局 NPM 包安装的简单 CircleCI 2.0 配置失败

Ale*_*lls 6 node.js npm docker circleci circleci-2.0

我有一个运行良好的 Dockerfile:

FROM node:10
RUN npm set unsafe-perm true
RUN npm install -g '@oresoftware/r2g@0.0.132'
Run Code Online (Sandbox Code Playgroud)

但是与上述 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'"
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的 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
Run Code Online (Sandbox Code Playgroud)

CircleCI 2.0 应该使用 Docker,所以我不确定为什么会发生这个权限错误。

rob*_*E13 9

tldr - 使用以下前缀:

npm install --prefix=$HOME/.local --global serverless
Run Code Online (Sandbox Code Playgroud)
  • 替换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是可执行的。


vst*_*stm 6

如上所述,顶部的 Dockerfile 与 CircleCI-config 中的不完全相同。在 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'
Run Code Online (Sandbox Code Playgroud)

使用此 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'"
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这样您就不必sudo每次要使用该软件包时都这样做。