使用Artillery容器在CI流水线中执行npm ci失败的问题求助
npm ci时出现EACCES权限错误的解决方法 问题场景
使用Artillery官方容器artilleryio/artillery:latest在GitHub Actions流水线中执行夜间性能测试,本地运行正常,但流水线内执行npm ci时触发权限错误,核心报错信息:
npm ERR! code FETCH_ERROR
npm ERR! errno FETCH_ERROR
npm ERR! invalid json response body at https://registry.npmjs.org/jsesc reason: Invalid response body while trying to fetch https://registry.npmjs.org/jsesc: EACCES: permission denied, mkdir '/github/home/.npm/_cacache/content-v2/sha512/b6/68'
当前工作流配置:
jobs: performance-tests: runs-on: ubuntu-latest permissions: contents: 'read' id-token: 'write' environment: name: dev container: artilleryio/artillery:latest steps: - name: NPM Clean run: npm cache clean --force - name: NPM Install run: npm ci - name: Run Performance Tests env: AUTHORIZATION_LOGIN_PATH: "" run: //home/node/artillery/bin/run run -o notification.json test/performance/perf-tests.yml - name: Generate Performance Test run: //home/node/artillery/bin/run report --output notification.html notification.json
核心原因
Artillery容器默认使用node用户运行,但GitHub Actions挂载的/github/home目录属于root用户,node用户无写入权限,导致npm无法创建缓存目录。
解决方法
方法1:指定npm缓存目录到有权限的路径
修改npm ci命令,通过--cache参数指定缓存目录为容器内node用户的主目录(默认有权限):
- name: NPM Install run: npm ci --cache /home/node/.npm
方法2:切换到root用户执行命令
在步骤中添加sudo提升权限,或直接指定用户为root:
# 方式A:用sudo - name: NPM Install run: sudo npm ci # 方式B:指定执行用户 - name: NPM Install run: npm ci user: root
方法3:修改目录权限
在执行npm命令前,修改/github/home目录的归属,让node用户可写入:
- name: Fix Directory Permissions run: sudo chown -R node:node /github/home - name: NPM Install run: npm ci
方法4:使用--unsafe-perm参数(测试场景可用)
允许npm以当前用户权限运行安装,绕过权限检查(不推荐生产环境使用):
- name: NPM Install run: npm ci --unsafe-perm
验证建议
优先尝试方法1,它符合容器最小权限原则,无需提升权限。修改配置后重新运行流水线,确认权限问题是否解决。
内容的提问来源于stack exchange,提问作者D.Hodges

