node.js - CircleCI 忘记机器执行器上的 Node 版本

标签 node.js circleci nvm

我使用 nvm 将 Node 版本设置为 10.15.1,并在下一个 run 步骤中返回到 6.1.0。我尝试了几种变体,包括这个:https://www.cloudesire.com/how-to-upgrade-node-on-circleci-machine-executor/

我是否遗漏了一些明显的东西?我只需要每个 run 步骤记住我在第一个步骤中设置的 Node 版本,这样在这种情况下它们都将使用 10.15.1

这是我的工作流程中的工作:

dev:
  environment:
    BASH_ENV: run/env/test/.env
  machine:
    image: circleci/classic:latest
  steps:
  - checkout
  - run:
      name: Install node@10
      command: |
        set +e

        curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

        nvm install 10
        nvm alias default 10

        rm -rf ~./node-gyp

        node -v # prints 10.15.1 as expected

  - run:
      name: Install yarn and rsync
      command: |
        node -v # prints 6.1.0

        export NVM_DIR="/opt/circleci/.nvm"
        [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

        node -v # prints 6.1.0

        curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
        echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
        sudo apt-get update && sudo apt-get install yarn rsync

  - run:
      name: Install node modules
      command: |
        node -v # prints 6.1.0

        yarn install # this is what is failing because of the unexpected node version

  - run:
      name: Deploy to Dev Server
      command: |
        if [ "${CIRCLE_BRANCH}" == "master" ]; then rsync -arhvz --exclude .git/ -e "ssh -o StrictHostKeyChecking=no" --progress \
        ./ ubuntu@xxx.xxx.xxx.xxx:/var/www/xxx/xxx/; fi
        if [ "${CIRCLE_BRANCH}" == "master" ]; then ssh -o StrictHostKeyChecking=no ubuntu@xxx.xxx.xxx.xxx 'cd /var/www/xxx/xxx && pm2 restart all --update-env'; fi

最佳答案

通过将已安装的 Node 添加到 $PATH 来解决

示例:

    - run:
        name: 'Install Project Node'
        command: |
          set +x
          source ~/.bashrc

          nvm install 12
          NODE_DIR=$(dirname $(which node))
          echo "export PATH=$NODE_DIR:\$PATH" >> $BASH_ENV

关于node.js - CircleCI 忘记机器执行器上的 Node 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55027617/

相关文章:

javascript - 尝试在 NodeJS 上使用 PFUser

pm2 - 找不到 npm 命令 pm2 : command not found

node.js - Github Actions 和 npm - npm : command not found

automated-tests - 如何修复 CircleCI 中长时间运行的 testcafe 测试

react-native - 如何在 CircleCI 2.0 上配置顺序工作流?

node.js - 在 Mac OS 中启动终端时,我突然收到 nvm 安装错误消息

javascript - 以编程方式保存 chrome 的控制台日志

javascript - 如何访问 meteor 包内的变量(函数)?

angularjs - 通过 socket.io 更新实时 d3 图表

python - 如何使用 python3、CircleCI 和 Mayavi 对图形进行单元测试