升级Node.js至v16后GitHub Actions中npm权限错误求助
升级Node.js至v16.18.1后GitHub Actions中ESLint执行EACCES权限错误
问题背景
将Node.js从v14.15升级至v16.18.1后,在GitHub Actions的CI/CD工作流中运行ESLint时出现Error: EACCES: permission denied, scandir '/root/.npm/_logs'错误。操作在Docker容器内执行,本地使用相同镜像构建的容器可正常运行ESLint脚本,但在GitHub Actions中工作流失败。
错误详情
Creating csps-lint ... done Attaching to csps-lint csps-lint | csps-lint | > lint csps-lint | > bash ./scripts/lint.sh csps-lint | csps-lint | Running Prettier to identify code formatting issues... csps-lint | csps-lint | Checking formatting... csps-lint | All matched files use Prettier code style! csps-lint | csps-lint | Running ESLint to identify code quality issues... csps-lint | npm WARN logfile Error: EACCES: permission denied, scandir '/root/.npm/_logs' csps-lint | npm WARN logfile error cleaning log files [Error: EACCES: permission denied, scandir '/root/.npm/_logs'] { csps-lint | npm WARN logfile errno: -13, csps-lint | npm WARN logfile code: 'EACCES', csps-lint | npm WARN logfile syscall: 'scandir', csps-lint | npm WARN logfile path: '/root/.npm/_logs' csps-lint | npm WARN logfile } csps-lint | npm ERR! code EACCES csps-lint | npm ERR! syscall mkdir csps-lint | npm ERR! path /root/.npm/_cacache/tmp csps-lint | npm ERR! errno -13 csps-lint | npm ERR! csps-lint | npm ERR! Your cache folder contains root-owned files, due to a bug in csps-lint | npm ERR! previous versions of npm which has since been addressed. csps-lint | npm ERR! csps-lint | npm ERR! To permanently fix this problem, please run: csps-lint | npm ERR! sudo chown -R 1001:123 "/root/.npm" csps-lint | csps-lint | npm ERR! Log files were not written due to an error writing to the directory: /root/.npm/_logs csps-lint | npm ERR! You can rerun the command with `--loglevel=verbose` to see the logs in your terminal csps-lint | ESLint found code quality issues. Please address any errors above and try again. csps-lint | csps-lint exited with code 1 1 Aborting on container exit... Error: Process completed with exit code 1.
相关配置文件
GitHub Actions工作流配置
name: test-eslint-fix on: push: branches: - eslint-fix jobs: login: runs-on: ubuntu-latest steps: - uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} linting: needs: login runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: LINT_COMMAND=lint docker-compose -f docker-comp-lint.yml up --abort-on-container-
Dockerfile
# start FROM a base layer of node v16.18.1 FROM node:16.18.1 # confirm installation RUN node -v RUN npm -v # Globally install webpack in the container for webpack dev server RUN npm install webpack -g # Install db-migrate RUN npm install db-migrate@0.11.11 -g # Set up a WORKDIR for application in the container WORKDIR /usr/src/app # A wildcard is used to ensure both package.json AND package-lock.json are copied COPY package*.json /usr/src/app/ # npm install to create node_modules in the container RUN npm install # EXPOSE your server port EXPOSE 3000
docker-compose配置
version: '3' services: csps-lint: image: <<my image>> container_name: csps-lint ports: - '3002:3002' volumes: - ./:/usr/src/app - node_modules:/usr/src/app/node_modules environment: - NODE_ENV=test - LINT_COMMAND=${LINT_COMMAND} command: npm run ${LINT_COMMAND} volumes: node_modules:
ESLint执行脚本(lint.sh)
# Run Prettier in check mode and store status echo -e "\033[1;33mRunning Prettier to identify code formatting issues...\033[0m\n" prettier --check . PRETTIER_STATUS=$? # Run ESLint in check mode and store status echo -e "\n\033[1;33mRunning ESLint to identify code quality issues...\033[0m" npx eslint . ESLINT_STATUS=$?
已尝试的解决方法
- 将错误提示中推荐的修复命令
sudo chown -R 1001:123 "/root/.npm"添加到lint.sh脚本、Dockerfile、docker-compose文件中,均未解决问题,部分场景下还出现无sudo权限的情况。 - 参考过其他类似错误的解决方案,但均无效。
关键发现
本地运行脚本时用户为root,但CI/CD中运行时用户为ID 1001的匿名用户,同一镜像为何用户不同?
本地执行docker exec csps-lint ls -la /root/.npm/_logs的输出:
total 1080 drwxr-xr-x 1 root root 4096 Dec 2 23:02 . drwxr-xr-x 1 root root 4096 Dec 2 20:15 .. -rw-r--r-- 1 root root 1589 Nov 15 06:46 2022-11-15T06_46_44_197Z-debug-0.log -rw-r--r-- 1 root root 1589 Dec 2 20:15 2022-12-02T20_15_05_450Z-debug-0.log -rw-r--r-- 1 root root 65102 Dec 2 20:15 2022-12-02T20_15_05_684Z-debug-0.log -rw-r--r-- 1 root root 59037 Dec 2 20:15 2022-12-02T20_15_09_141Z-debug-0.log -rw-r--r-- 1 root root 943351 Dec 2 20:15 2022-12-02T20_15_14_433Z-debug-0.log -rw-r--r-- 1 root root 1706 Dec 2 20:23 2022-12-02T20_23_05_075Z-debug-0.log -rw-r--r-- 1 root root 1756 Dec 2 20:23 2022-12-02T20_23_16_787Z-debug-0.log -rw-r--r-- 1 root root 1599 Dec 2 23:02 2022-12-02T23_02_23_793Z-debug-0.log
内容的提问来源于stack exchange,提问作者Kyle Combs
相关产品推荐
相关产品推荐

