GitLab CI/CD shell执行器:找不到npm命令

3

在我的服务器上,我已经配置了GitLab Runner使用 shell 执行器。并且 which node 命令的输出为:/home/ubuntu/.nvm/versions/node/v14.11.0/bin/node

因此,我的gitlab-ci.yml文件如下:

stages:
  - prepare
  - check
  - deploy

default:
  before_script:
    - export PATH=$PATH:/home/ubuntu/.nvm/versions/node/v14.11.0/bin

prepare:
  stage: prepare
  only:
    refs:
      - dev
  before_script:
    - export PATH=$PATH:/home/ubuntu/.nvm/versions/node/v14.11.0/bin
  script:
    - npm i --ignore-scripts --include=dev
  cache:
    key: $CI_COMMIT_BRANCH
    paths:
      - node_modules/

prepare 任务报错 npm: command not found,导致流水线失败。如何解决此问题?

4个回答

4

npm命令行之前,您可以向您的script步骤添加以下内容:

- echo "PATH='${PATH}'"

那样,您可以检查路径是否实际上被修改了。 尝试添加引号:
- export PATH="$PATH:/home/ubuntu/.nvm/versions/node/v14.11.0/bin"

2

Shell执行器使用.profile来加载环境变量。您可以通过将以下内容添加到gitlab-runner的$HOME/.profile中,使其使用nvm:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

0

gitlab-runner 用户.profile 中添加。这对我在 Ubuntu 20.04 上有效。

Shell 执行程序使用 .profile 加载 env。您可以通过将以下内容添加到 gitlab-runner $HOME/.profile 中,使 gitlab-runner 使用 nvm

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

0

在这种情况下,您必须在正确的用户(gitlab-runner)中安装nvm。

sudo -iu gitlab-runner   #change user to gitlab-runner
touch ~/.profile #create profile file

安装 nvm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接